首页 > 解决方案 > mail() 错误地向 cPanel 默认帐户发送电子邮件

问题描述

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

示例 Outlook 帐户:

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


我的网站托管在安装了 cPanel 的基本虚拟主机上,这意味着我获得了一个“默认”电子邮件帐户。例如:default@mydomain.com。我在我的网站(联系表格)上编写了一个 PHP 脚本,通过mail()to发送电子邮件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.";
        }
    }

?>

标签: phpemailmx-record

解决方案


解决方案是将我的 cPanel“电子邮件路由”选项更改为“远程”,以便所有本地电子邮件首先检查 MX 记录。

在此处输入图像描述


推荐阅读