Heim  >  Artikel  >  Backend-Entwicklung  >  PHPMailer出现Called Mail() without being connected错误

PHPMailer出现Called Mail() without being connected错误

WBOY
WBOYOriginal
2016-06-23 13:54:382174Durchsuche

用PHPMailer发送邮件,根据例子敲了如下代码

require("class.phpmailer.php"); //下载的文件必须放在该文件所在目录$mail = new PHPMailer(); //建立邮件发送类//$address = $_POST['address'];$mail->IsSMTP(); // 使用SMTP方式发送$mail->Host = "mail.qq.com";// 您的企业邮局域名$mail->Port = '25';//$mail->SMTPAuth = true; // 启用SMTP验证功能$mail->Username = "497045318@qq.com"; // 邮局用户名(请填写完整的email地址)$mail->Password = "******"; // 邮局密码$mail->From = "497045318@qq.com"; //邮件发送者email地址$mail->FromName = "Frank Tung";$mail->AddAddress("tongzhenhang@qq.com");//收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")//$mail->AddReplyTo("", "");//$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件//$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式$mail->Subject = "PHPMailer测试邮件"; //邮件标题$mail->Body = "Hello,这是测试邮件"; //邮件内容$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略if(!$mail->Send()){	echo "邮件发送失败. <p>";	echo "错误原因: " . $mail->ErrorInfo;	exit;}echo "邮件发送成功";

结果出现
邮件发送失败.

错误原因: The following From address failed: 497045318@qq.com : Called Mail() without being connected

我尝试把class.phpmailer.php中的
/**
   * Method to send mail: ("mail", "sendmail", or "smtp").
   * @var string
   */
  var $Mailer            = 'mail';

改成
  var $Mailer            = 'smtp';

此错误依旧。

我再去搜索QQ的smtp,网页如下
http://kf.qq.com/faq/120322fu63YV130422nqIrqu.html
将$mail->Host = "mail.qq.com";
改成
$mail->Host = "smtp.qq.com";
$mail->Port = '465';
还是报错。
改成
$mail->Port = '587';
报错信息:
邮件发送失败.
错误原因: The following From address failed: 497045318@qq.com : MAIL not accepted from server,530,Must issue a STARTTLS command first.
SMTP server error: Must issue a STARTTLS command first.
SMTP server error: Must issue a STARTTLS command first. 

我Youtube了一个教学视频
InMotion Hosting - How to use PHPMailer
https://www.youtube.com/watch?v=JltHLzkghmY
我的代码与他基本一样,与之唯一的区别是我在本地电脑localhost,而不是在服务器上(我还没个人的服务器)我想是不是与这个因素相关。

我想请教各位这是什么原因。十分感谢!如果还有什么好的方法/代码可以用php发送邮件,也请教各位了!


回复讨论(解决方案)

原因已经找到了。
因为当时在测试功能时候,由于一直各种跳错,我就去一行行排错。

//$mail->SMTPAuth = true; // 启用SMTP验证功能

在排错时候,我把上面一行注释掉了,最后忘了把注释去掉。
所以这一行不可或缺

Stellungnahme:
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