php - PHP Mail 函数中链接的奇怪行为

标签 php email hyperlink

我目前正在尝试获取通过 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/

相关文章:

php - 显示来自mysql数据库的数据

php - 随机改变背景

javascript - PHP创建动态链接,如何在点击时调用Jquery上的右侧div来执行AJAX调用

php - 如何使用 php 开始下载

c# - 如何在 Azure 中以编程方式接收电子邮件?

email - DB2 如何从存储过程发送电子邮件?

c# - Process.Start(link) 省略部分链接

css - 如何更改与事件页面对应的超链接中的图像?

ruby-on-rails - Rails 4 actionmailer 不发送电子邮件

html - 单击时同时更改两个脚注链接的背景颜色?