2

我正在尝试使用该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

3 回答 3

9

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

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

至:

$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

并如评论中所述更改charesetcharset

编辑:

如果您发送 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');

在页面顶部,然后从那里获取,如果您仍然无法自行解决,请在此处发布,我会尽力帮助您。

于 2013-09-17T16:06:27.570 回答
5

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

$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);
于 2013-09-17T16:11:33.737 回答
-1

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

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

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

于 2015-03-19T14:26:37.237 回答