php 注册后发送电子邮件验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16373217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending an email verification after registering
提问by loomie
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. It even goes in the "Message success" if case, but I still can't get any email. What could be the problem?
以防万一你想知道我从哪里获取 $emailAddress:这只是一个代码片段,我已经让软件回显了电子邮件地址,它是正确的。如果情况下它甚至会进入“消息成功”,但我仍然无法收到任何电子邮件。可能是什么问题呢?
回答by aditya
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
回答by Ali Akbar Azizi
in local host i think the best way is using phpmailer
and gmail
account
在本地主机我认为最好的方法是使用phpmailer
和gmail
帐户
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/
回答by Ali Akbar Azizi
It seems your mail server SMTPis not configured correctly....
看来您的邮件服务器SMTP配置不正确....
check the port and IP of SMTP server address.
检查 SMTP 服务器地址的端口和 IP。
回答by user123_456
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
如果这不起作用,那么有很多关于如何实现您想要做的事情的教程。这是一个链接:从本地主机发送电子邮件
回答by Wiggler Jtag
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() 函数工作正常:问题可能是,该电子邮件正在进入垃圾邮件箱(因为这些本地主机邮件系统通常被标记为垃圾邮件机器人,因此电子邮件服务正在保护电子邮件免受未经验证的主机的攻击)。
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. Very easy, quick and your emails wont be marked as spam. And install PEAR library to send email. Here is an example.
对您来说最好的方法是使用一些免费或付费的 smtp 服务。非常简单、快捷,您的电子邮件不会被标记为垃圾邮件。并安装 PEAR 库来发送电子邮件。这是一个例子。
$from = '[email protected]';
$to = '[email protected]';
$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/[email protected]&token=79054025255fb1a26e4bc422aef54eb4
on registration page: generate the activation token like 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 种方法可以做到:)