2

I am working on a project that doesn't use any framework and I would like to use Symfony Mailer component to handle sending emails.

The installation part (composer require) was well handled and everything is included in my code without any error. However, I still have a problem : the documentation of the component seems to be written only for using it with the symfony framework. Indeed, it is refering to autoloaded config files that o bviously don't exist in my app.

This implementation seems to be very tricky and I was wondering if any of you guys already faced the same problem and what solution you came up with?

yivi
  • 42,438
  • 18
  • 116
  • 138
user2377141
  • 132
  • 12

1 Answers1

5

Your question made me wonder too how it is easy to send mail only with the mailer component.

So I created a new project from scratch and tried the simplest possible version following the mailer component documentation.


use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Email;

class MyMailer
{
    // googleDns format is gmail+smtp://USERNAME:PASSWORD@default
    public function __construct(private string $googleDsn)
    {
    }

    public function send()
    {
        $template = file_get_contents('https://raw.githubusercontent.com/leemunroe/responsive-html-email-template/master/email.html');

        $transport = Transport::fromDsn($this->googleDsn);
        $mailer = new Mailer($transport);

        $email = (new Email())
            ->from('mygmail@address.com')
            ->to('thedelivery@gmail.com')
            ->subject('Time for Symfony Mailer!')
            ->html($template);

        $mailer->send($email);
    }
}

And I successfully received my mail. I send my mail with gmail, for your information. Transport class should do the sending job for you, but if not you can have a look to inside vendor/symfony/mailer/Transport folder

Mcsky
  • 1,426
  • 1
  • 10
  • 20
  • 1
    Thanks a lot for your answer ! I fail to send the mails on uWamp but I can tell this would work if I would have a genuine SMTP server to use. I will try this on my development server. – user2377141 Sep 03 '21 at 13:03
  • Glad to help, happy coding! – Mcsky Sep 03 '21 at 13:37