PHP mail()函数不发送电子邮件

PHP mail()函数不发送电子邮件,php,email,Php,Email,我正在尝试使用mail()PHP函数发送电子邮件。我让它工作,直到我试图给它一个“用户注册”的主题,然后邮件不发送 这是代码(已大大简化) $to=$this->post_数据['register-email']; $message='Hello etc'; $headers='来自:noreply@example.com' . “\r\n”; $headers.='内容类型:文本/html;chareset=iso-8859-1\r\n'; $headers.='From:Website';

我正在尝试使用
mail()
PHP
函数发送电子邮件。我让它工作,直到我试图给它一个“用户注册”的主题,然后邮件不发送

这是代码(已大大简化)

$to=$this->post_数据['register-email'];
$message='Hello etc';
$headers='来自:noreply@example.com' . “\r\n”;
$headers.='内容类型:文本/html;chareset=iso-8859-1\r\n';
$headers.='From:Website';
邮件($to,‘用户注册’,$message,$headers);
我还尝试使用一个包含文本字符串的变量,但没有成功

为什么我添加主题例外时它不发送邮件

谢谢

编辑:更新的代码仍然不起作用

$to = $this->post_data['register-email'];
$message = 'Hello etc';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: Website <admin@example.com>';
mail($to, 'User Registration', $message, $headers);
$to=$this->post_数据['register-email'];
$message='Hello etc';
$headers='MIME版本:1.0'。“\r\n”;
$headers.=“内容类型:text/html;字符集=iso-8859-1\r\n”;
$headers.='From:Website';
邮件($to,‘用户注册’,$message,$headers);

在您的第四行中,您正在使用
作为字符串处理其中的所有内容,所以请更改

$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
至:

$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
如注释中所述,将
chareset
更改为
charset

编辑:

$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
如果您发送的是txt/html邮件,您也可以根据邮件头中的mime设置来发送,那么请尝试以下方法

    $to = $this->post_data['register-email'];
    $message = 'Hello etc';

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= 'From: Website <admin@example.com>' . "\r\n";
    mail($to, 'User Registration', $message, $headers);

在页面顶部,然后从那里开始,如果您仍然无法自己解决问题,请将其发布到此处,我将尽力帮助您。

在您的第四行中,您使用的
'
将其内部的所有内容作为字符串处理,因此请更改

$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
至:

$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
如注释中所述,将
chareset
更改为
charset

编辑:

$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
如果您发送的是txt/html邮件,您也可以根据邮件头中的mime设置来发送,那么请尝试以下方法

    $to = $this->post_data['register-email'];
    $message = 'Hello etc';

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= 'From: Website <admin@example.com>' . "\r\n";
    mail($to, 'User Registration', $message, $headers);

在页面顶部,然后从那里开始,如果您仍然无法自己解决问题,请将其发布到此处,我将尽力帮助您。

我在我的大多数项目中使用此代码:

$subject = 'subject';
$message = 'message';
$to = 'user@gmail.com';
$type = 'plain'; // or HTML
$charset = 'utf-8';

$mail     = 'no-reply@'.str_replace('www.', '', $_SERVER['SERVER_NAME']);
$uniqid   = md5(uniqid(time()));
$headers  = 'From: '.$mail."\n";
$headers .= 'Reply-to: '.$mail."\n";
$headers .= 'Return-Path: '.$mail."\n";
$headers .= 'Message-ID: <'.$uniqid.'@'.$_SERVER['SERVER_NAME'].">\n";
$headers .= 'MIME-Version: 1.0'."\n";
$headers .= 'Date: '.gmdate('D, d M Y H:i:s', time())."\n";
$headers .= 'X-Priority: 3'."\n";
$headers .= 'X-MSMail-Priority: Normal'."\n";
$headers .= 'Content-Type: multipart/mixed;boundary="----------'.$uniqid.'"'."\n";
$headers .= '------------'.$uniqid."\n";
$headers .= 'Content-type: text/'.$type.';charset='.$charset.''."\n";
$headers .= 'Content-transfer-encoding: 7bit';

mail($to, $subject, $message, $headers);
$subject='subject';
$message='message';
$to$user@gmail.com';
$type='plain';//或HTML
$charset='utf-8';
$mail='no reply@'。str_replace('www.,'',$_SERVER['SERVER_NAME']);
$uniqid=md5(uniqid(time());
$headers='From:'.$mail.\n;
$headers.=“回复:”.$mail.\n”;
$headers.=“返回路径:”.$mail.\n”;

$headers.='消息ID:我在我的大多数项目中使用此代码:

$subject = 'subject';
$message = 'message';
$to = 'user@gmail.com';
$type = 'plain'; // or HTML
$charset = 'utf-8';

$mail     = 'no-reply@'.str_replace('www.', '', $_SERVER['SERVER_NAME']);
$uniqid   = md5(uniqid(time()));
$headers  = 'From: '.$mail."\n";
$headers .= 'Reply-to: '.$mail."\n";
$headers .= 'Return-Path: '.$mail."\n";
$headers .= 'Message-ID: <'.$uniqid.'@'.$_SERVER['SERVER_NAME'].">\n";
$headers .= 'MIME-Version: 1.0'."\n";
$headers .= 'Date: '.gmdate('D, d M Y H:i:s', time())."\n";
$headers .= 'X-Priority: 3'."\n";
$headers .= 'X-MSMail-Priority: Normal'."\n";
$headers .= 'Content-Type: multipart/mixed;boundary="----------'.$uniqid.'"'."\n";
$headers .= '------------'.$uniqid."\n";
$headers .= 'Content-type: text/'.$type.';charset='.$charset.''."\n";
$headers .= 'Content-transfer-encoding: 7bit';

mail($to, $subject, $message, $headers);
$subject='subject';
$message='message';
$to$user@gmail.com';
$type='plain';//或HTML
$charset='utf-8';
$mail='no reply@'。str_replace('www.,'',$_SERVER['SERVER_NAME']);
$uniqid=md5(uniqid(time());
$headers='From:'.$mail.\n;
$headers.=“回复:”.$mail.\n”;
$headers.=“返回路径:”.$mail.\n”;

$headers.='消息ID:我建议使用PHP\u EOL而不是\r\n或\n,因为换行符将由您的环境确定

$headers  = 'MIME-Version: 1.0' . PHP_EOL;

等等。。希望这能最终解决你的问题

我建议使用PHP\u EOL而不是\r\n或\n,因为换行符将由您的环境决定

$headers  = 'MIME-Version: 1.0' . PHP_EOL;


等等。。希望这能最终解决你的问题

这段代码是在Linux还是Windows上运行的?如果Windows上运行的是什么PHP版本?您也没有向我们展示如何添加“主题:”的话,您可以添加它吗?您有一个打字错误,是charset,而不是chareset@crm尝试使用这个
$to=$\u POST['register-email']
而不是
$to=$this->post_data['register-email']谢谢,但它仍然不起作用。这段代码是在Linux还是Windows上运行的?如果Windows上运行的是什么PHP版本?您也没有向我们展示如何添加“主题:”的话,您可以添加它吗?您有一个打字错误,是charset,而不是chareset@crm尝试使用这个
$to=$\u POST['register-email']
而不是
$to=$this->post_data['register-email']谢谢,但它仍然不起作用谢谢,我用仍然不起作用的新代码更新了我的问题。谢谢,我用仍然不起作用的新代码更新了我的问题。无论在什么环境下,标题都应该用分隔符分隔\r\n,请参阅此处的文档:。这最终取决于您的环境,如Linux PHP\u EOL=\n和Windows as\r\n(每个RFC的正确CRLF)“2.3.7行……此外,文本中的“裸”“CR”或“LF”字符的外观(即,没有其他字符)在使用邮件系统作为工具的邮件实现和应用程序中长期存在问题。“邮件客户端无法正确转换这些实例是怎么说的?”。。抱歉,我在linux上从来没有遇到过电子邮件问题,也从来没有因为\r\n这两个原因而出现过双线!但是PHP_EOL与电子邮件规范无关。根据我的经验,假设某个东西可能会转换成某个东西,如果不是现在的话,那么在将来就是一个bug的配方。你是对的,一个bug很容易由于缺乏理解而引入,但在这种情况下,我倾向于认为遵循RFC到bit有点夸张!最后,对第一篇文章最有价值的回应是实际使用一个库,如SwitMailer::MailTransport(顺便说一句,它使用了一个条件,如果不是在linux上,并且没有使用SMTP来实际替换\r\n在标题、正文和主题中的PHP_EOL:),这已经有一段时间了!无论在什么环境下,标题都应该用\r\n分隔,请参见此处的文档:。这最终取决于您的环境,如Linux PHP\u EOL=\n和Windows as\r\n(每个RFC的正确CRLF)“2.3.7行……此外,文本中的“裸”“CR”或“LF”字符的外观(即,没有其他字符)在使用邮件系统作为工具的邮件实现和应用程序中长期存在问题。“邮件客户端无法正确转换这些实例是怎么说的?”。。好吧,我从来没有