php SMTP 错误:无法连接到 SMTP 主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6209064/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:39:36  来源:igfitidea点击:

SMTP Error: Could not connect to SMTP host

phpsmtpphpmailer

提问by anvd

I have this code, and all works well in my local server. The email is sent without any problem.

我有这个代码,在我的本地服务器上一切正常。电子邮件发送没有任何问题。

But now I pass the content to webserver, and I get this error...

但是现在我将内容传递给网络服务器,并且出现此错误...

SMTP Error: Could not connect to SMTP host.

SSL is enable in the server..correct? so, what is the problem? enter image description here

服务器中启用了 SSL..正确吗?那么,有什么问题呢? 在此处输入图片说明

            $mail = new PHPMailer();
            $mail->IsSMTP();
            $mail->SMTPAuth   = true;                  // enable SMTP authentication
            $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
            $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
            $mail->Port       = 465;                   // set the SMTP port
            $mail->Username   = "dnteiro"; // GMAIL username
            $mail->Password   = "xxx";      // GMAIL password

回答by AJ.

It sounds like your web host is blocking outbound connections to smtp.gmail.com:465. Suggestions:

听起来您的网络主机正在阻止与 smtp.gmail.com:465 的出站连接。建议:

  1. Verify: If you have shell/terminal access to your web hosting server, try a telnet test to verify that they are in fact blocking this. Run telnet smtp.gmail.com 465

  2. Contact: Call or email your hosting provider and find out what SMTP server they provide for outbound relay. Make sure they know you want to use your @gmail.comaddress as the From/Reply-to address.

  3. Update code: Once your host provides you with a different mail server, update your code and try again.

  1. 验证:如果您可以通过 shell/终端访问您的网络托管服务器,请尝试 telnet 测试以验证它们实际上是否阻止了此. 跑telnet smtp.gmail.com 465

  2. 联系:致电或发送电子邮件给您的托管服务提供商,了解他们为出站中继提供的 SMTP 服务器。确保他们知道您想使用您的@gmail.com地址作为发件人/回复地址。

  3. 更新代码:一旦您的主机为您提供了不同的邮件服务器,请更新您的代码并重试。

If your web host doesn't allow outbound relay from its servers at all, then you need to look at switching hosts, if this is a requirement for your application.

如果您的网络主机根本不允许来自其服务器的出站中继,那么您需要查看切换主机,如果这是您的应用程序的要求。

回答by alfasin

AJ is right in regards to #2, it is specified in: http://support.google.com/mail/bin/answer.py?hl=en&answer=78775

AJ 就 #2 而言是正确的,具体在:http: //support.google.com/mail/bin/answer.py?hl=en&answer=78775

回答by Kannan Kannan

require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.abc.co.in';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->SMTPOptions = array(
    'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
    )
); 

$mail->Username = 'user name';
$mail->Password = 'password';

$mail->setFrom("from email id", "name");
$mail->addAddress("receipiant email id");

$mail->isHTML(true);
$bodycontent = 'body';
$mail->Body    = $bodycontent;

if (!$mail->send()) {
    echo "message has been not send", $mail->ErrorInfo;
    }
else{
    echo "message has been send";
    }