0

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://mymail@gmail.com:MYPASS@smtp.gmail.com:587');

$mailer = new Mailer($transport);
$email = (new Email());
$email->from('mymail@gmail.com');

$email->to('mymail@gmail.com');

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

  • Welcome to SO. Seems you question is relative compact. I guess it can help if you explain a bit more. And tell us more on what you tried. My first bet would be you forgotten to run ‘composer install’ or didn’t require the symfony mailer package. – Danny Van Der Sluijs Feb 05 '23 at 12:33
  • @DannyVanDerSluijs, Thanks! I`ve changed my post, but, unfortunately, I have nothing to say else. I hope, info, which I added, will help somehow. – S1mplified Feb 05 '23 at 12:42
  • "There was Symfony" - what does that mean? Which packages did you install so far? – Nico Haase Feb 05 '23 at 12:53
  • @NicoHaase , I mean, when I open composer.json, I see this: `{ "require": { "symfony/mailer": "^6.2" } }` So, I have installed only Symfony. – S1mplified Feb 05 '23 at 12:57
  • `Symfony\Mailer\Transport` is not used in the code you've shared. Are you sure you're sharing the proper code? Also, importing `Symfony\Mailer\Mailer` makes no sense, as no such class exists. That namespace should be `Symfony\Component\Mailer\Mailer` – Nico Haase Feb 05 '23 at 13:00
  • @NicoHaase , Affirmative, my bad. Have changed it – S1mplified Feb 05 '23 at 13:05
  • @NicoHaase , Have tried to insert 'component', and it returns another error. I attached it upper. – S1mplified Feb 05 '23 at 13:09
  • "it returns another error" - why not share it? Usually, such error messages are given in plain text, and thus they are shared best **in plain text** – Nico Haase Feb 06 '23 at 07:17
  • @NicoHaase , Fatal error: Uncaught Symfony\Component\Mailer\Exception\TransportException: Expected response code "250" but got code "530", with message "530 5.7.0 Must issue a STARTTLS command first. j18-20020a19f512000000b004d4ead86cb2sm961239lfb.20 - gsmtp". in D:\MAMP\htdocs\project\project\Coursework\email\vendor\symfony\mailer\Transport\Smtp\SmtpTransport.php:337. It returns me this error. – S1mplified Feb 07 '23 at 08:53
  • As usual: please add all clarification to your question by editing it. Also, share your attempts to resolve the problem - you're not the first one that sees this error message – Nico Haase Feb 07 '23 at 08:56

1 Answers1

0

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:MYPASS@smtp.gmail.com: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.

MRRaja
  • 1,073
  • 12
  • 25