Ahmed Jerbi Ahmed Jerbi - 3 months ago 18
PHP Question

Enable to send mails with Swiftmailer

I'm using

Swiftmailer
with
Symfony 2.8
to send mails from a gmail account to an outlook account.
The code I'm using in my controller:

$message = \Swift_Message::newInstance()
->setSubject('Test Email')
->setFrom('xxxxx@xxxxx.com')
->setTo('xxxxxx@outlook.com')
->setBody(
$this->renderView(

'QualifBundle:Mails:Welcome.html.twig'
),
'text/html'
);
$this->get('mailer')->send($message);


Configuration file:

# Swiftmailer Configuration
swiftmailer:
transport: %mailer_transport%
encryption: %mailer_encryption%
auth_mode: %mailer_auth_mode%
host: %mailer_host%
username: %mailer_username%
password: %mailer_password%
spool: { type: memory }


Parameters yml:

mailer_username: xxxxxxx@gmail.com
mailer_password: xxxxxxxxx
mailer_transport: smtp
mailer_encryption: ssl
mailer_auth_mode: login
mailer_host: smtp.gmail.com


I managed to solve SSL errors, now with this code my app runs without exceptions but the mail is not received ( maybe not even been send )
How can I solve or fingerprint this ?

Update
I removed
spool:{ type: memory }
so that mails get send directly and I got this exception.

Connection could not be established with host 127.0.0.1 [Connection refused #111]


My Host is supposed to be
smtp.gmail.com
not the
localhost
any idea ?

Solution
I solved my problem by hacking into vendor's files, I know it's not the best solution but at least I can send mails.

look for
_establishSocketConnection()
fonction in
StreamBuffer.php
should be in this path
/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport
into the project.
And add those following lines to escape SSL verification:

//...
$options = array();
if (!empty($this->_params['sourceIp'])) {
$options['socket']['bindto'] = $this->_params['sourceIp'].':0';
}
//Add those two lines
$options['ssl']['verify_peer'] = FALSE;
$options['ssl']['verify_peer_name'] = FALSE;
//...


Then I restarted the browser I mailing works like a charm

Answer

Solution I solved my problem by hacking into vendor's files, I know it's not the best solution but at least I can send mails.

look for _establishSocketConnection() fonction in StreamBuffer.php should be in this path /vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport into the project. And add those following lines to escape SSL verification:

//...
$options = array();
if (!empty($this->_params['sourceIp'])) {
    $options['socket']['bindto'] = $this->_params['sourceIp'].':0';
}
//Add those two lines
$options['ssl']['verify_peer'] = FALSE;
$options['ssl']['verify_peer_name'] = FALSE;
//...

Then I restarted the browser I mailing works like a charm

Comments