【问题标题】:How to insert a hyperlink in PHP email message?如何在 PHP 电子邮件中插入超链接?
【发布时间】:2017-11-26 14:55:11
【问题描述】:

我尝试进行自己的研究,但以下链接对我没有帮助。

PHP Email & Hyperlinks PHP: Send mail with link

$subject = "Thank you for registering to " . SITE_NAME;

                    $mail_content = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                                <html xmlns="http://www.w3.org/1999/xhtml">
                                <head>
                                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                                </head>
                                <body>

                                <div>
                                        <p>' . $user->first_name . ' ' . $user->lastname_name . ', thank you for registering to ' . SITE_NAME . '.</p>
                                        <p>Please click the following link to proceed to the Questionnaire "<a href =\"www.example.com\">www.example.com</a>"</p>


                                </div>
                                </body>
                                </html>';

在我希望有链接 URL 的电子邮件中,当用户单击它时,他会被重定向到 URL 页面。在我检查它的那一刻,我收到了电子邮件,但链接是带有引号的纯文本。像这样的“www.example.com”

我做错了什么?

【问题讨论】:

  • 如果这就是您所做的全部:还不够,您需要告诉电子邮件收件人您实际上是在发送 HTML,并且您需要在其中添加一个协议url 前面(如`http://` 或https://)。
  • 您发布的两个链接都可以用作重复关闭。那么你是如何将它与邮件结合使用的呢?
  • 如果这些链接没有为我的代码提供解决方案,则不能重复。
  • 正如 Wrikken 所说,您需要将 http:// 添加到 www.example.com 中,如 &lt;a href =\"http://www.example.com\"&gt;www.example.com&lt;/a&gt; - 并尝试删除 "&lt;a href =\"www.example.com\"&gt;www.example.com&lt;/a&gt;"&lt;/p&gt; 中的引号,如 &lt;a href ="http://www.example.com"&gt;www.example.com&lt;/a&gt;&lt;/p&gt; 中一样
  • Fred-ii ,它仍然不是一个链接。是否有任何适当的 php 代码可以将所需的文本转换为链接?

标签: php email


【解决方案1】:

删除

中双引号的转义符
"<a href =\"www.example.com\">www.example.com</a>"
          ^                ^

并添加http://https://

"<a href ="http://www.example.com">www.example.com</a>"

<a href ="http://www.example.com">www.example.com</a>

如果你不想在它周围加上引号。


测试:(并假设您的SITE_NAME constant 已经定义,以及其他变量)。

<?php

define("SITE_NAME", "Our Website!"); // I added that

$subject = "Thank you for registering to " . SITE_NAME;

$mail_content = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

<div>
        <p>' . $user->first_name . ' ' . $user->lastname_name . ', thank you for registering to ' . SITE_NAME . '.</p>
        <p>Please click the following link to proceed to the Questionnaire "<a href ="http://www.example.com">www.example.com</a>"</p>


</div>
</body>
</html>';

echo $mail_content;

【讨论】:

  • @Nathalie 总是很高兴能提供帮助。请记住将其标记为已解决。
【解决方案2】:

我在用 php 发送电子邮件时遇到了类似的问题,我使用stripslashes() 和电子邮件 body 并且它有效。你也可以尝试看看它是否适合你

$mail_content = stripslashes($mail_content);

【讨论】:

    猜你喜欢
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 2017-10-21
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多