简体   繁体   English

SMTP 错误:无法连接到服务器:权限被拒绝 (13)

[英]SMTP ERROR: Failed to connect to server: Permission denied (13)

Good day!再会!

Im trying to make my phpmailer work in my shared hosting (freehostia.com) and I always get this error.我试图让我的 phpmailer 在我的共享主机(freehostia.com) ,但我总是收到此错误。 The username and password of my gmail is correct, and the rest of the settings is like this:我的gmail的用户名和密码是正确的,其余的设置是这样的:

$mail = new PHPMailer;

$mail->isSMTP();
$mail->SMTPDebug  = 2;
$mail->Host       = 'tls://smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth   = true; // Enable SMTP authentication
$mail->Username   = 'mymail@gmail.com'; // SMTP username
$mail->Password   = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port       = 587; // TCP port to connect to

$mail->setFrom('mymail@gmail.com', 'ASAPHOT Administrator'); // Add a recipient
$mail->addAddress('sorianorobertc@gmail.com'); // Name is optional
$mail->addReplyTo('mymail@gmail.com', 'ASAPHOT Administrator');

$mail->isHTML(true); // Set email format to HTML

$mail->Subject = 'it works';
$mail->Body    = 'it works';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if (!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

openssl is uncommented in the php.ini also. openssl 在php.ini也没有注释。 Am I missing something here?我在这里错过了什么吗? Thank you.谢谢你。

Complete error message:完整的错误信息:

Connection: opening to smtp.gmail.com:587, timeout=300, options=array ()
SMTP ERROR: Failed to connect to server: Permission denied (13)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Let's get to the answer first of all!我们先来揭晓答案! Try with the following command:尝试使用以下命令:

$ getsebool httpd_can_sendmail

if it shows: httpd_can_sendmail --> off如果显示: httpd_can_sendmail --> off

switch it on with this:用这个打开它:

$ sudo setsebool -P httpd_can_sendmail 1

then try sending the email again.然后尝试再次发送电子邮件。

This solution comes from this great page .这个解决方案来自这个很棒的页面

As it was pointed out from this article, you may also need to try sudo setsebool -P httpd_can_network_connect 1 .正如本文所指出的,您可能还需要尝试sudo setsebool -P httpd_can_network_connect 1 While for my CentOS 7 vm hosted by DigitalOcean, it's not necessary.而对于我由 DigitalOcean 托管的 CentOS 7 虚拟机,则没有必要。

The problem I met was failing to send out emails from a Drupal website, with the SMTP Authentication Support module, which relies on PHPMailer underneath.我遇到的问题是无法使用 SMTP 身份验证支持模块从 Drupal 网站发送电子邮件,该模块依赖于底层的 PHPMailer。 And the SMTP server used was Google.使用的 SMTP 服务器是 Google。

BTW, I had suspected it was OpenSSL certificate problem and did some test but no luck.顺便说一句,我怀疑这是 OpenSSL 证书问题并做了一些测试但没有运气。 So by setting the $SMTPDebug level to 2 from the PHPMailer source code, I was able to capture the "Permission denied (13)" error message.因此,通过将 PHPMailer 源代码中的$SMTPDebug级别设置为 2,我能够捕获“权限被拒绝 (13)”错误消息。

This suggests that fopen wrappers or socket functions are disabled in your PHP installation.这表明在您的 PHP 安装中禁用了 fopen 包装器或套接字函数。 Not unusual in shared hosting.在共享主机中并不罕见。 Running phpinfo() should tell you.运行phpinfo()应该会告诉你。

You can probably use $mail->isMail();您可能可以使用$mail->isMail(); and skip the auth to send via the ISP's mail server instead of SMTP, but beware of SPF issues.并跳过通过 ISP 的邮件服务器而不是 SMTP 发送的身份验证,但要注意 SPF 问题。

Thanks for this solutions, it helped me. 感谢您的解决方案,它对我有帮助。 I just moved my project to new server, while testing I found that mail functio 我只是将项目移到新服务器上,一边测试一边发现邮件功能

问题是 freehostia 的免费托管会阻止发出的邮件。

Freehostia blocks the list below in the free plan. Freehostia 在免费计划中阻止了以下列表。 (Chocolate) (巧克力)

-mailer -邮递员

-curl -卷曲

-soap get -肥皂得到

-xml get -xml 获取

this is important for http这对 http 很重要

  • getsebool httpd_can_sendmail is Off then turn On it sudo setsebool -P httpd_can_sendmail 1 getebool httpd_can_sendmail 关闭然后打开它 sudo setsebool -P httpd_can_sendmail 1
  • may need sudo 'setsebool -P httpd_can_network_connect 1'可能需要 sudo 'setsebool -P httpd_can_network_connect 1'
  • disable_function = ...... delete exec for exec() in php.ini disable_function = ...... 删除php.ini中exec()的exec
use this
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "ssl://smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port

if this not working than try bellow for php code.如果这不起作用,请尝试下面的 php 代码。

mail($to,$subject,$message);邮件($to,$subject,$message);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM