如何在 PHP 电子邮件中插入超链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27134137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:12:09  来源:igfitidea点击:

How to insert a hyperlink in PHP email message?

phpemail

提问by Nathalie

I tried to do my own research but the following links did not help me.

我试图做我自己的研究,但以下链接对我没有帮助。

PHP Email & HyperlinksPHP: Send mail with link

PHP 电子邮件和超链接PHP:发送带有链接的邮件

$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>';

In the email message I want there to be the link URL, when user clicks it, he gets redirected to the URL page. At this moment when I checked it, I receive the email but the link is plain text with quote marks around it. Like this "www.example.com"

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

What am I doing wrong?

我究竟做错了什么?

回答by Funk Forty Niner

Remove the escapes for your double quotes in

删除双引号的转义符

"<a href =\"www.example.com\">www.example.com</a>"
          ^                ^

and add http://or https://

并添加http://https://

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

or

或者

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

if you don't want quotes around it.

如果你不想要它周围的引号。



Tested with: (and assuming your SITE_NAMEconstanthas already been defined, and other variables).

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

<?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;

回答by geekbro

I had a similar issue while sending email in php , i used stripslashes()with the email bodyand it worked .you can also try and seeif that works for you

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

$mail_content = stripslashes($mail_content);