PHP邮件无法发送但没有错误

3

我有一个发送电子邮件的脚本,在我的VPS上的一个域名上可以正常工作,但在另一个域名上却无法工作,这非常令人困惑!我检查了所有smtp详细信息等等,但找不到任何原因。脚本运行时似乎是成功的,没有出现错误,并且变量都已就位。

如果有人有任何想法,将不胜感激!

<?php
$email=$_POST['email'];
$name=$_POST['name'];
$business=$_POST['business'];
$town=$_POST['town'];
$phone=$_POST['phone'];
$rpo=str_replace("rpo", "RPO",$_POST['rpo']);
$retained=str_replace("retained", "Retained",$_POST['retained']);
$ats=str_replace("ats", "ATS",$_POST['ats']);

$interest=$rpo . ' ' . $retained . ' ' . $ats;
$interest=str_replace('','<br>',$interest);

$msg = "You've received a new enquiry from $name at $business<br>
They are based in $town and interested in:<br>
$interest<br>
You can contact them on:<br>
<b>Phone: </b>$phone<br>
<b>Email: </b>$email<br>
This is an automatically generated email from the company website generated from http://example.com";

$msg = wordwrap($msg,70);

mail('example@example.com','New Enquiry',$msg);

header("location:http://example.com");
?>

尝试使用if(mail('example@example.com','新查询',$msg))来确保邮件功能正常工作。 - undefined
已经完成了,但是显示的是一个空白页面,即没有错误信息。 - undefined
如果(mail('example@example.com','新查询',$msg)){echo "已发送";}else{echo "发送失败"; } - undefined
抱歉,我正在回复第一条评论,没有看到你的!我现在会尝试一下。 - undefined
你解决了这个问题吗? - undefined
显示剩余3条评论
1个回答

6

试试这个方法来找出问题所在

$success = mail('example@example.com','New Enquiry',$msg);
if (!$success) {
   print_r(error_get_last()['message']);
}

快速查看Php邮件文档

注意事项: 发送邮件时,邮件必须包含一个From头。这可以通过additional_headers参数设置,或在php.ini中设置默认值。 如果没有这样做,将会出现类似于Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing的错误消息。在Windows下,From标题还设置了Return-Path。

返回值

如果邮件成功接收并发送,则返回TRUE;否则返回FALSE。

重要提示:仅因为邮件被接受并发送,并不意味着邮件实际上到达了预期的目的地。

更新 根据评论,仅用最简单的方法调试,删除文件中的所有代码

<?php
// the message
$msg = "First line of text\nSecond line of text";

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);

// send email
if(!mail("someone@example.com","My subject",$msg)){
    var_dump(error_get_last()['message']);
}
?>

谢谢,我已经完成了这个任务,没有任何错误信息出现,一切都表现得像是成功发送了。 - undefined
嗯,更奇怪的是我得不到TRUE或FALSE,只是空白。 - undefined
啊,这就是为什么它说$success未定义。 - undefined
仍然显示为空白,我好迷惑!哈哈 - undefined
@Rockhopper 再次检查一下我的代码,(有打印错误吗?)? - undefined
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接