簡體   English   中英

無法使用PHP發送電子郵件

[英]Can't send an email with 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!";
                }
            }
         }
    }

我剛剛測試了您的電子郵件表格,它運行正常,我收到了電子郵件。

$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,然后從那里開始。

嘗試改變

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

if ($sentmail) { ...

或縮短一點嘗試

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM