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

9

这是我的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);
    */ 
}

这里是错误信息:
( ! ) 警告:fsockopen() [function.fsockopen]: 无法连接到ssl://smtp.gmail.com:465(无法找到套接字传输“ssl” - 配置PHP时是否忘记启用它?)位于C:\Program Files\wamp\www\swift-mailer\lib\classes\Swift\Transport\StreamBuffer.php的第233行
( ! ) 致命错误:未捕获异常'Swift_TransportException',消息为“无法与主机smtp.gmail.com建立连接[Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? #44551400]”,位于C:\Program Files\wamp\www\swift-mailer\lib\classes\Swift\Transport\StreamBuffer.php的第235行
( ! ) Swift_TransportException:无法与主机smtp.gmail.com建立连接[Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? #44551400] ,位于C:\Program Files\wamp\www\swift-mailer\lib\classes\Swift\Transport\StreamBuffer.php的第235行
问题出在哪里?
更新:
我检查了phpinfo(),它显示:
OpenSSL support     disabled (install ext/openssl) 

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

7个回答

16

我正在搜索一个类似的问题,发现你需要编辑php.ini文件。 编辑以下行:

;extension=php_openssl.dll

去掉分号,它就能正常工作。

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


3

2

在您的config.yml中,需要添加以下内容以使gmail正常工作:

swiftmailer: encryption: tls

或将您的代码中的: ->setEncryption('ssl') 替换为 ->setEncryption('tls')

请注意,不要使用ssl。


1

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

extension=php_openssl.dll

1
事实上,我建议在 25 端口上使用 TLS。使用以下语法进行测试:
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 25, 'tls')
    ->setUsername('007@gmail.com')
    ->setPassword('123');

0

希望你已经解决了你的问题,但对于我来说,这一行:

;extension=php_openssl.dll

在我的php.ini文件中不存在(在Win7上运行XAMPP 1.7.7),所以只需在扩展部分添加它,将分号删除即可。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接