[英]PHP mail() function not sending email
我正在尝试使用mail()
PHP
函数发送电子邮件。 我一直在工作,直到我试图给它一个“用户注册”的主题,然后邮件没有发送!
这是代码(已经大大简化了)
$to = $this->post_data['register-email'];
$message = 'Hello etc';
$headers = 'From: noreply@example.com' . "\r\n" ;
$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
$headers .= 'From: Website <admin@example.com>';
mail($to, 'User Registration', $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);
在您的第 4 行,您使用'
将其中的所有内容作为字符串处理,因此请更改
$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
到:
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
并且在评论中提到改变chareset
到charset
编辑:
如果您发送 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);
如果它仍然不起作用,您可以尝试调试您的代码,只需添加
error_reporting(E_ALL);
ini_set('display_errors', '1');
在页面顶部,然后从那里取出,如果您仍然无法自己解决,请将其张贴在这里,我会尽力帮助您。
我在我的大部分项目中使用此代码:
$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);
我建议使用 PHP_EOL 而不是 \\r\\n 或 \\n 因为换行符将由您的环境决定......
$headers = 'MIME-Version: 1.0' . PHP_EOL;
等等...希望这可能最终解决您的问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.