Home > Net >  Symfony6 how to correctly configure sendmail to send emails from localhost
Symfony6 how to correctly configure sendmail to send emails from localhost

Time:08-19

Basically on my home Ubuntu machine I want to be able to send emails in my symfony applications from localhost. I have installed sendmail with its basic configuration. I can send an email using the command line: sendmail -v command. The email is received. I can even use the PHP function to send the email:

$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]";

mail($to,$subject,$txt,$headers

The email is received.

However, using the symfony mail functionality I am not getting the emails and I am not receiving any errors either.

This is my sendmail configuration in php.ini:

[mail function]
; For Win32 only.
; https://php.net/smtp
SMTP = localhost
; https://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; https://php.net/sendmail-from
;sendmail_from = [email protected]

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; https://php.net/sendmail-path
sendmail_path = /usr/sbin/sendmail -t -i

This is my symfony php code:

public function resetPassword(User $user, ResetPasswordToken $resetToken): void
{

    $to = "[email protected]";
    $subject = "My subject";
    $txt = "Hello world!";
    $headers = "From: [email protected]";

    mail($to,$subject,$txt,$headers); // <----- THIS IS WORKING!!!


    $subject = 'My subject';

    $email = (new TemplatedEmail());
    $email->from(new Address('[email protected]'));
    $email->to('[email protected]');
    $email->subject($subject);
    $email->htmlTemplate('shop/email/reset_password.html.twig');
    $email->context([
        'resetToken' => $resetToken
    ]);

    try {
        $this->mailer->send($email); // <----- THIS IS NOT WORKING!!!
    } catch (TransportExceptionInterface $e) {
        dd($e->getCode());
    }
}

In my .env file I have tried multiple different configurations including:

###> symfony/mailer ###
MAILER_DSN=smtp://localhost
###< symfony/mailer ###

###> symfony/mailer ###
MAILER_DSN=sendmail://default
###< symfony/mailer ###

###> symfony/mailer ###
MAILER_DSN=native://default
###< symfony/mailer ###

None of them worked. Any ideas are appreciated!

CodePudding user response:

debug the $this->mailer e.g. first with dd() and if it doesnt help... do actually proper debug.

as well, keep in mind that sometimes clearing cache can help

php bin/console cache:clear

CodePudding user response:

So appareantly everything was fine. The problem was that if you install symfony with their installer using the --webapp option, the queue messenger is installed: https://symfony.com/doc/current/messenger.html and all my emails where in queue. Removing the messenger fixed my problems. Thanks!

  • Related