2011-10-08 24 views
0

我在我的代码的最后部分,电子邮件验证。但是,电子邮件似乎没有正确发送。为了防止垃圾邮件,我将用_EMAIL_替换我的电子邮件。PHP的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); 

所有数据被正确地进入数据库(没有不正确的变量),但电子邮件来,虽然是这样的:

Please confirm your email 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 

我假设这是我做错了,但经过各种故障排除后我无法发现它。

+0

看看你正在显示的输出,它看起来像脚本不是整个脚本。链接创建应该与一些HTML一起? –

回答

0

哪里是你的PHP邮件内容类型?要发送HTML邮件,必须设置Content-type标头。

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

?> 

就是这样。再次发送,一切都应该超链接。

1

默认情况下,mail发送纯文本电子邮件。如果您想发送HTML电子邮件,则必须在邮件中添加适当的标题。请记住,并非所有的电子邮件客户端都支持HTML邮件,并且有些人会禁用它。

随着中说,如果你仍然要发送HTML邮件(许多人做的),这里是如何:

mail($To, $Subject, $Message, 'Content-type: text/html');

也似乎与你的消息字符串的一些问题。 HTML链接是这样的:

<a href="target-url">link-text</a>

所以,尽量让你的消息字符串是这样的:

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