Symfony Mailer trouble

462 Views Asked by At

The code does not work(. It gives me the error

<?php

error_reporting(E_ALL);
ini_set('display_errors', 'On');


require_once 'vendor/autoload.php';

use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;
$transport = Transport::fromDsn('smtp://[email protected]:[email protected]:587');

$mailer = new Mailer($transport);
$email = (new Email());
$email->from('[email protected]');

$email->to('[email protected]');

$email->subject('Some subject');
$email->text('test-message');

$mailer->send($email);

?>

Tried to 'require' files with these classes, but it did not help. Also I checked my composer.json. There was Symfony, so I`m sure, I have it installed with my composer.

1

There are 1 best solutions below

1
MRRaja On

The issue with the code is that the DSN (Data Source Name) specified for the Transport object is incorrect.

The correct DSN for Gmail should be
smtp://mymail%40gmail.com:[email protected]:587 (the email address in the username part of the DSN should be URL encoded). Additionally, it's not recommended to hard-code the password in the code as it can pose a security risk. Instead, the password should be stored in a secure environment variable.