php sending links via email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Josh_Dude83
Forum Newbie
Posts: 10
Joined: Mon Jul 12, 2010 11:15 am

php sending links via email

Post by Josh_Dude83 »

I'm trying to send an email from my code with a link to another page. Like so:

$subject = "Password Reset!";
$message = "Here is the link to reset your password: <a href='changepassword.php?id=2489&user=tom'>Link to Change Password</a> \r\n$userid, please don't forget it often!\n\n";
mail( $email, "Subject: $subject", $message, "From: someone@example.com" );

However, when I look it my email the link show up just as: <a href='changepassword.php?id=2489&user=tom'>Link to Change Password</a>
It's not actually a link. What am i doing wrong?
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: php sending links via email

Post by jraede »

You have to add e-mail headers to display the e-mail as HTML. But, 99% of e-mail clients automatically add links to e-mail addresses and URLs, so you can just write it out without the <a></a> tags and it will show up as a link.
Josh_Dude83
Forum Newbie
Posts: 10
Joined: Mon Jul 12, 2010 11:15 am

Re: php sending links via email

Post by Josh_Dude83 »

isn't that what I did in the above code? It doesn't work.
Josh_Dude83
Forum Newbie
Posts: 10
Joined: Mon Jul 12, 2010 11:15 am

Re: php sending links via email

Post by Josh_Dude83 »

Oh wow what a tard. Okay fixed it:

$subject = "Password Reset!";
$message = "Here is the link to reset your password: <ahref='http://...../changepassword.php?id=2489&user=tom'>Link to Change Password</a> \r\n$userid, please don't forget it often!\n\n";
mail( $email, "Subject: $subject", $message, "From: jap97@pitt.edu" );

I forgot to actually add the complete address. SO used to working on the local machine. Alright so...but the link still isn't showing up right.

Instead of:

Here is the link to reset your password: Link to Change Password
jap97, please don't forget it often!

Its showing up as:

Here is the link to reset your password: <ahref='http://.../changepassword.php?id=2489&user=tom'>Link to Change Password</a>
please don't forget it often!
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: php sending links via email

Post by jraede »

Right, like I said. You have to add headers to the e-mail via the mail() function in order to get it to properly display HTML tags. Because you don't have those headers, it will display like you have, with the tags among the text. You can either add the proper headers to get HTML to display, but it's pointless because e-mail clients automatically recognize links. Just type out the URL and it will show up as a link.
Josh_Dude83
Forum Newbie
Posts: 10
Joined: Mon Jul 12, 2010 11:15 am

Re: php sending links via email

Post by Josh_Dude83 »

I have no idea how to user headers. Never taught that. I just want the link to show up as "A Link to change password" and not the actual link. Looking up headers now, but help would be appreciated.
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: php sending links via email

Post by jraede »

I don't know how to be more clear. Just send it with the URL typed out like www.reset-password.com. The e-mail client will turn it into a link. No need to use headers.
Josh_Dude83
Forum Newbie
Posts: 10
Joined: Mon Jul 12, 2010 11:15 am

Re: php sending links via email

Post by Josh_Dude83 »

URL typed out like www.reset-password.com. The e-mail
But I dont want to see www.reset-password.com in my email. I want to see "Change password". I want the link to appear as that string not the actual link itself. That's what I'm getting at.
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: php sending links via email

Post by jraede »

Gotcha, then you'll have to use a content-type header. Check out example 3 on this site: http://www.w3schools.com/PHP/func_mail_mail.asp
Post Reply