duankange2433 2012-08-19 00:32
浏览 21

PHP mail() - 邮件不发送[关闭]

would anyone be able to help me with my question. I am trying to enable users to register to my site, and then login after they've verified their email address. When they register, their details get inserted in a database and once that happens, the email is supposed to be sent to their email. I find that their details are being inserted into the table, but the email is not sending. I don't know what I'm doing wrong and am at my wits end trying to figure it out and I'm not getting any error messages either. Also, I haven't been using PHP for very long. I also have my database connected and stuff. Here is my code:

$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
}

Any help would be greatly appreciated :)

  • 写回答

2条回答 默认 最新

  • dongshan4316 2012-08-19 00:34
    关注
    $headers = 'From myname';
    

    This is an invalid header line. It should be:

    $headers = 'From: myname';
    

    This is probably causing a mail daemon or server to reject the message as ill-formed.

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大