Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to send emails with Symfony 2 using SwiftMailer : Connection could not be established with host smtp.gmail.com [ #0]

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.

like image 691
AndoniLarz Avatar asked Oct 19 '22 13:10

AndoniLarz


1 Answers

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.

like image 150
dsbaars Avatar answered Oct 27 '22 11:10

dsbaars