邮件功能总是调用else部分

时间:2018-10-16 06:00:09

标签: php email

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

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

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

测试邮件

<?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;
}
?>

3 个答案:

答案 0 :(得分: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";

答案 1 :(得分:0)

使用以下方法检查example2.com上是否已启用邮件功能:

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

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

答案 2 :(得分:0)

尝试这个简单的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";
}