php - 为什么标头阻止邮件发送?
问题描述
这可能听起来像以前提出的问题,但相信我不是
我试图通过Localhost和Hostinger 服务器mail()
的 PHP函数发送使用 HTML 模板的电子邮件,但它们产生了不同的问题。
在本地主机上,尽管有标题,但电子邮件仍以纯文本形式发送
$headers = "MIME-Version: 1.0\r\n" . "Content-Type: text/html; charset=UTF-8";
我已经在stackoverflow中解决了所有类似的问题,并尝试了每一件事,但我无法让它发挥作用。在对此进行了更多研究后,我发现了这一点
我假设,您的电子邮件客户端正在考虑 smtp 服务器“不安全”,因此只会将所有 html 显示为纯文本,而不是呈现它
因此,我切换了主机并尝试做同样的事情,但这次我发现
headers
是导致问题的原因。header
如果在函数中传递变量,则不会发送电子邮件mail()
。我试图连接不起作用的标题。然后我制作了一组标题并将它们与 php 一起加入,implode
这也不起作用。html, head, body
在 stackoverflow 上的一个类似问题上,我发现如果使用标签时,webmails 会搞砸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";
}
PS 我不能使用 PHPMailer 或其他类似的库
解决方案
发件人信息应在标头内
因此,请更改以下行:
$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))
推荐阅读
- c# - 使用 c# 创建多个字典并附加到列表中
- android - 禁止来自 sdk 的颤振测试
- insert - 使用 SAS 将包含空值和空白的行插入数据库
- dependencies - 我可以使用测试依赖项来调节 pytest-xdist 并行执行吗?
- azure - Azure Function with KEDA 没有横向扩展
- visual-studio-code - VSCode:我可以打开单个文件而不是工作区吗?
- node.js - 意外的标记 ?在 JSON 中的位置 0
- node.js - 如何在 json 格式的环回过滤器中使用范围
- javascript - 函数被调用但没有完成渲染
- java - 为什么我无法显示我的列表元素?- 爪哇