在PHP中发送邮件失败

时间:2013-03-15 04:26:45

标签: php

我正在尝试使用php中的邮件功能发送电子邮件:

$subject = 'testing';
$email = 'test@gmail.com';
$message = 'test message';          
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: The test site" . "\r\n";

if (mail($email, $subject, $message, $headers)) {
  $data['msg']="Message send successfully";
} 
else {
  $data['msg']="Please try again, Message could not be sent!";
}  

我遇到以下错误:

A PHP Error was encountered

Severity: Warning

Message: mail() [function.mail]: SMTP server response: 501 Syntax error in parameters or arguments

Filename: sendemail.php

Line Number: 40

我可能猜错误是由于没有设置在php中发送电子邮件所需的配置。我应该做什么或者我必须在php.ini文件中进行更改,但它不可访问。有什么解决方案吗?

4 个答案:

答案 0 :(得分:2)

$subject = 'testing';
$email = 'test@gmail.com';
$message = 'test message';          
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: The test site" . "\r\n";


$to=$toEmail;
$subject=$sub;
$from="info@mypropick.com"; 
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: <".$from.">\n";
$headers .= "X-Priority: 1\n";
$message='<div style=" width:700px; margin:0 auto; border:1px solid #e2e2e2; padding:20px;">
<h3>MYPROPICK Services:</h3>'.$msg.'</div>';
$message .= "<br/>Regards <br />MYPROPICK.COM";


if (mail($to, $subject, $message, $headers )) {
  $data['msg']="Message send successfully";
} 
else {
  $data['msg']="Please try again, Message could not be sent!";
}  

答案 1 :(得分:0)

第三行错过了引号“'”

$message = 'test message' ;   
                        ^

尝试:

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: valid@email.com" . "\r\n";

'From'必须是有效的电子邮件地址

答案 2 :(得分:0)

您忘记了代码中的结束撇号

$message = 'test message;

应该是

$message = 'test message'; 

答案 3 :(得分:0)

SMTP错误501:参数或参数中的语法错误(例如,无效的电子邮件地址)

  

命令正确且已识别,但参数(参数,例如电子邮件地址)无效。例如,您尝试将无效的电子邮件地址用作sender\@domain.com,并且电子邮件地址中不允许使用"\"

http://info.webtoolhub.com/kb-a15-smtp-status-codes-smtp-error-codes-smtp-reply-codes.aspx