【问题标题】:SwiftMailer force SMTPAuth like PHPMailerSwiftMailer 强制 SMTPAuth 像 PHPMailer
【发布时间】: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 = trueSwift_SmtpTransport 本身相比没有做任何额外的事情,那么为什么PHPMailer 会发送邮件,而SwiftMailer 不会呢?我也有 SPF 记录。
  • stackoverflow.com/a/48933513/10283047 - 您可能需要明确指定您希望这是一个加密连接。 Swift_SmtpTransport 构造函数的第三个参数,'ssl''tls'。使用 phpMailer 版本,您有 $data->SMTPSecure 处理该部分。
  • 谢谢你,那不见了。

标签: php laravel email swiftmailer outlook.com


【解决方案1】:

https://stackoverflow.com/a/48933513/10283047 处理用 Java 发送邮件,但这里的基本问题似乎是一样的:

如果您通过端口 587 进行连接,您最初会启动一个普通连接,您必须通过显式发送 STARTTLS 命令来启动 TLS。您必须告诉 JavaMail 这样做,否则它将尝试不安全地进行。除非建立了 TLS 连接,否则 SMTP 服务器不会发送任何身份验证机制信息,因此 JavaMail 假设不需要身份验证并尝试在没有身份验证的情况下发送邮件。

因此,您似乎需要在此处明确指定您希望这也是加密连接。

Swift_SmtpTransport 构造函数的第三个参数执行此操作,提供“ssl”或“tls”:

new Swift_SmtpTransport('smtp.example.org', 587, 'ssl'); // or
new Swift_SmtpTransport('smtp.example.org', 587, 'tls');

使用 phpMailer 版本,您可以使用 $data->SMTPSecure 处理该部分。

【讨论】:

    猜你喜欢
    • 2015-07-29
    • 2011-08-08
    • 2021-10-14
    • 1970-01-01
    • 2021-12-20
    • 2015-05-28
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    相关资源
    最近更新 更多