为什么 Headers 阻止邮件发送?

Why Headers Preventing mail from sending?

这听起来可能与之前提出的问题相似,但请相信我,这不是

我试图通过 PHP mail() 函数从 LocalhostHostinger 服务器 但它们造成了不同的问题。

  1. 在本地主机上,尽管有 headers

    ,电子邮件仍以纯文本形式发送
     $headers =
         "MIME-Version: 1.0\r\n" . 
         "Content-Type: text/html; charset=UTF-8";
    

    我已经解决了 Whosebug 中的所有类似问题,并尝试了每一种方法,但我无法使其正常工作。在对此进行更多研究后,我发现 this

    I assume, that your email client is considering the smtp-server "unsafe", and hence is just going to display all the html as plaintext, rather than rendering it

  2. 因此我切换了我的主机并尝试做同样的事情,但这次我发现 headers 导致了问题。如果在 mail() 函数中传递 header 变量,则不会发送电子邮件。我试图连接没有用的 headers 。然后我制作了一个 headers 数组,并将它们与 php implode 连接起来,这也没有用。在 Whosebug 上的一个类似问题上,我发现如果使用 html, head, body 标签,那么网络邮件就会乱七八糟,因为它们使用 xhtml。我删除了它们,但仍然没有成功。

我也试过 error reporting,结果显示 module sqlite3 already loaded,我认为这与邮件无关。

下面是我的代码

php

<?php
 $email_template = file_get_contents("path/to/my/template");
 $lucky_number = rand(999999, 111111);
 $email_template = str_replace("{{user}}", "User", $email_template);
 $email_template = str_replace("{{lucky_number}}", $lucky_number, $email_template);
 $sender = "from:iusername@host.com"; // I found that if I dont use from, my mail ends up in spam folder
 $receiver = "username@host.com";
 $subject = "Random Subject Name";
 $headers =
    "MIME-Version: 1.0\r\n" . 
    "Content-Type: text/html; charset=UTF-8";

 if(mail($receiver, $subject, $email_template, $sender, $headers))
 {
    echo "Email Sent Successfully";
 }
 else
 {
    echo "Email Sending Failed";
 }

P.S 我不能使用 PHPMailer 或其他类似的库

发件人信息应该在headers

里面

因此,请更改以下行:

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

 if(mail($receiver, $subject, $email_template, $sender, $headers))
 

$sender = "iusername@host.com";

$headers = "From: $sender <$sender>\r\nReply-To: $sender\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";

 if(mail($receiver, $subject, $email_template, $headers))