phpsymfonysymfony-mailer

How to debug Symfony Mailer delivery problems?


I am working on my first Symfony 5 project and struggle to send mail using the build in MailerInterface. I have worked with Swift Mailer in Symfony 3 before and never had similar issues before.

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Mailer\MailerInterface;
...

class SomeController extends AbstractController {
    public someAction(Request $request, MailerInterface $mailer) {
        ...
        $email = (new TemplatedEmail())
            ->from(new Address('address@example.com', 'My Symfony Mail'))
            //->to($user->getEmail())
            ->to('receiver@example.com')
            ->subject('Subject')
            ->htmlTemplate('email.html.twig');
    
        $mailer->send($email);
    }
}


// .env
#MAILER_DSN=smtp://user:pass@smtp.example.com:25
MAILER_DSN=sendmail://default

If MAILER_DSN has some malformed format an exception is thrown and shown on the Symfony debugger page, e.g. The "invaliddsn" mailer DSN must contain a scheme... Thus it seems that the configured DSN smtp://user:pass@smtp.example.com:25 is correct. Using the same credentials, host and port in other mail applications is no problem.

However, when using this code no error/exception is shown and I receive no mail at all. Of course I have double checked the logs (no errors), the receivers spam folder (nothing). Specifying an SMTP server or sendmail does not make any difference.

The Symfony docs only explain how to handle exceptions but in my case no exceptions are thrown.

While there are a lot of other questions about mailing issues in Symfony, most of them deal with the older Swift Mailer or other, specific problems.

How can I figure out if a mail is send and not received or not send at all?


Solution

  • If it's not throwing an exception, you can assume the transport did not raise an error condition.

    From the docs:

    Handling Sending Failures

    Symfony Mailer considers that sending was successful when your transport (SMTP server or third-party provider) accepts the mail for further delivery. The message can later be lost or not delivered because of some problem in your provider, but that’s out of reach for your Symfony application.

    If there’s an error when handing over the email to your transport, Symfony throws a Symfony\Component\Mailer\Exception\TransportExceptionInterface. Catch that exception to recover from the error or to display some message

    You can also check the object that send() returns:

    The Symfony\Component\Mailer\SentMessage object returned by the send() method of the Symfony\Component\Mailer\Transport\TransportInterface provides access to the original message (getOriginalMessage()) and to some debug information (getDebug()) such as the HTTP calls done by the HTTP transports, which is useful to debug errors.

    Finally, if you have the Symfony Web Profiler, since you are doing the sending during a Web Request, you can check the profiler's output for information about the mail sending attempt.