首页 > 解决方案 > 带有新 Swift_Mailer 的 Laravel Mailer 在 Gmail 中显示为错误的发件人域

问题描述

我想从我的 Laravel 应用程序中的不同帐户发送邮件。

因此我创建了一个新的swiftmailer 传输

// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.example.org', 25))
  ->setUsername('your username')
  ->setPassword('your password')
;

// Create the Mailer using your created Transport
$swift_mailer = new Swift_Mailer($transport);

当我直接从Swift_Mailer对象发送消息时

// Create a message
$message = (new Swift_Message('Wonderful Subject'))
  ->setFrom(['john@doe.com' => 'John Doe'])
  ->setTo(['receiver@domain.org', 'other@domain.org' => 'A name'])
  ->setBody('Here is the message itself')
  ;

// Send the message
$result = $swift_mailer->send($message);

我收到一封实际上是从我的邮件服务器发送的邮件:

在此处输入图像描述

但是,当我使用上面的相同对象从Laravel Mailer发送邮件时(如此处所述):Swift_Mailer

$view = app()->get('view');
$events = app()->get('events');
$mailer = new \Illuminate\Mail\Mailer($view, $swift_mailer, $events);
$mailer->alwaysFrom('john@doe.com', 'John Doe');
$mailer->alwaysReplyTo'john@doe.com', 'John Doe');

$mailer->to('my_email@test.com')->send(new \App\Mail\Test);

然后它出现在 Gmail 中,邮件并没有真正从我的邮件服务器发送:

在此处输入图像描述

当我点击详细信息时,它告诉我

发件人域与“发件人:”地址中的域不同。

我实际上拥有这两个域。在我的.env文件中,发件人地址是****.com,我想从一个****.net地址创建一个新邮件。

****.com如果使用 Laravel类,为什么 Gmail 会认为我已经通过我的地址发送了邮件Mailer

注意:这不是多个邮件配置的重复- 这实际上是我开始寻找的地方,但是 5 岁的答案不太有效,我给出了这个答案,但我遇到了上述问题。

标签: phplaravellaravel-5gmailswiftmailer

解决方案


我解决了如下:

我首先比较了两封邮件的标题,我意识到在使用该Mailer对象的邮件中,我有一个带有***.com域的响应地址。

spf=pass (google.com: domain of respond@****.com designates **** as
permitted sender) smtp.mailfrom=respond@****.com Return-Path: <respond@***.com>

然后我检查了 Mailer 类(事后看来,我想知道为什么我没有先看这里..),我发现了一个月前添加的以下代码段:

 $this->withSwiftMessage(function ($message) {
          $message->getHeaders()
                  ->addTextHeader('Return-Path', 'respond@****.net');
 });

所以我只需要删除它,现在一切正常。

总之,我确信问题出在Mailer课堂上,但它只是在Mailable我正在使用的 build 方法中。


推荐阅读