5
votes

Here's a description of my environment first, since it's a bit particular : I use a Windows 8.1 PC combined with a virtual machine running with Vagrant. It's basically a Debian 64bits machine. This VM is in fact my server.

Then, I use Symfony 2 for my project and I'm trying to use SwiftMailer with gmail (using my own gmail adress) in order to send emails thanks to a contact form (email address of the user, name of the user and content of the mail).

My action fills the email data with the ones given in the form and sends the mail. Btw, it checks if the data is valid by using the ->isValid() method.

My problem comes after I submit the form, I receive an Exception from SwiftMailer : Connection could not be established with host smtp.gmail.com [ #0]

Here's my config_dev.yml :

swiftmailer:
    transport: gmail
    username:  "%gmail_user%"
    password:  "%gmail_password%"
    delivery_address: "%gmail_user%"
    encryption: ssl
    host: smtp.gmail.com
    port: 465
    auth_mode: login

The "%gmail_user%" and stuff is defined in a parameters.yml file. Here's the content :

parameters:
mailer_transport: gmail
mailer_user: null
mailer_password: null
gmail_user: [email protected]
gmail_password: my_gmail_passwd

After a lot of search through the whole Internet, I've tried many things from here or from elsewhere, and nothing did work : Using tls, trying port 587, trying with the IP of the smtp.gmail.com server gave me the same error.

The SSL extension seems to be enabled too :

php --info | grep openssl
OpenSSL support => enabled

and some other stuff that tells the version of OpenSSL.

I checked my IMAP settings and the general setting that enables or disables applications to access to my gmail, it is enabled.

I also tried to telnet smtp.google.com 465 and it worked fine since I reached to connect.

I just don't know what to do now. I hope some of you had the same issue and can help me.

Have a nice day !

Edit : Problem solved and a new one appeared

After further investigation, I randomly tried to change the config_dev.yml file that way :

transport: mail

This solved my problem with the SwiftMailerException, though the mail doesn't come to the delivery_address email address even if I change it to another address I own (an hotmail one). I'll work on it, but I hope my solution can help.

Edit #2 : Tried to solve the sending problem but nothing worked.

This is going to be a book.. I tried to add a spool system by adding this in the config.yml :

spool: {type: memory }

The mail seem to be sent since the profiler tells me he sent it. I added this to my controller to send all the mails that could be spooled :

$spool = $transport->getSpool();
$sent = $spool->flushQueue($this->container->get('swiftmailer.transport.real'));

I also added this to check if it's really sent :

if ($this->mailer->send($message)) {
        echo '[SWIFTMAILER] sent email to ' . $toEmail;
    } else {
        echo '[SWIFTMAILER] not sending email: ' . $mailLogger->dump();
    }    

I am really lost and don't know what to do..

Here's the code that send the mail in the controller :

$message = \Swift_Message::newInstance()
        ->setSubject("The subject from the form")
        ->setFrom("The address from the form")
        ->setTo("The address which comes from an entity")
        ->setBody("The body from the form made with a twig template");

        $mailer = $this->container->get('mailer');

        $mailLogger = new \Swift_Plugins_Loggers_ArrayLogger();
        $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger));

        if ($mailer->send($message)) {
            echo 'sent email';
        } else {
            echo 'not sending email: ' . $mailLogger->dump();
        }

This always tells me 'sent email', but I never receive it.

3
Are you saying that using transport: mail allows you to send the email? That's a bit strange. Can you post the controller that creates and sends the message?George
Using transport: mail instead of transport: gmail solved the SwiftMailer exception. However I still don't receive the mail but I think it comes from the spool.AndoniLarz
you need to manually add the spool in your config spool: { type: memory }. try adding that and see if it worksGeorge
I added it and the mail still isn't received. I think I'll post another question since I looked to a lot of solutions that didn't work and I'm lost now..AndoniLarz
ok, the problem may be with your controller - can show show the code that sends the mail?George

3 Answers

1
votes

Did you try to make a connection with cURL? I had something similar with elasticsearch (elastica), apparently it used a cURL HTTP proxy I didn't know about because it was configured in php.ini.

off-topic: You can also try if mailcatcher works for your needs.

1
votes

I had the same error (Connection could not be established with host smtp.gmail.com [ #0]) and the reason was in antivirus (Avast!). When I disabled it error has gone.

1
votes

I found the solution and never answered here but since this question has like 2000+ views I feel like it's important.

So, everything was fine and the only thing I didn't know about was that during development (aka when in dev environment), swiftmailer seems to disable delivery, as told here : http://symfony.com/doc/current/email/dev_environment.html#disabling-sending

All I had to do was to set this in config_dev.yml and config_test.yml files :

swiftmailer:
    disable_delivery: false

Just be careful to which address your mails go. For example, on this project, it would have sent dummy mails to our clients which would have been quite problematic.

To prevent this, you can do what's told in the symfony documentation (http://symfony.com/doc/2.8/email/dev_environment.html#sending-to-a-specified-address-es)

# app/config/config_dev.yml
swiftmailer:
    delivery_addresses: ['[email protected]']