首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用cloudflare时php邮件和SMTP

使用cloudflare时php邮件和SMTP
EN

Stack Overflow用户
提问于 2018-02-14 00:02:29
回答 5查看 3K关注 0票数 5

我正在使用cloudflare在我的网站上,我想保持我的服务器的IP (原始IP)的私有,以避免DDoS攻击被直接发送到我的服务器的IP。我的服务器使用Apache,PHP,MySQL。

当使用php邮件发送电子邮件时(即使我使用phpmailer库通过外部SMTP发送电子邮件),我的服务器的IP也会添加到邮件头中。这种情况发生在Google、Mailgun和其他人身上,因为他们的策略可能是在邮件头中写入邮件来源的IP。

目前,我想到的唯一解决方案是创建自己的REST并通过另一台服务器发送电子邮件,这样做需要付出很大的努力:

原始服务器IP通过我的REST向我的邮件服务器IP发送文本格式的电子邮件数据,然后我的邮件服务器IP使用phpmailer的php邮件功能通过SMTP向用户发送电子邮件。这样,我的邮件服务器的IP将出现在电子邮件头中,而不是源服务器的IP中。

有没有更优雅的方法来做这件事?是否有提供rest的邮件服务,如果我使用他们的API,他们将不会在电子邮件头中显示我的服务器的IP?或者可能有一个已经开发的REST /库可以根据我的请求远程发送电子邮件,所以我不需要从头开始开发和测试自己的电子邮件了吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2018-02-22 01:16:07

您应该通过邮件发送邮件(或sendgrid,或jetmail,或SES,或.)通过他们的API,而不是SMTP协议,您的IP不会被泄露。

例如,对于Mailgun:https://github.com/mailgun/mailgun-php

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$mg = Mailgun::create('key-example');

# Now, compose and send your message.
# $mg->messages()->send($domain, $params);
$mg->messages()->send('example.com', [
  'from'    => 'bob@example.com',
  'to'      => 'sally@example.com',
  'subject' => 'The PHP SDK is awesome!',
  'text'    => 'It is so simple to send a message.'
]);

但是大多数提供商都有SDK:

此外,I建议使用功能强大的 SwiftMailer来处理电子邮件。最酷的一点是,它抽象了传输,您可以使用包从SMTP或任何提供程序API切换。

票数 8
EN

Stack Overflow用户

发布于 2018-02-18 11:19:55

您可以使用例如mailchimp、amazon或其他邮件服务提供商,它们不应该添加您的ip。但这些服务是有报酬的。

票数 0
EN

Stack Overflow用户

发布于 2018-02-18 12:45:34

很久以前,在大学里,我不能使用php邮件命令,因为防火墙规则,所以写我自己的SMTP编辑类。后来的一段时间,我开始使用PHPMailer类,我从来没有遇到过其他问题,甚至使用Gmail作为发件人。看看https://github.com/PHPMailer/PHPMailer

这是一个简单的例子:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48782301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文