我目前正在尝试获取通过 PHP 邮件功能发送的电子邮件中的链接。这是我的代码 - 我还包含了一些我尝试过的东西(连同注释一起注释掉):
$to = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9adbcaaad99adbcaaadb0b7bef7bab6b4" rel="noreferrer noopener nofollow">[email protected]</a>";
<pre><code>$subject = "Testing email";
//$body = '<strong>This is strong text</strong>'; <-- Works
//and the text is correctly emphasised.
//$body = '<a href="www.yahoo.com">Link Test</a>'; <-- Works
//but without http:// at the start makes the link relative to the server root
//$body = "<a href='http://www.yahoo.com'>Link Test</a>"; <-- Does not work
//$body = "<a href=\"http://www.yahoo.com\">Link Test</a>"; <-- Does not work
$body = '<a href="http://www.yahoo.com">Link Test</a>'; //<-- Does not work
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Steven Parler <". $to . ">\r\n";
$headers .= "X-Mailer: PHP/".phpversion() . "\r\n";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
</code></pre>
可以看出,html 在没有链接的情况下也可以工作,并且只要我不在链接本身中包含“http://”,链接也可以工作。如果我确实包含“http://”,则根本不会接收/发送任何电子邮件(我不确定哪一个,因为 mail() 命令返回 true 表示它已发送)。
我不太确定为什么这不起作用。我的虚拟主机服务器上是否需要更改某些设置?我使用的是 Windows 共享主机。
提前感谢任何人能给我的任何建议——我一直在为这个问题烦恼,哈哈。 :)
最佳答案
不要手动构建 MIME 消息。它太痛苦和脆弱了。使用类似 PHPMailer 的内容或SwiftMailer自动为您完成。您提供 HTML,他们将提供适当的 header
关于php - PHP Mail 函数中链接的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4974581/