PHPMailer错误 - SMTP错误:数据不被接受

时间:2016-03-12 04:08:00

标签: php email smtp phpmailer

我已经填写了一份联系表格,显然我希望当用户在我的网站上填写此表格时收到确认电子邮件。

我正在使用PHPMailer。当我在我的计算机上测试代码作为localhost时,一切正常,并且正确接收确认消息。

这里是PHP脚本:

<?php

$Name= $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];

if ($Name=='' || $Email=='' || $Message==''){

echo "<script>alert('Please fill out mandatory fields');location.href ='javascript:history.back()';</script>";

}else{

require("archivosformulario/class.phpmailer.php");
$mail = new PHPMailer();

$mail->From     = $Email;
$mail->FromName = $Name; 
$mail->AddAddress("myemail@gmail.com");


$mail->WordWrap = 50; 
$mail->IsHTML(true);     
$mail->Subject  =  "Contact Form";
$mail->Body     =  "Name: $name \n<br />".    
"Email: $Email \n<br />".    
"Message: $Message\n<br />";


// SMTP Server

$mail->IsSMTP(); 
$mail->SMTPDebug  = 2;    
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true; 
$mail->Port = 465;
$mail->SMTPSecure = "ssl"; 
$mail->Username = "myemail@gmail.com"; 
$mail->Password = "mypassword"; 

if ($mail->Send())
echo "<script>alert('We have received your message. We will contact you shortly.');location.href ='javascript:history.back()';</script>";
else
echo "<script>alert('Error');location.href ='javascript:history.back()';</script>";

}

?>

问题是当我在网络托管(即123-reg.co.uk)上传这个PHP脚本时,它不起作用。

错误讯息:

SMTP Error: Data not accepted

这里是PHP脚本:

<?php

$Name= $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];

if ($Name=='' || $Email=='' || $Message==''){

echo "<script>alert('Please fill out mandatory fields');location.href ='javascript:history.back()';</script>";

}else{

require("archivosformulario/class.phpmailer.php");
$mail = new PHPMailer();

$mail->From     = $Email;
$mail->FromName = $Name; 
$mail->AddAddress("email@mydomainat123-reg.com");


$mail->WordWrap = 50; 
$mail->IsHTML(true);     
$mail->Subject  =  "Contact Form";
$mail->Body     =  "Name: $name \n<br />".    
"Email: $Email \n<br />".    
"Message: $Message\n<br />";


// SMTP Server

$mail->IsSMTP(); 
$mail->SMTPDebug  = 2;    
$mail->Host = "smtp.123-reg.co.uk";
$mail->SMTPAuth = true; 
$mail->Port = 25;
$mail->Username = "email@mydomainat123-reg.com"; 
$mail->Password = "password"; 

if ($mail->Send())
echo "<script>alert('We have received your message. We will contact you shortly.');location.href ='javascript:history.back()';</script>";
else
echo "<script>alert('Error');location.href ='javascript:history.back()';</script>";

}

?>

我尝试从另一个免费的网络托管上传和执行这个PHP脚本,但它无济于事。

我是PHP的新手,特别是在处理邮件功能方面。

可能是什么原因?任何额外的帮助将不胜感激。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

您的服务器不允许您配置不同的发件人和用户名:$mail->From,如$mail->Username

...而且

我大多数时候都看到过这封邮件,无论如何都要成功发送电子邮件,但并非总是如此。要进行调试,请设置:

$mail->SMTPDebug = true;

您可以回显调试消息或使用error_log()

记录它们
// 'echo' or 'error_log'
$mail->Debugoutput = 'echo';

特别是在负载很重的服务器上的可能候选者是SMTP超时:

// default is 10
$mail->Timeout = 60;

class.smtp.php还有一个Timelimit属性,用于从服务器读取!

希望这些信息有所帮助! :)