使用 php、gmail 和 swiftmailer 发送电子邮件会导致与 SSL 相关的错误

Posted

技术标签:

【中文标题】使用 php、gmail 和 swiftmailer 发送电子邮件会导致与 SSL 相关的错误【英文标题】:Sending email using php, gmail, and swiftmailer causes SSL-related error 【发布时间】:2011-06-18 07:27:43 【问题描述】:

这是我的 php 代码:

function SendCookieToTheMail()

    require_once 'swift-mailer/lib/swift_required.php';
    //Create the Transport
    $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com')
      ->setPort(465)
      ->setEncryption('ssl')
      ->setUsername('007@gmail.com')
      ->setPassword('123')
      ;

    //Create the Mailer using your created Transport
    $mailer = Swift_Mailer::newInstance($transport);

    //Create a message
    $message = Swift_Message::newInstance('Test')
      ->setFrom(array('007@gmail.com' => 'From mr. 007'))
      ->setTo(array('007@gmail.com', '007@gmail.com' => 'To mr. 007'))
      ->setBody('Body')
      ;

    //Send the message
    $result = $mailer->send($message);

    /*
    You can alternatively use batchSend() to send the message

    $result = $mailer->batchSend($message);
    */ 

这是错误:

( ! ) Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) in C:\Program Files\wamp\www\swift-mailer\lib\classes\Swift\Transport\StreamBuffer.php on line 233

( ! ) Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? #44551400]' in C:\Program Files\wamp\www\swift-mailer\lib\classes\Swift\Transport\StreamBuffer.php on line 235

( ! ) Swift_TransportException: Connection could not be established with host smtp.gmail.com [Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? #44551400] in C:\Program Files\wamp\www\swift-mailer\lib\classes\Swift\Transport\StreamBuffer.php on line 235

问题出在哪里??

更新:

我检查了phpinfo(),它说:

OpenSSL support     disabled (install ext/openssl) 

我参考了下面的链接,但我无法安装 ssl...

【问题讨论】:

【参考方案1】:

事实上,我建议在端口 25 上使用 tls 使用以下语法进行测试:

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 25, 'tls')
    ->setUsername('007@gmail.com')
    ->setPassword('123');

【讨论】:

【参考方案2】:

gmail 在你的 config.yml 中需要这个

快速邮件: 加密:tls

或替换您的: ->setEncryption('ssl') 经过 ->setEncryption('tls')

而不是 ssl

【讨论】:

【参考方案3】:

您应该从 php 扩展中启用 php_openssl 模块。只需编辑您的 php.ini 文件

extension=php_openssl.dll

【讨论】:

【参考方案4】:

我希望你已经解决了你的问题,但是对我来说:

;extension=php_openssl.dll

在我的 php.ini 中不存在(在 Win7 上运行 XAMPP 1.7.7), 所以只需继续将它添加到扩展部分,从中删除分号,它应该可以工作。

【讨论】:

【参考方案5】:

我正在寻找一个类似的问题,我发现你必须编辑 php.ini 文件 编辑以下行

;extension=php_openssl.dll

去掉分号就可以了

希望对其他人有所帮助:)

【讨论】:

【参考方案6】:

您需要配置 php 以使用 ssl

http://www.php.net/manual/en/openssl.installation.php

【讨论】:

【参考方案7】:

你的 php 支持 SSL 吗? http://php.net/manual/en/function.fsockopen.php,并查看http://www.php.net/manual/en/openssl.installation.php 以供参考。

创建一个页面
phpinfo();

是否启用了 SSL?

【讨论】:

以上是关于使用 php、gmail 和 swiftmailer 发送电子邮件会导致与 SSL 相关的错误的主要内容,如果未能解决你的问题,请参考以下文章

尝试使用 swift mailer、gmail smtp、php 发送邮件

绕过 Gmail 的垃圾邮件过滤器(使用 PHP 从共享主机发送的邮件)

为啥 swiftmailer 不采用环境变量?

通过 Gmail API 发送电子邮件时应用 MSO 条件语句

PHP - 使用 STARTTLS 和自签名证书的 Swiftmailer

如何使用swiftmailer-bundle在symfony 4中设置多个邮件