PHP email() function not sending message correct?
I'm at the final part of my code, the email verification. However, the email don't seem to be sent correctly. For the sake of spam, I'll replace my email with _EMAIL_
.
$hash = genRandomString();
$link = 'http://www.website.com/verify.php?email=' . $email . '&hash=' . $hash;
$message = 'Please confirm your email address by click this following link, or copy and pasting it into your web browser.'
. $link;
mail($email, 'Account verification for website.com', $message);
All the data is going correctly into the database (No incorrect variables), but the email comes though something like this:
Please confirm your emai开发者_C百科l address by click this following link, or copy and pasting it into your web browser.
_EMAIL_&hash=0uhcoawgi3qsek8l7rjoheo'>http://www.website.com/verify.php?email=_EMAIL_&hash=0uhcoawgi3qsek8l7rjoheo
I'm assuming this is something I'm doing wrong, but I can't spot it after various pieces of troubleshooting.
By default, mail
sends plain text emails. If you want to send HTML emails, you have to add the appropriate header to the message. Keep in mind that not all email clients support HTML mail, and some people disable it.
With that said, if you still want to send HTML mail (lots of people do), here's how:
mail($To, $Subject, $Message, 'Content-type: text/html');
You also seem to have some issues with your message string. HTML links look like this:
<a href="target-url">link-text</a>
So, try making your message string something like this:
$message = 'Please confirm your email address by click this following link, or copy and pasting it into your web browser. <a href="'. $link . '">Activation Link</a>';
Where's your PHP mail content-type? To send HTML mail, the Content-type header must be set.
<?php
// message
$hash = genRandomString();
$link = 'http://www.website.com/verify.php?email=' . $email . '&hash=' . $hash;
$message = 'Please confirm your email address by click this following <a href="/link">link</a>.'
. $link;
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Mail it
mail($email, 'Account verification for website.com', $message, $headers);
?>
That's it. Send it again and everything should be hyperlinked.
精彩评论