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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18854572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:19:31  来源:igfitidea点击:

PHP mail() function not sending email

phpemail

提问by crmepham

I am attempting to send an email using the mail()PHPfunction. I had it working until I attempted to give it a subject of "User registration", then the mail is not sent!

我正在尝试使用该mail()PHP功能发送电子邮件。我一直在工作,直到我试图给它一个“用户注册”的主题,然后邮件没有发送!

Heres the code (have simplified it greatly)

这是代码(已经大大简化了)

$to = $this->post_data['register-email'];
$message = 'Hello etc';
$headers = 'From: [email protected]' . "\r\n" ;
$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
$headers .= 'From: Website <[email protected]>';
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?

为什么添加主题异常时不发送邮件?

Thanks

谢谢

EDIT: 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 <[email protected]>';
mail($to, 'User Registration', $message, $headers);

回答by Breezer

On your 4th line you're using 'that handles everything inside of it as a string so change

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

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

To:

到:

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

and as mentioned in the comments change charesetto charset

并如评论中所述更改charesetcharset

Edit:

编辑:

if your sending a txt/html mail you have according to documentationto set mime in the headers too so try this

如果您发送 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 <[email protected]>' . "\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.

在页面顶部,然后从那里取出,如果您仍然无法自己解决,请将其张贴在这里,我会尽力帮助您。

回答by Yevgen

I'm using this code on most of my projects:

我在我的大部分项目中使用此代码:

$subject = 'subject';
$message = 'message';
$to = '[email protected]';
$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);

回答by MediaVince

I would suggest to use PHP_EOL instead of \r\n or \n as the line break would then be determined by your environment...

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

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

etc.. hoping this might finally solve your problem!

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