Nathalie
Nathalie

Reputation: 134

How to insert a hyperlink in PHP email message?

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

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

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"

What am I doing wrong?

Upvotes: 4

Views: 39049

Answers (2)

geekbro
geekbro

Reputation: 1323

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

$mail_content = stripslashes($mail_content);

Upvotes: 0

Funk Forty Niner
Funk Forty Niner

Reputation: 74220

Remove the escapes for your double quotes in

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

and add http:// or 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_NAME constant has already been defined, and other variables).

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

Upvotes: 4

Related Questions