SwiftMailer + Gmail - Cannot send email
Asked Answered
E

3

2

I can't connect with Gmail SMTP server.

Look:

$transport = Swift_SmtpTransport::newInstance()
            ->setHost('smtp.gmail.com')
            ->setPort(465)
            ->setEncryption('ssl')
            ->setUsername('[email protected]')
            ->setPassword('mypasss');

    $mailer = Swift_Mailer::newInstance($transport);

    $message = Swift_Message::newInstance('Contato via Site')
            ->setFrom(array($email => $de))
            ->setTo(array($destinatario => 'Agência Linka'))
            ->setBody($corpo_mensagem, 'text/html')
            ->setCharset('UTF-8');

    $mailer->send($message);

And then I get this:

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [ #0]' in ...

Anyone has experienced this?

Everard answered 14/12, 2015 at 20:56 Comment(0)
C
5

First login with your account and open this in new tab,

https://www.google.com/settings/u/1/security/lesssecureapps
https://accounts.google.com/b/0/DisplayUnlockCaptcha
https://security.google.com/settings/security/activity?hl=en&pli=1

you need to make sure your using email id has enable for less secure apps.

Candlestick answered 21/10, 2017 at 15:13 Comment(0)
N
1

You can send mail by using tls instead of ssl certificate like below:

    $transport =  Swift_SmtpTransport::newInstance('smtp.gmail.com', 587,'tls')
                            ->setUsername('[email protected]')
                            ->setPassword('mypasss');

    $mailer = Swift_Mailer::newInstance($transport);

    $message = Swift_Message::newInstance('Contato via Site')
            ->setFrom(array($email => $de))
            ->setTo(array($destinatario => 'Agência Linka'))
            ->setBody($corpo_mensagem, 'text/html')
            ->setCharset('UTF-8');

    $mailer->send($message);
Normanormal answered 7/3, 2016 at 5:53 Comment(0)
N
-1

Gmail has changed his policy so yuo will need to take special additional steps to make this working, I could explain it here for you but You can have a look at this answer which explains the action you need to take:

Using php's swiftmailer with gmail

Nkrumah answered 14/12, 2015 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.