PHPMailer为什么不能发送邮件

WBOY
Freigeben: 2016-08-08 09:33:18
Original
1469 Leute haben es durchsucht

PHPMailer 是一个很有用的 PHP 发送邮件的类。它支持使用 smtp 服务器发送邮件,同时支持 Sendmail, qmail, Postfix, Imail, Exchange, Mercury, Courier 等邮件服务器。SMTP服务器的话还支持验证,多SMTP发送(不过不太清楚有什么用).邮件发送可以包括多个TO, CC, BCC and REPLY-TO,支持text和HTML两种邮件格式,可以自动换行,支持各种格式的附件及图片,自定义邮件头等基本的邮件功能。
   由于 PHP 中只包含了一个 mail 函数,所以 PHPMailer 是对其很大的增强,相信是可以满足很多人的需求的,呵呵。其主要包括两个类文件:用于实现发送邮件功能的 class.phpmailer.php 和 smtp 实现的 class.smtp.php 。然后还有可以实现多种错误输出的文件,以及很详细的文档。

PHPMailer不能连接SMTP服务器,和修改SMTP大小写没有关系

(2011-10-22 12:17:35)

转载▼

标签:

php

phpmailer

杂谈

分类: 默认分类

PHPmailer无法发送邮件,提示错误Error: Could not connect to SMTP host

博客之前有两篇文章,《PHPMailer::不能连接SMTP服务器》《PHPMailer不能连接SMTP服务器的两种常见原因》
一为转载,一为笔记,结果误人子弟了,不是每个人能解决问题。
有朋友来信求助,我也着急。虽然后来解决了,但我还是不得要领,静下心来又看了看

PHPMailer不能连接SMTP服务器,究竟为什么?先用代码检测一下:


function Get_host($host){  //解析域名
$Get_host=gethostbyname($host);
echo "尝试连接 $host ...
\r\n ";
if(!$Get_host){
$str= "解析失败 (1)


";
}elseif($Get_host==$host){
$str= "解析失败 (2): 可能是一个无效的主机名
";
}else{
echo "域名解析为 $Get_host ...
\r\n";
Open_host($host);}
echo $str;
}

Function Open_host($host){  //连接主机

if(function_exists('fsockopen')){
$fp = fsockopen($host,25,&$errno,&$errstr,60);
  elseif(function_exists('pfsockopen')){
    echo "服务器不支持Fsockopen,尝试pFsockopen函数 ...
\r\n";
    $fp = pfsockopen($host,25,&$errno,&$errstr,60); }
  else
    exit('服务器不支持Fsockopen函数');

if(!$fp){
echo "代号:$errno,
\n错误原因:$errstr
";
}else{
echo "SMTP服务器连接ok!
\r\n";
fwrite($fp, "");
$out0= fgets($fp, 128);
#echo $out0;
if (strncmp($out0,"220",3)==0){ // 判断三位字符内容
echo '220 SMTP服务端响应正常
';
}else{
echo '服务器端错误
';}
}
}
//SMTP服务器地址
$site = array("smtp.163.com","smtp.sina.cn","smtp.sina.com","smtp.qqq.com","smtp.126.com");

//调运脚本
#$host="smtp.163.com";
#echo Get_host($host);


for ($i=0; $i {  
$host= $site[$i];
 echo Get_host($host);
}



PHPmailer是一个非常棒的PHP发送mail类,处理错误则侧重于和SMTP服务器会话过程中的问题,比如身份验证不对、收件人为空的错误提示,但是对于连接到smtp过程的错误提示以“Could not connect to SMTP host”一言蔽之,导致了很多问题没能解决,更可笑的是导致一些有用却讲不出道理的方法流传于世,可见,冥冥中一切皆有定数。

好了,不说口水话了。
想要搞清楚Could not connect to SMTP host的原因,自然要明白连接服务的步骤
一次完整有效的SMTP发信过程应该包括:解析域名、连接SMTP服务器、验证身份、确定收件人和信件内容、发送

上面那段PHP代码就是把这几个步骤分开来做,找出原因,然后寻找方法。回显的结果大概有如下几种:

1、解析失败 (2): 可能是一个无效的主机名
说明域名无法解析。可能是DNS级别的问题。联系管理员或者更换服务商

2、服务器不支持Fsockopen,尝试pFsockopen函数
如果用pfsockopen函数连接服务器成功了,则修改class.smtp.php 的$this->smtp_conn = fsockopen( 为$this->smtp_conn = pfsockopen( 。使PHPmailer恢复正常使用

3、服务器端错误
成功和远程主机建立连接,不过对方没有安装SMTP协议发送220响应码,说明SMTP服务器可能有问题

4、220 SMTP服务端响应正常

好吧,不论是fsockopen函数还是pfsockopen函数,已经和远程的SMTP服务器正常连接了。如果是无法用PHPmailer发信,我强烈建议你换一个账号重新试一下

5、其他报错,比如这样

Warning: fsockopen(): unable to connect to smtp163.com:25
你绝对有理由相信是防火墙搞的鬼! 这种情况下,如果不能联系管理员改防火墙规则 你可以试试《PHPMailer::不能连接SMTP服务器》中的方法,
搜索
function IsSMTP() {
$this->Mailer = 'smtp';
}

改成:
function IsSMTP() {
$this->Mailer = 'SMTP';
}

正如我标题所言“PHPMailer不能连接SMTP服务器,和修改SMTP大小写没有关系”。当然我不可能恶趣味的作弄你,而是有时候真的有效,治愈成功率多大就看你的人品了

来分析一下原因吧。
这句代码大概在class.phpmailer.php286行左右。这个函数在使用PHPmailer类时要首先调用,用以声明发送mail的方式

跟踪this->Mailer 到 class.smtp.php 400行左右 

switch($this->Mailer) {
      case 'sendmail':
        $result = $this->SendmailSend($header, $body);
        break;
      case 'smtp':
        $result = $this->SmtpSend($header, $body);
        break;
      case 'mail':
        $result = $this->MailSend($header, $body);
        break;
      default:
        $result = $this->MailSend($header, $body);
        break;

首先smtp绝对不等于SMTP!这一基本原则我居然都会忘掉。
所以,上面的条件都不满足 PHPmailer将执行 $result = $this->MailSend($header, $body);这句

再来跟踪MailSend()函数 在class.phpmailer.php 460行左右:

  function MailSend($header, $body) {
    $to = '';
    for($i = 0; $i to); $i++) {
      if($i != 0) { $to .= ', '; }
      $to .= $this->AddrFormat($this->to[$i]);
    }

    $toArr = split(',', $to);

    $params = sprintf("-oi -f %s", $this->Sender);
    if ($this->Sender != '' && strlen(ini_get('safe_mode'))        $old_from = ini_get('sendmail_from');
      ini_set('sendmail_from', $this->Sender);
      if ($this->SingleTo === true && count($toArr) > 1) {
        foreach ($toArr as $key => $val) {
          $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
        }
      } else {
        $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
      }
    } else {
      if ($this->SingleTo === true && count($toArr) > 1) {
        foreach ($toArr as $key => $val) {
          $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
        }
      } else {
        $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
      }
    }

    if (isset($old_from)) {
      ini_set('sendmail_from', $old_from);
    }

    if(!$rt) {
      $this->SetError($this->Lang('instantiate'));
      return false;
    }

    return true;
  }


注意$rt = @mail( 这是用PHP内置的mail函数发信啊!


来自W3School的mail发信实例

$to = "somebody@example.com"; //这里改成你的邮箱地址
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: dongfangtianyu@qq.com" . "\r\n" .
mail($to,$subject,$txt,$headers);
?>


如果在你的服务器上运行这脚本能够收到邮件,那么你完全可以用修改SMTP大小写的方法。不过,毕竟不大好用

 

以上就介绍了PHPMailer为什么不能发送邮件,包括了PHPMailer发送邮件方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!