【问题标题】:Mail function always calling the else part邮件函数总是调用 else 部分
【发布时间】:2019-03-20 14:14:07
【问题描述】:

我有两个域名。例如https://example.comhttps://example2.com

我正在使用邮件功能发送测试邮件。

我能够从https://example.com 发送电子邮件并且我获得了成功,但是我用于https://example2.com 的代码相同,并且我没有收到它总是调用else 部分的电子邮件。

测试邮件

<?PHP
$sender = 'xxx@xx.com';
$recipient = 'zzz@zz.com';

$subject = "php mail test";
$message = "php test message";
$headers = 'From: ' . $sender . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html;charset=UTF-8" . "\r\n";

if (mail($recipient, $subject, $message, $headers))
{
    echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
$errorMessage = error_get_last()['message'];
echo $errorMessage;
}
?>

【问题讨论】:

  • $headers .= "MIME-Version: 1.0" . "\r\n"; 你忘了连接 $headers
  • 您将不得不提供某种错误,以便我们为您提供帮助。 Try this。并且From 位被覆盖在您的标头字符串中(不使用.= 进行MIME)
  • 是的,我更新了同样的问题。 @smoqadam 感谢您的通知。
  • @rkeet,我用 $errorMessage = error_get_last()['message']; 更新代码但它没有显示任何错误。我只得到回声“错误:消息未接受”;
  • 试试看this

标签: php email


【解决方案1】:

From 标头无效,因为下一个标头紧随其后:

$headers = 'From:' . $sender;
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

应该是这样的:

// add \r\n
$headers = 'From: ' . $sender . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html;charset=UTF-8" . "\r\n";

【讨论】:

  • 感谢您的通知,我尝试了但仍然遇到同样的问题。prnt.sc/l6l39i
【解决方案2】:

试试这个简单的php邮件功能

$to      = $email_id;
$subject = "";
$message = "Your mail Body Content. you also use here HTML Tags for better performence. ";
$headers = 'From: xyz@domainname.com' . "\r\n" .
  'Reply-To: xyz@domainname.com' . "\r\n" .
  'X-Mailer: PHP/' . phpversion();
$headers.= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type:text/html;charset=UTF-8" . "\r\n";

if(mail($to, $subject, $message, $headers)){
   echo "Message accepted";
}else{
   echo "Error: Message not accepted";
}

【讨论】:

  • 感谢您的回复,它仍然显示其他部分。 prntscr.com/l6l39i 但相同的代码适用于我的第一个域
  • 您的表单邮箱是同域还是不同域?
  • 是的,它属于同一个域。我也尝试过其他的
  • 问题可能与您的托管服务提供商有关,您可以联系一次托管服务提供商支持团队
  • 你能强调一下不同之处吗? 究竟需要从原始代码更改为您的代码才能使其正常工作?
【解决方案3】:

检查example2.com是否启用了邮件功能:

if ( function_exists( 'mail' ) )
{
    echo 'mail() is available';
}
else
{
   echo 'mail() has been disabled';
}

如果禁用,请通过php.ini 或在 cPanel 中启用它。

【讨论】:

  • 我所做的只是注释掉我的 if else 条件并添加您的代码进行检查,我得到的输出是“mail() 可用”。
  • 您是否检查了服务器或服务器文档根目录上的错误日志?
  • 你能帮我检查一下吗?
猜你喜欢
  • 1970-01-01
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
相关资源
最近更新 更多