php PHPMailer中的SMTP连接()失败错误

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

SMTP connect() failed error in PHPMailer

phpemailsmtp

提问by Datta Dhonde

I am completely new to PHP and I want to send a mail using PHP. I have a Contact Us form whcih will accept email is of the person contacting me and therefore the mail will be sent to me. I am using PHPMailer library from https://github.com/PHPMailer/PHPMailer/tree/masterand following is the code snippet I am using.

我对 PHP 完全陌生,我想使用 PHP 发送邮件。我有一个联系我们表格,它会接受与我联系的人的电子邮件,因此邮件将发送给我。我正在使用https://github.com/PHPMailer/PHPMailer/tree/master 中的PHPMailer 库,以下是我正在使用的代码片段。

<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();  

$mail->SMTPSecure = 'tls';

$mail->Host     = "resolver1.opendns.com"; // this SMTP server of my machine
//$mail->Host     = "208.67.222.222";//ip ; which one to use the resolver1.opendns.com or 208.67.222.222 ???

$mail->From     = "[email protected];//email id of the person 

$mail->AddAddress("[email protected]");//my email id

$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;

if(!$mail->Send()) 
{
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
}  
else 
{
  echo 'Message has been sent.';
}
?> 

I am getting the error " Message was not sent.Mailer error: SMTP connect() failed." I am not getting what is the problem..? $mail->Host = ""; please comment on what this stands for??

我收到错误消息“邮件未发送。邮件程序错误:SMTP 连接()失败。” 我不明白是什么问题..?$mail->Host = ""; 请评论这代表什么?

回答by Mayank Sharma

Add $mail->SMTPDebug = 1;and try to debug the problem.

添加$mail->SMTPDebug = 1;并尝试调试问题。

回答by Datta Dhonde

As very well exampled by @joydesigner, To connect via SMTP, you will need to pass hostname, username and passwordand then it should connect and send email.

正如@joydesigner 所举的例子,要通过 SMTP 连接,您需要通过hostname, username and password,然后它应该连接并发送电子邮件。

$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // tls or ssl connection as req

Here I see you have passed only, hostinformation, pls add username & passwordas well and try once.

在这里我看到您只通过了,host信息,请添加username & password并尝试一次。

Also check that TLS/SSL PORTis open for your server:

还要检查TLS/SSL PORT您的服务器是否已打开:

check with:

检查:

telnet resolver1.opendns.com 25

回答by joydesigner

Maybe it is your configuration problem.

可能是你的配置问题。

example of phpmailer configuration is like this:

phpmailer 配置示例如下:

<?php
 require 'class.phpmailer.php';

 $mail = new PHPMailer;

 $mail->isSMTP();                                      // Set mailer to use SMTP
 $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
 $mail->SMTPAuth = true;                               // Enable SMTP authentication
 $mail->Username = 'jswan';                            // SMTP username
 $mail->Password = 'secret';                           // SMTP password
 $mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

 $mail->From = '[email protected]';
 $mail->FromName = 'Mailer';
 $mail->addAddress('[email protected]', 'Josh Adams');  // Add a recipient
 $mail->addAddress('[email protected]');               // Name is optional
 $mail->addReplyTo('[email protected]', 'Information');
 $mail->addCC('[email protected]');
 $mail->addBCC('[email protected]');

 $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
 $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
 $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
 $mail->isHTML(true);                                  // Set email format to HTML

 $mail->Subject = 'Here is the subject';
 $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
 $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;
  exit;
 }

 echo 'Message has been sent';

Here the $mail->Host is the smtp mail server. Normally started with smtp.

这里的 $mail->Host 是 smtp 邮件服务器。通常以 smtp 开始。

回答by Chair

You should check the tcp port 25 of the resolver1.opendns.com , it seens being block or not starting up the stmpd such as sendmail or some MTA.

您应该检查 resolver1.opendns.com 的 tcp 端口 25,它看到被阻止或未启动 stmpd,例如 sendmail 或某些 MTA。

try telnet resolver1.opendns.com 25

试试 telnet resolver1.opendns.com 25

and you will find tcp port 25 is not opened.

你会发现tcp 25端口没有打开。