0

I want to send mails from different accounts in my Laravel application.

Therefore I created a new swiftmailer transport

// 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);

When I send the message directly from the Swift_Mailer object

// 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);

I get a mail that is actually send from my mail server:

enter image description here

However, when I send a mail from a Laravel Mailer (as explained here) using the same Swift_Mailer object from above:

$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);

then it appears in Gmail, that the mail wasn't really send from my mail server:

enter image description here

When I click on details it tells me that

The sender domain is different from the domain in the "From:" address.

I actually own both domains. In my .env file the FROM address is ****.com and I wanted to create a new mail from a ****.net address.

Why does Gmail think I have send a mail via my ****.com address if use the Laravel Mailer class?

Note: This is not a duplicate of multiple mail configurations - this is in fact where I started looking, but the 5 year old answer was not quite working and I gave this answer, but I am stuck with the above described problem.

Adam
  • 25,960
  • 22
  • 158
  • 247

1 Answers1

0

I solved it as follows:

I compared first the header of both mails, I realized that in the mail using the Mailer object I had a respond address with the ***.com domain.

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

Then I checked the Mailer class (with hindsight, I wonder why I didn't look here first..) and I found the following snippet that I added some month ago:

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

So I just had to remove it, now everything works.

In summary, I was convinded the problem was in the Mailer class, but it was simply inside the build method of the Mailable that I was using.

Adam
  • 25,960
  • 22
  • 158
  • 247