使用Swift Mailer,GMail和PHP发送电子邮件,权限被拒绝错误 [英] Sending email with Swift Mailer, GMail and PHP, Permission Denied Error

查看:416
本文介绍了使用Swift Mailer,GMail和PHP发送电子邮件,权限被拒绝错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了使用Gmail发送电子邮件的SwiftMailer 4.1.6。我已经为此写了以下代码。

I downloaded SwiftMailer 4.1.6 for sending email using Gmail. I had written the following code for that purpose.

<?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);

,导致以下错误:

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分发功能,这可能是导致此问题的原因。

We had this problem and the cause turned out to be a SELinux setting that prevents Apache and therefore PHP from opening any outgoing socket connections. We had disabled it but forgot the -P parameter so it was reverted in the next reboot. If you are using CentOS, RHEL or some other SELinux enabled distribution, this is a likely cause for the problem.

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

The connection restriction can be disabled with this command:

setsebool -P httpd_can_network_connect on

这篇关于使用Swift Mailer,GMail和PHP发送电子邮件,权限被拒绝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆