mail() 错误地将电子邮件发送到 cPanel 默认帐户

mail() is Incorrectly Sending Emails to cPanel Default Account

我从 GoDaddy 购买了一个域并将其链接到 Office 365(通过 MX 记录)。这意味着我在 Outlook 中有几个电子邮件帐户是@mydomain.com.

示例 Outlook 帐户:

我可以 send/receive 通过这些帐户发送电子邮件。


我的网站托管在安装了 cPanel 的基本虚拟主机上,这意味着我获得了一个 "default" 电子邮件帐户。例如:default@mydomain.com。我在我的网站(联系表)上写了一个 PHP 脚本,它通过 mail() 发送电子邮件到 contact@mydomain.com

但是,所有电子邮件都发送到默认的 cPanel 帐户 default@mydomain.com 而不是 Outlook 帐户 contact@mydomain.com

为了测试,我尝试将电子邮件发送到未托管在 mydomain 上的我的个人帐户,并且它按预期工作。电子邮件会立即发送。

为什么我的网站会错误地向 Outlook 帐户发送电子邮件?感谢您的宝贵时间。


编辑:

请求脚本:

<?php

    $uploadedFile = $statusMsg = '';

    if (isset($_POST['submit']))
    {
        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];

        if(!empty($first_name) && !empty($last_name) && !empty($message))
        {
            if(filter_var($email, FILTER_VALIDATE_EMAIL))
            {
                $uploadStatus = 1;

                if(!empty($_FILES["attach"]["name"]))
                {
                    $targetDir = "uploads/";
                    $fileName = basename($_FILES["attach"]["name"]);
                    $targetFilePath = $targetDir . $fileName;
                    $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

                    if(move_uploaded_file($_FILES["attach"]["tmp_name"], $targetFilePath))
                    {
                        $uploadedFile = $targetFilePath;
                    }
                    else
                    {
                        $uploadStatus = 0;
                        $statusMsg = "Sorry, there was an error uploading your file.";
                    }
                }

                if($uploadStatus == 1)
                {
                    $name = $first_name.' '.$last_name;

                    $mailTo = "contact@mydomain.com";//changed to my real outlook account

                    $htmlContent = '<h2>Contact Request Submitted</h2>
                    <p><b>Name:</b> '.$name.'</p>
                    <p><b>Email:</b> '.$email.'</p>
                    <p><b>Phone:</b> '.$phone.'</p>
                    <p><b>Message:</b><br/>'.$message.'</p>';

                    // Header for sender info
                    $headers = "From: $name"." <".$email.">";


                    if(!empty($uploadedFile) && file_exists($uploadedFile))
                    {
                        // Boundary 
                        $semi_rand = md5(time()); 
                        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

                        // Headers for attachment 
                        $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

                        // Multipart boundary 
                        $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
                        "Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n"; 

                        // Preparing attachment
                        if(is_file($uploadedFile)){
                            $message .= "--{$mime_boundary}\n";
                            $fp =    @fopen($uploadedFile,"rb");
                            $data =  @fread($fp,filesize($uploadedFile));
                            @fclose($fp);
                            $data = chunk_split(base64_encode($data));
                            $message .= "Content-Type: application/octet-stream; name=\"".basename($uploadedFile)."\"\n" . 
                            "Content-Description: ".basename($uploadedFile)."\n" .
                            "Content-Disposition: attachment;\n" . " filename=\"".basename($uploadedFile)."\"; size=".filesize($uploadedFile).";\n" . 
                            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
                        }

                        $message .= "--{$mime_boundary}--";
                        $returnpath = "-f" . $email;

                        // Send email
                        $mail = mail($mailTo, "Contact Form Submission from ".$name, $message, $headers, $returnpath);

                        // Delete attachment file from the server
                        @unlink($uploadedFile);
                    }
                    else
                    {
                         // Set content-type header for sending HTML email
                        $headers .= "\r\n". "MIME-Version: 1.0";
                        $headers .= "\r\n". "Content-type:text/html;charset=UTF-8";

                        // Send email
                        $mail = mail($mailTo, 'Contact Form Submission from '.$name, $htmlContent, $headers); 
                    }

                    // If mail sent
                    if($mail)
                    {
                        $statusMsg = "Your message has been sent. Thanks!";
                    }
                    else
                    {
                        $statusMsg = 'Your contact request submission failed, please try again.';
                    }
                }

            }
            else
            {
                $statusMsg = 'Please enter a valid email address.';
            }
        }
        else
        {
            $statusMsg = "Please fill out the required information.";
        }
    }

?>

解决方案是将我的 cPanel "Email Routing" 选项更改为 "Remote",以便所有本地电子邮件首先检查 MX 记录。