我下载了 SwiftMailer 4.1.6 以使用 Gmail 发送电子邮件。我为此目的编写了以下代码。
<?php
require_once 'swiftmailer/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('[email protected] /cdn-cgi/l/email-protection')
->setPassword('***********');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('[email protected] /cdn-cgi/l/email-protection' => 'Jomit Jose'))
->setTo(array('[email protected] /cdn-cgi/l/email-protection' => 'Jomit Jose'))
->setBody('This is the text of the mail send by Swift using SMTP transport.');
$numSent = $mailer->send($message);
printf("Sent %d messages\n", $numSent);
并导致以下错误:
Fatal error: Uncaught exception 'Swift_TransportException' with message
'Connection could not be established with host smtp.gmail.com
[Permission denied #13]' in
/home/jomit/public_html/email_test/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:266
Stack trace:
#0 /home/jomit/public_html/email_test/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php(66):
Swift_Transport_StreamBuffer->_establishSocketConnection()
#1 /home/jomit/public_html/email_test/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(117):
Swift_Transport_StreamBuffer->initialize(Array)
#2 /home/jomit/public_html/email_test/swiftmailer/lib/classes/Swift/Mailer.php(79):
Swift_Transport_AbstractSmtpTransport->start()
#3 /home/jomit/public_html/email_test/test.php(16):
Swift_Mailer->send(Object(Swift_Message))
#4 {main} thrown in /home/jomit/public_html/email_test/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php on line 266
可能出了什么问题?
我们遇到了这个问题,原因是 SELinux 设置阻止了 Apache 以及 PHP 打开任何传出套接字连接。我们已禁用它但忘记了-P
参数,以便在下次重新启动时恢复。如果您使用 CentOS、RHEL 或其他支持 SELinux 的发行版,这可能是导致问题的原因。
可以使用以下命令禁用连接限制:
setsebool -P httpd_can_network_connect on
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)