使用php向用户注册发送电子邮件

时间:2015-08-14 03:49:41

标签: php mysql email-validation

我正在使用MySQL构建注册表单来存储数据并完美地工作。当用户注册时将发送验证电子邮件以激活用户帐户。问题不是发送给电子邮件用户的验证邮件,而是用户数据和激活码可以进入mysql。

此处为 insert.php

的代码
<?php
    //panggil file config.php untuk menghubung ke server
    include('config.php');

    //tangkap data dari form
    $nama = $_POST['nama'];
    $username = mysql_real_escape_string($_POST['username']);
    $email = mysql_real_escape_string($_POST['email']);
    $alamat = $_POST['alamat'];
    $telp = $_POST['telp'];
    $password = mysql_real_escape_string($_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=mysql_query("SELECT id_user FROM user WHERE email='$email'");
        // email check
        if(mysql_num_rows($count) < 1)
        {
            $query = mysql_query("insert into user (nama,email,telp,password,alamat,username,activation) VALUES('$nama', '$email', '$telp', '$password', '$alamat', '$username', '$activation')") or die(mysql_error());
            if ($query) 
            {
                header('location:index.php');
            }
            // sending email
            include ('sendEmail.php');

            $to=$email;
            $subject="Email verification";
            $body='Hi, <br/> <br/> Silakan verifikasi dengan klik link di bawah ini. <br/> <br/> <a href="http://localhost/LAPAN-Project/verify.php?code=' . $activation . '">Verifikasi</a>';

            sendEmail($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
//}
?>

此处代码 sendEmail.php

<?php
function sendEmail($to,$subject,$body)
{
    require ('class.phpmailer.php');
    $from       = "me@kumistebal.web.id";
    $mail       = new PHPMailer();
    $mail->IsSMTP(true);            // use SMTP
    $mail->IsHTML(true);
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = "ssl://smtp.gmail.com"; // SMTP host
    $mail->Port       =  465;                    // set the SMTP port
    $mail->Username   = "*********@gmail.com";  // SMTP  username
    $mail->Password   = "*********";  // SMTP password
    $mail->SetFrom($from, 'From Name');
    $mail->AddReplyTo($from,'From Name');
    $mail->Subject    = $subject;
    $mail->MsgHTML($body);
    $address = $to;
    $mail->AddAddress($email, $to);

    if(!$mail->Send())
        echo "Mailer Error: " . $mail->ErrorInfo;
    else
        echo "Message has been sent";
}    

?>

有没有人知道怎么做?

2 个答案:

答案 0 :(得分:0)

而不是$mail->Host = "ssl://smtp.gmail.com"; // SMTP host

只需使用

$mail->Host = "smtp.gmail.com"; // SMTP host

答案 1 :(得分:0)

您需要使用$ mail-&gt; Host =&#34; smtp.gmail.com&#34 ;;更改主机// SMTP主机和$ mail-&gt;端口= 25; //设置SMTP端口