【发布时间】:2019-09-06 11:04:54
【问题描述】:
我正在努力使用 Exchange 在线帐户从网络发送邮件。 最近,在迁移到 Laravel 时,我发现与 PHPMailer 配合良好的现有设置不适用于 Laravel 底层的 SwiftMailer。
PHPMailer 工作原理:
$data = new PHPMailer(true);
$data->CharSet = 'UTF-8';
$data->isSMTP();
$data->Host = config('mail.host');
$data->SMTPAuth = true; // apparently this line does the trick
$data->Username = config('mail.username');
$data->Password = config('mail.password');
$data->SMTPSecure = config('mail.encryption');
$data->Port = config('mail.port');
$data->setFrom('site@mydomain.com','site mailer');
$data->addAddress('me@mydomain.com', 'Me');
$data->Subject = 'Wonderful Subject PHPMailer';
$data->Body = 'Here is the message itself PHPMailer';
$data->send();
与 SwiftMailer 相同的逻辑:
$transport = (new Swift_SmtpTransport(config('mail.host'), config("mail.port"), config('mail.encryption')))
->setUsername(config('mail.username'))
->setPassword(config('mail.password'));
$mailer = new Swift_Mailer($transport);
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['site@mydomain.com'=>'site mailer'])
->setTo(['me@mydomain.com'=>'Me'])
->setBody('Here is the message itself');
$numSent = $mailer->send($message);
SwiftMailer,报错:
530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [xxxxxxxxxxxxx.xxxxxxxx.prod.outlook.com]
smtp-mail.outlook.com 和端口 587 的 SMTP 服务器相同
NB 是的,我很清楚其他地方使用mydomain-com.mail.protection.outlook.com 和端口 25 的建议。但是这样做会在垃圾邮件中收到消息,原因是“我们无法验证发件人”,这是我不能接受的行为。
而且我们谈论的是少量,因此对其他/第 3 方群发邮件服务不感兴趣。
到目前为止,我的发现是,$phpMailer->SMTPAuth = true; 改变了游戏规则。如果没有这一行,它会产生与 SwiftMailer 相同的错误。
问题是如何在SwiftMailer 上强制执行相同的行为?
如前所述,我实际上使用 Laravel Mail,但出于本示例的目的,我直接提取了 SwiftMailer 调用。
编辑: SwiftMailer 有$transport->setAuthMode(),应该与$phpMailer->AuthType 相同。尝试了两者的可用 CRAM-MD5、LOGIN、PLAIN、XOAUTH2 值。
PHPMailer 与所有这些都工作,除了 XOAUTH2。对于 SwiftMailer,这些都没有改变任何东西,仍然给出错误。
EDIT2:
我有 SPF 记录 (DNS TXT) v=spf1 include:spf.protection.outlook.com -all
已解决: 将 tls 添加到 Swift 传输构造中。
显然 PHPMailer 默认为 tls,因为 config('mail.encryption') 是 null。
【问题讨论】:
-
$phpMailer->SMTPAuth = true做了什么,你基本上已经在这里了 -Swift_SmtpTransport类有它的名字是有原因的。 phpMailer 允许您通过该标志进行配置,SwiftMailer 通过提供不同的传输类来实现。 -
“但是这样做,会在垃圾邮件中收到消息,原因是“我们无法验证发件人的身份”,这是我无法接受的行为。” -这应该可以通过为 your 域提供适当的 SPF 记录来解决,允许
mydomain-com.mail.protection.outlook.com作为发件人...... -
对不起,如果
SMTPAuth = true与Swift_SmtpTransport本身相比没有做任何额外的事情,那么为什么PHPMailer 会发送邮件,而SwiftMailer 不会呢?我也有 SPF 记录。 -
stackoverflow.com/a/48933513/10283047 - 您可能需要明确指定您希望这是一个加密连接。
Swift_SmtpTransport构造函数的第三个参数,'ssl'或'tls'。使用 phpMailer 版本,您有$data->SMTPSecure处理该部分。 -
谢谢你,那不见了。
标签: php laravel email swiftmailer outlook.com