【问题标题】:mail() is Incorrectly Sending Emails to cPanel Default Accountmail() 错误地向 cPanel 默认帐户发送电子邮件
【发布时间】:2019-05-30 14:23:20
【问题描述】:

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

Outlook 帐户示例:

  • sales@mydomain.com
  • contact@mydomain.com
  • matt@mydomain.com

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


我的网站托管在安装了 cPanel 的基本虚拟主机上,这意味着我获得了一个“默认”电子邮件帐户。例如: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.";
        }
    }

?>

【问题讨论】:

  • 我们需要更多细节,从发送邮件的代码开始。
  • 这是一个支持电子邮件附件的大脚本,它适用于我给它的任何“收件人”地址。但当然,我会编辑。
  • 与您的房东交谈。他们很有可能在同一台服务器上为您的域配置了邮件。如果域存在于本地,大多数 MTA 甚至都不会查看 DNS。
  • 只是为了确保脚本正确,排除问题的可能原因。
  • 无论如何都要避免在 PHP 中使用mail(),原因有很多。理想情况下,您应该使用商业级电子邮件网关,例如(这不是背书)SendGrid,它具有用于发送电子邮件的 Web 服务,比 PHP 的 mail() 或尝试直接从 PHP 使用 SMTP 更可靠。例如:sendgrid.com/docs/for-developers/sending-email/…

标签: php email mx-record


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-30
    • 2015-10-28
    • 2016-02-22
    • 2013-05-28
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    相关资源
    最近更新 更多