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:
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:
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.