gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:24:12 25 4
gpt4 key购买 nike

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

我还尝试使用包含文本字符串的变量,但没有用。

为什么我添加主题异常(exception)后邮件没有发送?

谢谢

编辑:更新后的代码仍然无法正常工作

$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

编辑:

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

在页面顶部,然后从那里开始,如果您仍然不能自己解决它,请将其张贴在这里,我会尽力帮助您。

关于PHP mail() 函数不发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18854572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com