使用 php、gmail 和 swiftmailer 发送电子邮件导致 SSL 相关错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4846599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending email using php, gmail, and swiftmailer causes SSL-related error
提问by LostLord
Here is my PHP code:
这是我的 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('[email protected]')
->setPassword('123')
;
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance('Test')
->setFrom(array('[email protected]' => 'From mr. 007'))
->setTo(array('[email protected]', '[email protected]' => '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);
*/
}
Here is the error:
这是错误:
( ! ) 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
( ! ) 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
( ! ) 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
( ! ) 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
Where is the problem??
问题出在哪儿??
Update:
更新:
I checked phpinfo()
and it says:
我查了一下phpinfo()
,上面写着:
OpenSSL support disabled (install ext/openssl)
I referred to the links below, but I couldn't install ssl...
我参考了下面的链接,但我无法安装 ssl...
采纳答案by keatch
Did your php support SSL ? http://php.net/manual/en/function.fsockopen.php, and check http://www.php.net/manual/en/openssl.installation.phpfor reference.
你的 php 支持 SSL 吗?http://php.net/manual/en/function.fsockopen.php,并检查http://www.php.net/manual/en/openssl.installation.php以供参考。
Create a page with
创建一个页面
phpinfo();
Is SSL enabled?
是否启用了 SSL?
回答by osos
i Was searching for a similar question and i found that you have to edit the php.ini file edit the following line
我正在寻找一个类似的问题,我发现你必须编辑 php.ini 文件编辑以下行
;extension=php_openssl.dll
remove the semi colon and it will work fine
删除分号,它会正常工作
Hope that help any one else :)
希望对其他人有帮助:)
回答by alainivars
gmail need this in your config.yml
gmail 在你的 config.yml 中需要这个
swiftmailer: encryption: tls
swiftmailer:加密:tls
or replace your: ->setEncryption('ssl') by ->setEncryption('tls')
或将您的:->setEncryption('ssl') 替换为 ->setEncryption('tls')
and not ssl
而不是 ssl
回答by Mimouni
in fact, I advice to use tls on port 25 test using the following syntax:
事实上,我建议使用以下语法在端口 25 测试上使用 tls:
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 25, 'tls')
->setUsername('[email protected]')
->setPassword('123');
回答by bekco
You should enable php_openssl module from php extensions. Just edit your php.inifile
您应该从 php 扩展中启用 php_openssl 模块。只需编辑您的php.ini文件
extension=php_openssl.dll
回答by Dador
You need to configure php to work with ssl
您需要配置 php 才能使用 ssl
回答by alchemication
I hope that you have solved your issue, however for me line:
我希望你已经解决了你的问题,但对我来说:
;extension=php_openssl.dll
did not exist in my php.ini (running XAMPP 1.7.7 on Win7), so just go ahead and add it in the extensions section, remove semicolon from it and it should work.
在我的 php.ini 中不存在(在 Win7 上运行 XAMPP 1.7.7),所以继续将它添加到扩展部分,从中删除分号,它应该可以工作。