使用Swift Mailer、GMail和PHP发送电子邮件,权限被拒绝错误

使用Swift Mailer、GMail和PHP发送电子邮件,权限被拒绝错误,php,email,gmail,swiftmailer,Php,Email,Gmail,Swiftmailer,我下载了SwiftMailer 4.1.6,用于使用Gmail发送电子邮件。为此,我编写了以下代码 <?php require_once 'swiftmailer/lib/swift_required.php'; $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl") ->setUsername('jomit.jmt@gmail.com') ->s

我下载了SwiftMailer 4.1.6,用于使用Gmail发送电子邮件。为此,我编写了以下代码

<?php

require_once 'swiftmailer/lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
        ->setUsername('jomit.jmt@gmail.com')
        ->setPassword('***********');

$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
        ->setFrom(array('jomit.jmt@gmail.com' => 'Jomit Jose'))
        ->setTo(array('jomit.jmt@gmail.com' => '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);

可能出了什么问题

我们遇到了这个问题,原因是SELinux设置阻止Apache和PHP打开任何传出套接字连接。我们禁用了它,但忘记了
-P
参数,因此在下次重新启动时将其还原。如果您使用的是CentOS、RHEL或其他支持SELinux的发行版,这可能是问题的原因

可以使用以下命令禁用连接限制:

setsebool -P httpd_can_network_connect on

尝试使用443端口,而不是465@maxjackie哎呀!这也会导致相同的错误:
uncaughtexception'Swift_TransportException'带有消息“无法与主机smtp.gmail.com建立连接[权限被拒绝#13]”
您的PHP设置是否支持SSL?@lvaroG.Vicario
phpinfo()
显示启用了
OpenSSL支持
很好的解决方案。对我有效。在OrangeHRM安装中遇到此问题。我花了一段时间才找到这个隐藏的答案。非常感谢。经过漫长的一天的搜索,我找到了这篇文章,它解决了我的Flyspore不发送电子邮件的问题,谢谢:)我知道这很旧,但是如果你收到一条消息说“如果没有管理策略,就不能设置持久的布尔值”,那么试着运行“sudo setsebool-P httpd_can_network_connect on”,这让我走上了正确的方向。在我的PHP服务器上,我有自动禁止IP的传出连接。我必须进去,允许smtp.office365.com的每一个都通过。
setsebool -P httpd_can_network_connect on