skip to Main Content

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!

2

Answers


  1. Chosen as BEST ANSWER

    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!


  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search