Search code examples
phplaravelsendmaillaravel-7laravel-mail

What's the correct configuration to send emails using Sendmail in Laravel?


I am using Laravel 7 and I want to send an email using the Sendemail driver via Laravel Mail facade. It worked when I used the PHP mail function but I want to use the Laravel Mail facade instead.

My .env file email configuration:

MAIL_DRIVER=sendmail
MAIL_SENDMAIL='/usr/sbin/sendmail -t -i'

My default mail setup in config/mail.php:

'default' => env('MAIL_MAILER', 'sendmail'),
'mailers' => [
    'smtp' => [
        'transport' => 'smtp',
        'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
        'port' => env('MAIL_PORT', 587),
        'encryption' => env('MAIL_ENCRYPTION', 'tls'),
        'username' => env('MAIL_USERNAME'),
        'password' => env('MAIL_PASSWORD'),
    ],
    'ses' => [
        'transport' => 'ses',
    ],
    'sendmail' => [
        'transport' => 'sendmail',
        'path' => '/usr/sbin/sendmail -bs',
    ],
    'log' => [
        'transport' => 'log',
        'channel' => env('MAIL_LOG_CHANNEL'),
    ],
    'array' => [
        'transport' => 'array',
    ],
],

I have created Mail class as explained in the docs. What is the right configuration to make it work?


Solution

  • First, change the default MAIL_MAILER to use Sendmail.

    MAIL_MAILER=sendmail
    

    Then in config/mail.php, update the Sendmail line.

    'sendmail' => [
        'transport' => 'sendmail',
        'path' => env('MAIL_SENDMAIL', '/usr/sbin/sendmail -bs')
    ],
    

    Finally, if you need to change the MAIL_SENDMAIL value then add this line to your .env.

    MAIL_SENDMAIL='/usr/sbin/sendmail -t -i'