繁体   English   中英

注册后发送电子邮件验证

[英]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

在本地主机中,我认为最好的方法是使用phpmailergmail帐户

在这里的教程: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM