2011-10-08 22 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>';