PHP mail() - 邮件不发送

时间:2012-08-19 00:32:36

标签: php

任何人都可以帮我解决我的问题。我试图让用户注册到我的网站,然后在他们验证了他们的电子邮件地址后登录。当他们注册时,他们的详细信息将被插入数据库中,一旦发生这种情况,该电子邮件应该被发送到他们的电子邮件中。我发现他们的详细信息正在插入表中,但电子邮件没有发送。我不知道我做错了什么,并且在我的智慧结束时试图找出它并且我也没有得到任何错误消息。另外,我还没有使用PHP很长时间。我也连接了数据库和东西。这是我的代码:

$email = $_POST['email'];

$emailcheck = "SELECT email FROM useraccounts WHERE email = '$email'";

$result = $conn->query($emailcheck); // executes query $emailcheck

//if the email exists it gives an error
if ($result->num_rows != 0) {
    echo "Sorry, the email $email is already in use.";
}

// check to see if both passwords entered match     
if ($_POST['password'] != $_POST['confirmpassword']) {
    echo "Your passwords did not match. Please try again.";
}

// to encrypt the password
$passhash = sha1($_POST['password']);

// to make confirm_code
$confirm_code = sha1($email);

if ($result->num_rows == 0 && $_POST['password'] == $_POST['confirmpassword']) {

    $insert = "INSERT INTO useraccounts (confirm_code, email, username, passhash, 
    firstname, lastname, gender, phone, address, userlevel, confirmed) 
    VALUES ('$confirm_code', '$email', '$_POST[username]', '$passhash', '$_POST[firstname]',
    '$_POST[lastname]', '$_POST[gender]', '$_POST[phone]', '$_POST[address]', 'user', 'false')";

    $add_member = $conn->query($insert);  // executes query $insert (adds user to table)

    //send confirmation email if user is added to useraccounts
    if($add_member) {

        //send email to
        $to = $email;  // $email = $_POST['email'] is present at top

        //email subject
        $subject = 'Registration Confirmation';

        //email message
        $message = 'my message goes here';

        $headers = 'From myname';

        //send email
        $sentmail = mail($to,$subject,$message,$headers);

        //if email is sent
        if($sentmail){
            echo "Your Confirmation link has been sent to your email address.";
        } else {
            echo "Cannot send Confirmation link to your email address."; // this statement is the output
        } // end if email is sent
    } else {
        echo "Cannot send Confirmation link to your email address.";
    } // end if member is added
}

非常感谢任何帮助:)

2 个答案:

答案 0 :(得分:4)

$headers = 'From myname';

这是一个无效的标题行。它应该是:

$headers = 'From: myname';

这可能导致邮件守护程序或服务器拒绝邮件格式错误。

答案 1 :(得分:0)

您的邮件服务器是否配置为开放中继?您正在尝试发送未经身份验证的电子邮件。大多数邮件服务器都不允许这样做。

检查this