SwiftMailer + Gmail - 无法发送电子邮件

时间:2015-12-14 20:56:45

标签: php gmail swiftmailer

我无法与Gmail SMTP服务器建立连接。

查找

$transport = Swift_SmtpTransport::newInstance()
            ->setHost('smtp.gmail.com')
            ->setPort(465)
            ->setEncryption('ssl')
            ->setUsername('email@gmail.com')
            ->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);

然后我明白了:

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

有人经历过这个吗?

3 个答案:

答案 0 :(得分:3)

首先使用您的帐户登录,然后在新标签页中将其打开

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

您需要确保使用的电子邮件ID能够启用安全性较低的应用。

答案 1 :(得分:1)

您可以使用tls而不是ssl证书发送邮件,如下所示:

    $transport =  Swift_SmtpTransport::newInstance('smtp.gmail.com', 587,'tls')
                            ->setUsername('email@gmail.com')
                            ->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);

答案 2 :(得分:-2)

Gmail已经改变了他的政策,所以你需要采取特殊的额外措施来实现这一目标,我可以在这里为你解释一下,但是你可以看看这个解释你需要采取的行动的答案:

Using php's swiftmailer with gmail