确认用户创建电子邮件

时间:2014-10-21 13:50:26

标签: php email smtp

我为我的网站设置了一个非常简单的注册框。输入信息并单击“提交”后,它会记录所有信息,并显示“您的确认链接已发送到您的电子邮件地址”。'但电子邮件无法送达。我认为问题在于我的SMTP设置,但我很难得到这个设置。请有人帮忙

<?php
ini_set ('SMTP', '############', 587);
ini_set ('sendmail_from', '############');
session_start();
include('configdb.php');

if(isset($_POST['submit']))
{
    //whether the username is blank
    if($_POST['username'] == '')
    {
        $_SESSION['error']['username'] = "User Name is required.";
    }
    //whether the email is blank
    if($_POST['email'] == '')
    {
        $_SESSION['error']['email'] = "E-mail is required.";
    } else {
        //whether the email format is correct
        if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])
           +([a-zA-Z0-9._-]+)+$/", $_POST['email']))
        {
            //if it has the correct format whether the email has already exist
            $email= $_POST['email'];
            $sql1 = "SELECT * FROM user WHERE email = '$email'";
            $result1 = mysqli_query($mysql,$sql1) or die(mysqli_error());
            if (mysqli_num_rows($result1) > 0)
            {
                $_SESSION['error']['email'] = "This Email is already used.";
            }
        } else {
            //this error will set if the email format is not correct
            $_SESSION['error']['email'] = "Your email is not valid.";
        }
    }
    //whether the password is blank
    if($_POST['password'] == '')
    {
        $_SESSION['error']['password'] = "Password is required.";
    }
    //if the error exist, we will go to registration form
    if(isset($_SESSION['error']))
    {
        header("Location: index.php");
        exit;
    } else {
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $com_code = md5(uniqid(rand()));

        $sql2 = "INSERT INTO user (username, email, password, com_code) VALUES ('$username', '$email', '$password', '$com_code')";
        $result2 = mysqli_query($mysql,$sql2) or die(mysqli_error());

        if($result2)
        {
            // To send HTML mail, the Content-type header must be set
            $headers  = 'MIME-Version: 1.0' . "\n";
            $headers .= 'Content-type: text/html; charset=utf-8' . "\n";

            // Additional heade rs
            $headers .= "From: ".$from . "\n";
            //$headers .= 'Bcc: info@domain.co.uk' . "\n";
            $to = $email;
            $subject = "Confirmation from TutsforWeb to $username";
            $header = "TutsforWeb: Confirmation from TutsforWeb";
            $message = "Please click the link below to verify and activate your account. rn";
            $message .= "http://www.yourname.com/confirm.php?passkey=$com_code";
            $sentmail = mail($to,$subject,$message,$header);

            if($sentmail)
            {
                echo "Your Confirmation link Has Been Sent To Your Email Address.";
            } else {
                echo "Cannot send Confirmation link to your e-mail address";
            }
        }
    }
}
?>

1 个答案:

答案 0 :(得分:0)

设置两件事:

<?php
  ini_set("SMTP", '############');
  ini_set("smtp_port", "587");
?>