1

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.

SiL3NC3
  • 690
  • 6
  • 29

3 Answers3

1

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

Nick
  • 2,593
  • 3
  • 30
  • 59
  • I don't want to use gmail. I just want to catch the mail locally via Laragon mail catch folder... no need of sending mail to google. Why is it working with the PHP without using gmail?! – SiL3NC3 Feb 21 '22 at 20:57
0

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() );
    }
});
silver
  • 4,433
  • 1
  • 18
  • 30
  • When I change back to smtp with localhost I got an error: Swift_TransportException Process could not be started [The system cannot find the specified path. ] (config cache cleared) – SiL3NC3 Feb 22 '22 at 08:34
  • update your sendmail path to the correct path specified on your ```phpinfo()``` – silver Feb 22 '22 at 11:30
  • path is correct: https://i.postimg.cc/c424b4Zn/2022-02-22-13-41-03-PHP-7-4-23-phpinfo.png – SiL3NC3 Feb 22 '22 at 12:42
  • I mean what about the path on ```sendmail``` inside ```config/mail.php``` ? the error thrown is saying something about incorrect path – silver Feb 22 '22 at 12:45
  • also try using forward slashes on your path instead of backward – silver Feb 22 '22 at 12:47
  • also no luck, tinker is always hanging. also changed to a local separate installation folder of sendmail that I can tweak the config. I was using localhost and port 2525... only hanging. – SiL3NC3 Feb 22 '22 at 12:51
  • post your ```app/config/mail.php``` file – silver Feb 22 '22 at 12:53
  • https://pastebin.com/RwS3SRMu - I'm setting all via .env file and so this is the untouched generated version. There shown is the default path to sendmail. When I use this path, then I get an error, that executable is not being found (because of windows and linux paths are different) with a expected message. – SiL3NC3 Feb 22 '22 at 13:35
  • I've edited my answer, try creating a route to send email instead from tinker and check if any exception is captured – silver Feb 22 '22 at 13:59
  • I added your code in my project. When I try to send the mail the browser is now in loading state... (likely endless) - sadly no error output. Feeling like a timeout... – SiL3NC3 Feb 22 '22 at 14:07
  • ... still loading state. It seems that this request will not come to end (sendmail.exe is still running) – SiL3NC3 Feb 22 '22 at 14:18
  • After killing sendmail.exe it got this error in laravel: "Exception: fwrite(): write of 261 bytes failed with errno=22 Invalid argument" – SiL3NC3 Feb 22 '22 at 14:20
0

This worked for me on windows 11 laragon laravel

MAIL_MAILER=sendmail
MAIL_SENDMAIL_PATH='C:/laragon/bin/sendmail/sendmail.exe -t'

Make sure to get exact sendmail path from laragon by going to

laragon(Right click in laragon) > php > Mail Catcher > Get sendmail_path.

I don't have reputation to post comment.

gabber
  • 31
  • 3