PHP mail()函数不发送电子邮件 [英] PHP mail() function not sending email
问题描述
我正在尝试使用 mail()
PHP
函数发送电子邮件。我一直工作,直到我试图给它一个用户注册的主题,然后邮件不发送!
I am attempting to send an email using the mail()
PHP
function. I had it working until I attempted to give it a subject of "User registration", then the mail is not sent!
他的代码(大大简化了) / p>
Heres the code (have simplified it greatly)
$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);
我还尝试使用一个包含文本字符串但不起作用的变量。
I also attempted to use a variable containing the string of text but that didnt work.
为什么我添加主题异常时不发送邮件?
Why is it not sending the mail when I add the subject exception?
谢谢
编辑:更新后的代码仍然不起作用
updated code thats still not working
$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);
推荐答案
在第四行,你使用'
将其中的所有内容作为字符串处理,以便更改
On your 4th line you're using '
that handles everything inside of it as a string so change
$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
至:
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
正如评论中提到的更改 chareset
到 charset
and as mentioned in the comments change chareset
to charset
修改:
如果您发送的txt / html邮件符合文档在标题中设置mime,所以尝试这个
if your sending a txt/html mail you have according to documentation to set mime in the headers too so try this
$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);
如果仍然不起作用,您可以尝试调试代码,只需添加
If it still doesn't work you could try to debugg your code, simply add
error_reporting(E_ALL);
ini_set('display_errors', '1');
,如果仍然无法解决通过你自己发布在这里,我会尽力帮助你。
on top of the page, and take it from there, and if you still can't solve it by yourself post it here and I'll do my best to help ya out.
这篇关于PHP mail()函数不发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!