Symfony6 how to correctly configure sendmail to send emails from localhost

Viewed 48

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 = "receiver@mail.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: sender@mail.com";

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 = me@example.com

; 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 = "receiver@mail.com";
    $subject = "My subject";
    $txt = "Hello world!";
    $headers = "From: sender@mail.com";

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


    $subject = 'My subject';

    $email = (new TemplatedEmail());
    $email->from(new Address('sender@mail.com'));
    $email->to('receiver@mail.com');
    $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!

2 Answers

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!

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

Related