简体   繁体   English

注册后发送电子邮件验证

[英]Sending an email verification after registering

I want, that users who register on my site, have to activate their account first, before using it. 我希望在我的网站上注册的用户必须先激活其帐户,然后才能使用它。 The problem is, that I don't get any email to my email test account. 问题是,我没有收到任何发送到我的电子邮件测试帐户的电子邮件。

Before I start posting a code, could the problem be, that I'm working currently on a local machine with xampp? 在我开始发布代码之前,问题可能出在我当前正在使用xampp在本地计算机上工作吗?

Otherwise, here is the code snippet 否则,这是代码段

$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>");
    }

Just in case you wonder where i take $emailAddress from: This is just a code snippet, i already let the software echo the email address, and it's correct. 以防万一,您想知道我从哪里获取$ emailAddress:这只是一个代码片段,我已经让软件回显了电子邮件地址,这是正确的。 It even goes in the "Message success" if case, but I still can't get any email. 如果出现这种情况,它甚至会出现在“消息成功”中,但我仍然无法收到任何电子邮件。 What could be the problem? 可能是什么问题呢?

After submit the form you can use a code or link and send it to the user email id 提交表单后,您可以使用代码或链接并将其发送给用户的电子邮件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";

Code from TalkersCode.com for complete tutorial visit http://talkerscode.com/webtricks/account-verification-system-through-email-using-php.php 来自TalkersCode.com的完整教程代码,请访问http://talkerscode.com/webtricks/account-verification-system-through-email-using-php.php

in local host i think the best way is using phpmailer and gmail account 在本地主机中,我认为最好的方法是使用phpmailergmail帐户

here the tutorial : http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/ 在这里的教程: http : //www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/

It seems your mail server SMTP is not configured correctly.... 看来您的邮件服务器SMTP配置不正确。

check the port and IP of SMTP server address. 检查SMTP服务器地址的端口和IP。

Try to change you PHP.ini file like this and tell me if it works. 尝试像这样更改您的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

If this is not working then there are a lot of tutorials of how to achieve what you are trying to do. 如果这不起作用,那么会有很多教程介绍如何实现您想做的事情。 Here is one link: Send email from localhost 这是一个链接: 从本地主机发送电子邮件

Had the same problem, but on linux. 有同样的问题,但是在linux上。

1) Assuming your mail() function is working properly: the problem could be, that email is coming into spam-box (because these localhost mail systems are often marked as spambots, so email services are protecting emails from unverified host). 1)假设您的mail()函数正常工作:问题可能是电子邮件进入了垃圾邮件箱(因为这些localhost邮件系统通常被标记为垃圾邮件,因此电子邮件服务可以保护电子邮件免受未经验证的主机的侵害)。

2) If mail() is not working, its not configured properly, if you follow some tutorial and configure it, which needs like 5 minutes, you will realise why not to use it:) 2)如果mail()无法正常工作,则说明其配置不正确,如果您按照一些教程进行了配置(大约需要5分钟),您将了解为什么不使用它:)

The best way for you is to use some free or paid smtp service. 最好的方法是使用一些免费或付费的smtp服务。 Very easy, quick and your emails wont be marked as spam. 非常简单,快捷,您的电子邮件将不会被标记为垃圾邮件。 And install PEAR library to send email. 并安装PEAR库以发送电子邮件。 Here is an example. 这是一个例子。

$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>");
}

To your verification code: instead of telling user to write down the verification code, better generate him the link, that he clicks his account gets activated. 针对您的验证码:与其说让用户写下验证码,不如说是更好地为他生成链接,即单击用户的帐户将被激活。 Something like 就像是

http://localhost/FinalYear/activation.php?email=some@email.com&token=79054025255fb1a26e4bc422aef54eb4

on registration page: generate the activation token like md5($email . '_sometext'); 在注册页面上:生成类似于md5($email . '_sometext');的激活令牌md5($email . '_sometext');

on activation page if(md5($_GET['email'] . '_sometext') == $_GET['token']) { activate.. } 在激活页面上if(md5($_GET['email'] . '_sometext') == $_GET['token']) { activate.. }

please note, that it is just an example, there are 10 ways how it could be done :) 请注意,这只是一个示例,有10种方法可以完成它:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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