我正在尝试使用php邮件库创建一个带有电子邮件激活表单的注册页面。在填写表格并提交我收到的页面后,我没有收到任何错误。我多次查看了我的电子邮件,但似乎没有收到任何电子邮件。由于没有错误,我不确定还有什么要找。以前有没有人遇到过类似的问题?这可能是gmail的一个问题吗?
继承我的Send_mail.php
<?php
function Send_Mail($to,$subject,$body)
{
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$from = "batoosay@gmail.com";
$mail = new PHPMailer();
$mail->IsSMTP(true); // use SMTP
$mail->IsHTML(true);
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "tls://smtp.gmail.com"; // SMTP host
$mail->Port = 465; // set the SMTP port
$mail->Username = "batoosay@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->SetFrom($from, 'From Name');
$mail->AddReplyTo($from,'From Name');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
$mail->Send();
}
?>
这是我的index.php
<?php
include 'db.php';
$msg='';
if(!empty($_POST['email']) && isset($_POST['email']) && !empty($_POST['password']) && isset($_POST['password']) )
{
// username and password sent from form
$email=mysqli_real_escape_string($connection,$_POST['email']);
$password=mysqli_real_escape_string($connection,$_POST['password']);
// regular expression for email check
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/';
if(preg_match($regex, $email))
{
$password=md5($password); // encrypted password
$activation=md5($email.time()); // encrypted email+timestamp
$count=mysqli_query($connection,"SELECT uid FROM users WHERE email='$email'");
// email check
if(mysqli_num_rows($count) < 1)
{
mysqli_query($connection,"INSERT INTO users(email,password,activation) VALUES('$email','$password','$activation')");
// sending email
include 'Send_Mail.php';
$to=$email;
$subject="Email verification";
$body='Hi, <br/> <br/> We need to make sure you are human. Please verify your email and get started using your Website account. <br/> <br/> <a href="'.$base_url.'activation/'.$activation.'">'.$base_url.'activation/'.$activation.'</a>';
Send_Mail($to,$subject,$body);
$msg= "Registration successful, please activate email.";
}
else
{
$msg= 'The email is already taken, please try new.';
}
}
else
{
$msg = 'The email you have entered is invalid, please try again.';
}
}
// HTML Part
?>
BTW它加载了很长时间。
答案 0 :(得分:1)
请将您的代码基于the gmail example provided with PHPMailer,因为它提供了正确的设置。
您在Host
属性中使用的语法很好(虽然不像设置SMTPSecure = 'tls'
那样常见),但您使用了错误的端口来使用{{1 (指定STARTTLS
时使用的内容) - 您需要使用tls
。这可能是导致您延误的原因。如果这不起作用,请阅读the troubleshooting guide。