[英]Sending an email verification after registering
我希望在我的網站上注冊的用戶必須先激活其帳戶,然后才能使用它。 問題是,我沒有收到任何發送到我的電子郵件測試帳戶的電子郵件。
在我開始發布代碼之前,問題可能出在我當前正在使用xampp在本地計算機上工作嗎?
否則,這是代碼段
$random = substr(number_format(time() * rand(),0,'',''),0,10);
$insertMailVerify = $this->db->prepare("INSERT INTO mailverify (mailAddress, token, datetime) VALUES (:mailAddress, :token, :date)");
$insertMailVerify->execute(array(':mailAddress'=>$emailAddress,
':token'=>$random,
':date'=>$date));
$to = $emailAddress;
$subject = "Activating your Account";
$body = "Hi, in order to activate your account please visit http://localhost/FinalYear/activation.php?email=".$emailAddress." and fill in the verification code $random";
if(mail($to, $subject, $body))
{
echo ("<p>Message success</p>");
}
else {
echo ("<p>Message fail</p>");
}
以防萬一,您想知道我從哪里獲取$ emailAddress:這只是一個代碼片段,我已經讓軟件回顯了電子郵件地址,這是正確的。 如果出現這種情況,它甚至會出現在“消息成功”中,但我仍然無法收到任何電子郵件。 可能是什么問題呢?
提交表單后,您可以使用代碼或鏈接並將其發送給用戶的電子郵件ID
$message = "Your Activation Code is ".$code."";
$to=$email;
$subject="Activation Code For Talkerscode.com";
$from = 'your email';
$body='Your Activation Code is '.$code.' Please Click On This link <a href="verification.php">Verify.php?id='.$db_id.'&code='.$code.'</a>to activate your account.';
$headers = "From:".$from;
mail($to,$subject,$body,$headers);
echo "An Activation Code Is Sent To You Check You Emails";
來自TalkersCode.com的完整教程代碼,請訪問http://talkerscode.com/webtricks/account-verification-system-through-email-using-php.php
在本地主機中,我認為最好的方法是使用phpmailer
和gmail
帳戶
在這里的教程: http : //www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/
看來您的郵件服務器SMTP配置不正確。
檢查SMTP服務器地址的端口和IP。
嘗試像這樣更改您的PHP.ini
文件,並告訴我它是否有效。
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 465 //if 465 is not working then try 25
如果這不起作用,那么會有很多教程介紹如何實現您想做的事情。 這是一個鏈接: 從本地主機發送電子郵件
有同樣的問題,但是在linux上。
1)假設您的mail()函數正常工作:問題可能是電子郵件進入了垃圾郵件箱(因為這些localhost郵件系統通常被標記為垃圾郵件,因此電子郵件服務可以保護電子郵件免受未經驗證的主機的侵害)。
2)如果mail()無法正常工作,則說明其配置不正確,如果您按照一些教程進行了配置(大約需要5分鍾),您將了解為什么不使用它:)
最好的方法是使用一些免費或付費的smtp服務。 非常簡單,快捷,您的電子郵件將不會被標記為垃圾郵件。 並安裝PEAR庫以發送電子郵件。 這是一個例子。
$from = 'from@domain.com';
$to = 'to@domain.com';
$subject = 'subject';
$body = 'Hello!';
$host = 'smtpserver.com';
$port = '25';
$username = 'yourlogin';
$password = 'yourpass';
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if(PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
針對您的驗證碼:與其說讓用戶寫下驗證碼,不如說是更好地為他生成鏈接,即單擊用戶的帳戶將被激活。 就像是
http://localhost/FinalYear/activation.php?email=some@email.com&token=79054025255fb1a26e4bc422aef54eb4
在注冊頁面上:生成類似於md5($email . '_sometext');
的激活令牌md5($email . '_sometext');
在激活頁面上if(md5($_GET['email'] . '_sometext') == $_GET['token']) { activate.. }
請注意,這只是一個示例,有10種方法可以完成它:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.