SMTP: Change Message-ID domain in sending emails from Laravel 5.7 (Swift Mailer) SMTP: Change Message-ID domain in sending emails from Laravel 5.7 (Swift Mailer) laravel laravel

SMTP: Change Message-ID domain in sending emails from Laravel 5.7 (Swift Mailer)


  1. Edit the file config/mail.php and define your domain near the end:
    'domain' => 'yourdomain.com',
  1. In the command line, create a new listener:
    php artisan make:listener -e 'Illuminate\Mail\Events\MessageSending' MessageSendingListener
  1. Edit the newly created listener and make it look as follows (do NOT implement ShouldQueue):
    <?php    /**     * Set the domain part in the message-id generated by Swift Mailer     */    namespace App\Listeners;    use Illuminate\Mail\Events\MessageSending;    use Swift_Mime_IdGenerator;    class MessageSendingListener    {        /**         * Create the event listener.         *         * @return void         */        public function __construct()        {            //        }        /**         * Handle the event.         *         * @param  MessageSending  $event         * @return void         */        public function handle(MessageSending $event)        {            $event->message->setId((new Swift_Mime_IdGenerator(config('mail.domain')))->generateId());        }    }
  1. Register the listener in app/Providers/EventServiceProvider:
        protected $listen = [           // [...]            \Illuminate\Mail\Events\MessageSending::class => [                 \App\Listeners\MessageSendingListener::class,            ],         ];

That's it, enjoy! :)


Just found a correct way to change @swift.generated in message ID.

Add this code to your AppServiceProvider->boot() method:

\Swift_DependencyContainer::getInstance()        ->register('mime.idgenerator.idright')        ->asValue(config('mail.domain'));

config('mail.domain') is a custom config entry so you can change it to whatever you want.

Tested in Laravel 6, maybe will work with 5.* versions too.

Also you can find many interesting configs in this file:vendor/swiftmailer/swiftmailer/lib/dependency_maps/mime_deps.php