无法发送带有PHP的电子邮件

时间:2016-03-27 00:51:02

标签: php

我正在尝试在用户创建帐户后发送确认电子邮件。电子邮件不发送任何内容。我测试了它,电子邮件甚至没有进入垃圾邮件或收件箱文件夹。我错过了什么吗?谢谢你的帮助。

$status = "";
    if (isset($_POST["sign_up"])) {
        $first_name     = (isset($_POST['first_name']) ? $_POST['first_name'] : null);
        $last_name      = (isset($_POST['last_name']) ? $_POST['last_name'] : null);
        $username       = (isset($_POST['username']) ? $_POST['username'] : null);
        $password       = (isset($_POST['password']) ? $_POST['password'] : null);
        $email          = (isset($_POST['email']) ? $_POST['email'] : null);
        $phone          = (isset($_POST['phone']) ? $_POST['phone'] : null);

        if ($first_name == "" || $last_name == "" || $username == "" || $password == "" || $email == "" || $phone == "") {
            $status = '<p style="color:#FF0000;">Please fill out all the field';
        } else if (strlen($password) < 6) {
            $status = '<p style="color:#FF0000;">Password must more than 6 characters';
        } else{
            $sql = "INSERT INTO members(
                first_name,
                last_name,
                username,
                password,
                email,
                phone
                ) VALUES (
                '$first_name',
                '$last_name',
                '$username',
                '$password',
                '$email',
                '$phone'
                )";

            $res = mysqli_query($mysqli,$sql) or die(mysqli_error());
            if ($res) {
                $to         = $email;
                $subject    = "Confirmation from Yu Fong to $username";
                $from       = "abc@yahoo.com";
                $message    = "Please click the link below to verify and activate your account. \r\n";
                $message    .= "Testing";
                $header     = "From: " .$from. "\r\n";
                $header     .="Reply-To: ".$from. "\r\n";
                $header     .= "CC: abc@yahoo.com\r\n";
                $header     .= "Return-Path: ".$from. "\r\n";
                $header     .= "MIME-Version: 1.0\r\n";
                $header     .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
                $header     .= "X-Priority: 3\r\n";
                $header     .= "X-Mailer: PHP". phpversion() ."\r\n";


                $sentmail   = mail($to, $subject, $message, $header);

                if ($sentmail == true) {
                    echo $status = '<p style="color:#FF0000;">Congratulations! Your account has been created. An confirmation email has been sent!';
                } else {
                    echo "Eror!";
                }
            }
         }
    }

2 个答案:

答案 0 :(得分:0)

我刚刚测试了您的电子邮件表单,它运行正常,我收到了电子邮件。

$to         = "Myemail@derp.derp";
$subject    = "Confirmation from Yu Fong to $username";
$from       = "derpington";
$message    = "Please click the link below to verify and activate your account. \r\n";
$message    .= "Testing";
$header     = "From: " .$from. "\r\n";
$header     .="Reply-To: ".$from. "\r\n";
$header     .= "CC: abc@yahoo.com\r\n";
$header     .= "Return-Path: ".$from. "\r\n";
$header     .= "MIME-Version: 1.0\r\n";
$header     .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$header     .= "X-Priority: 3\r\n";
$header     .= "X-Mailer: PHP". phpversion() ."\r\n";


$sentmail   = mail($to, $subject, $message, $header);

if ($sentmail == true) {
    echo $status = '<p style="color:#FF0000;">Congratulations! Your account has been created. An confirmation email has been sent!';
} else {
    echo "Eror!";
}

这只是你的形式,它有效,所以我假设问题来自另一个地方,尝试改变

if ($res) {

if (1+1 == 2) {

检查你的If语句是否真的打印为true,然后从那里开始。

答案 1 :(得分:-1)

尝试更改

if ($sentmail == true) { ... 

if ($sentmail) { ...

或者稍微缩短一下

if(@mail($to, $subject, $message, $headers))

    {
      echo "Mail Sent Successfully";
    }else{
      echo "Mail Not Sent";
    }