Laravel - Sending mail not working, tinker hanging when trying to use sendmail via Laragon on Windows

Viewed 541

In facts I found this question: Laragon and Laravel - sendmail not working ... but I decided to post a detailed new question, to get a response, that this issue is possibly a laravel bug... Thank you.

  • OS: Windows 10
  • Laravel Version: 8.75
  • PHP Version: 7.4.27
  • Database Driver & Version: sqlite

Description:

I trying around getting sendmail (Laragon) to work, but without luck. File '.env' got adjusted to use sendmail instead of smtp on a fresh laravel project.

Steps To Reproduce:

  • created new laravel project laravel new test
  • adjusted .env file:
MAIL_MAILER=sendmail
MAIL_SENDMAIL_PATH='C:\LARAGON\bin\sendmail\sendmail.exe -bs' 
  • run command: php artisan tinker
  • run tinker command: Mail::raw('Hello World!', function($msg) {$msg->to('mail@example.com')->subject('Test Email'); });

After running the tinker command, the cmd is hanging...

Also tried different options on the sendmail flags:

  • sendmail.exe -t -i <<< tinker hangs
  • sendmail.exe -t <<< tinker hangs
  • sendmail.exe -bs <<< tinker hangs

I can see that the sendmail.exe is running (in task manager), but seems not be able to finish (waited several minutes). Cancelation (CTRL-c) will close the tinker session and the sendmail.exe ist terminated.

With other php built-in mail function Laragon is working fine and also successfully catching the sent mails in the mail folder.

I always tried to clear configuration cache after every change of the .env file. php artisan config:clear

Sending mail via php works fine as expected with the following code:

<html>
   
   <head>
      <title>Sending HTML email using PHP</title>
   </head>
   
   <body>
      
      <?php
         $to = "xyz@somedomain.com";
         $subject = "This is subject";
         
         $message = "<b>This is HTML message.</b>";
         $message .= "<h1>This is headline.</h1>";
         
         $header = "From:abc@somedomain.com \r\n";
         $header .= "Cc:afgh@somedomain.com \r\n";
         $header .= "MIME-Version: 1.0\r\n";
         $header .= "Content-type: text/html\r\n";
         
         $retval = mail ($to,$subject,$message,$header);
         
         if( $retval == true ) {
            echo "Message sent successfully...";
         }else {
            echo "Message could not be sent...";
         }
      ?>
      
   </body>
</html>

Any help really appreciated.

2 Answers

Generally this happens when there is an authentication issue with sendmail.

Make sure you have setup sendmail with your gmail credentials, as suggested by the Laragon Documentation

Laragon Send Mail Settings

Additionally, gmail disables "less secure apps" by default. Make sure your account has less secure apps enabled here too (or even better, use an Application Specific Password): https://myaccount.google.com/lesssecureapps

output phpinfo() and check if you have the exact location define on sendmail_path

you also need to define it inside config/mail.php like

'sendmail' => [
    'transport' => 'sendmail',
    'path' => '/usr/sbin/sendmail -bs',
],

or pull the value from your .env file MAIL_SENDMAIL_PATH

'path' => env('MAIL_SENDMAIL_PATH')

Additionally, you may try using smtp driver with localhost info in your .env file, you can see these details in phpinfo() as well

MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=25
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=null

try creating a route to send the email or dump the error

Route::get('/test-mail', function () {
    try {
        $send = \Illuminate\Support\Facades\Mail::mailer('sendmail')->send([], [], function ($message)  {
            $message
                ->to('to@email.com')
                ->from('from@email.com', 'Test')
                ->subject( 'My Subject' )
                ->setBody('Test Content', 'text/html');
        });
        dd($send);
    } catch (\Exception $e )  {
        throw new \Exception( $e->getMessage() );
    }
});
Related