0

I'm trying to send an email when a user is subscribing in my web site.
To save me time ... I'm using Swift_Mailer with Symfony.

First, I tried to send a mail with the console :

php .\bin\console swiftmailer:email:send

And that's working, the mail was sent and the client received it.

So now I said to myself, I will be able to send emails with the code.
That's my Controller code :

public function register(Request $request, \Swift_Mailer $mailer)
{
    // Here saving new user in the database 

    $this->sendMail($user->getEMail(), $mailer);

    $content = $this->renderView("register.html.twig");
    return new Response($content);
}

private function sendMail($email, \Swift_Mailer $mailer)
{
    $message = (new \Swift_Message('Hello Email'))
        ->setFrom(['my.email@gmail.com' => "my name"])
        ->setTo($email)
        ->setBody('Here is the message itself');

    $mailer->send($message);
}

And That's my .env file :

MAILER_URL=gmail://my.email@gmail.com:myPasswordHere@localhost

When I'm trying to send the mail, no error, I tried this StackOverflow link, but no problem, echo "Successfull", but nothing in the Gmail send box and nothing my client email box.

Can you please help me to ... save me time ^^

------------------------------------------------------------- EDIT 1 ------------------------------------------------------------

I never edited the config/packages/swiftmailer.yaml file, that's it's content :

swiftmailer:
    url: '%env(MAILER_URL)%'
    spool: { type: 'memory' }

------------------------------------------------------------- EDIT 2 ------------------------------------------------------------

I also tryed to make a new transporter with this code :

$transport = (new \Swift_SmtpTransport('smtp.gmail.com', 465))
        ->setUsername('my.email@gmail.com')
        ->setPassword('myPasswordHere');
$mailer = new \Swift_Mailer($transport);

And that's the error I get : Connection could not be established with host smtp.gmail.com [php_network_getaddresses: getaddrinfo failed: Unknown host. #0]

------------------------------------------------------------- EDIT 3 ------------------------------------------------------------

I've got a swiftmailer.yaml fil in the config/packages/dev folder and that's it's default content :

# See https://symfony.com/doc/current/email/dev_environment.html
swiftmailer:
    # send all emails to a specific address
    #delivery_addresses: ['me@example.com']

I tried to put the same content as in the config/packages/swiftmailer.yaml, but no result.

WebIsOur
  • 21
  • 5
  • Have you configure swiftmailer ? If not, those lines in your yaml config files can help : `swiftmailer: url: '%env(MAILER_URL)%' spool: { type: 'memory' }` – titili Feb 15 '19 at 13:41
  • Thx @titili, look at my edit plz :) – WebIsOur Feb 15 '19 at 13:44
  • And in your dev config directory ? Do you have a swiftmailer.yaml file ? In my symfony project, I use it to redirect all the emails to a specific adress and have no problems. `swiftmailer: delivery_addresses: ['dev@example.com']` – titili Feb 15 '19 at 13:49
  • Sorry for this noob question but, where is the "dev config" repository plz ? :P – WebIsOur Feb 15 '19 at 13:51
  • Sorry, as you mention a swiftmailer file, I deduct you were using Symfony 4 in which config files are in `config/packages` directory. It contains `dev`, `prod`, `test` directories in which you can configure your application according to your environment. – titili Feb 15 '19 at 13:56
  • I'm using the Symfony 4 version, but I was talking about the config/package/swiftmailer.yaml file. Do I have to copy paste the cotent to the dev, prod and test content ? – WebIsOur Feb 15 '19 at 13:59
  • No, configuration in `config/packages` is common to each environment. I was guessing that dev environment maybe needs more configuration. But, if you don't have a `swiftmailer.yaml` in dev directory; this is probably not the problem. – titili Feb 15 '19 at 14:09
  • Please @titili have a look at my "Edit 3" – WebIsOur Feb 15 '19 at 14:14
  • Well, I suppose the problem may come from your `MAILER_URL`. Indeed, this environment variable is a shortcut for the code you described in your second edit and it seems that it does not work. You can try adding those lines in `config/packages/dev/web_profiler.yaml` : `web_profiler: intercept_redirects: true` in order to see if you can get more informations about the problem. – titili Feb 15 '19 at 14:33
  • Maybe take a look at your gmail account configuration also ! [Symfony - Gmail](https://symfony.com/doc/current/email.html#using-gmail-to-send-emails) – titili Feb 15 '19 at 14:44
  • My Gmail Account seems good, as I said, can send mail with the console. – WebIsOur Feb 15 '19 at 14:46

1 Answers1

-1

I don't see something like this in your code:

    // Create the Transport
    $transport = (new \Swift_SmtpTransport('mail hoster etc', port))
        ->setUsername('your email')
        ->setPassword('your password');
    // Create the Mailer using your created Transport
    $mailer = new \Swift_Mailer($transport);

Maybe it's a reason why your mails isn't sent.

Ivan Pruchai
  • 129
  • 2
  • 8
  • What makes you think that one should have such code? If sending mail works from the console, without having written such code, the problem might not be the same – Nico Haase Feb 15 '19 at 14:06