【问题标题】:PHP mail() function sending mail but being received blankPHP mail() 函数发送邮件但收到空白
【发布时间】:2020-03-08 01:56:01
【问题描述】:

我正在使用 PHP 的邮件功能从我的网站发送邮件,直到最近我更改了实际邮件的内容时,这一直有效。

我尝试将内容改回来,但同样的事情发生了。

我确实收到了邮件,当我查看原始邮件标题时可以看到邮件内容,但实际电子邮件看起来是空白的。

这就是我发送电子邮件的方式:

$to = [customer_email];
$subject = [email_subject];

$message_header= [message_header];
$message_content= [message_content];
$message_footer = [message_footer ];

$uid = md5(uniqid(time()));

$headers = "From: support@domain.com" . "\r\n";
$headers .= "Reply-To: support@domain.com" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n";
$headers .= "Bcc: sales@domain.com" . "\r\n";

$message = "--".$uid."\r\n";
$message .= "Content-type:text/html; charset=UTF-8" . "\r\n";
$message .= $message_header.$message_content.$message_footer . "\r\n\r\n";

mail($to, $subject, $message, $headers);

就像我说的,邮件正在发送和接收,没有被标记为垃圾邮件,只是显示为空白,但可以在邮件标题中看到内容。

完整的邮件标题在这里,只是更改了域和电子邮件地址等详细信息:

Delivered-To: it@example.com
Received: by 2002:a92:a047:0:0:0:0:0 with SMTP id b7csp3413998ilm;
        Tue, 12 Nov 2019 02:40:36 -0800 (PST)
X-Received: by 2002:a1c:dd06:: with SMTP id u6mr3380338wmg.109.1573555236560;
        Tue, 12 Nov 2019 02:40:36 -0800 (PST)
ARC-Seal: i=3; a=rsa-sha256; t=1573555236; cv=pass;
        d=google.com; s=arc-20160816;
        b=yW1gv+dBt0A/d/YPiLlXTyHZpPj5ImvpnZFs48/mV4D3OxnuPdXLzr32lkkzaSIhJ5
         DsXg9gjJbvsqPSsq0bdHSliVewxEcKqRYo2PGtlzBO47apCHKzAO3BjHFKL//A6MaGTE
         tzH5Eo7lwas1Ebq0lO3D9wyb//8Qp08ghABKIlDaHA5bhOWwiBal+sS3i+lHcsVzucx1
         65K1BA7uyyi+T4VNm9NQfklFBRH7fVPBGSxNItdPFhjq2M9iqBtJnquf4lbdeQoS2XxV
         3wVM7o2Rbojz/cUkiYx/GdE4EmKRV30Ws6+MHkm2lKPzA+xQmTprqu3S4qsuDWUcZ5Uo
         /Z0w==
ARC-Message-Signature: i=3; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post
         :list-id:mailing-list:precedence:date:message-id:mime-version
         :reply-to:from:subject:to;
        bh=Ibrhyqugys65Y8gzuSGloywNGoGY735hgcq1co4q+S8=;
        b=t2j1MvmNYcAlMt5Jkc+e4fImPECZ1+HqFd+QyCHe7dg0VwXDDZmxavpE93d9/FLCwG
         b9TGvdowKYr8eTrIzj8gvLPpzIN0KnK4xeBqzc6N9jDECfIU85tbW3ecSPvdBMEWBnQS
         pdtyrvl60Ph07Vu0cS7jlIOkHYbx84Z/geTW26pdS1mkecwxtV6WYyxrDVD+ZB0GBmql
         0UnU85ubA56tKy2WfcaKEY4nZf98wSFuyombgFD5gwqUdJRWU3997mzjzzgoLX/kbAXo
         3xBtnRj/nibLxUivbx2ABkND4eKw/lJcF+tuqIW2SXXl5HeVQb/jhPF5DmzHwGvZGPV/
         dL7g==
ARC-Authentication-Results: i=3; mx.google.com;
       arc=pass (i=2 spf=pass spfexample=our_shared_server);
       spf=pass (google.com: example of sales+bncbcb3zg7v6mnbbjeyvlxakgqe2rwanzi@example.com designates 209.85.220.69 as permitted sender) smtp.mailfrom=sales+bncBCB3ZG7V6MNBBJEYVLXAKGQE2RWANZI@example.com
Return-Path: <sales+bncBCB3ZG7V6MNBBJEYVLXAKGQE2RWANZI@example.com>
Received: from mail-sor-f69.google.com (mail-sor-f69.google.com. [209.85.220.69])
        by mx.google.com with SMTPS id z22sor1615053wml.11.2019.11.12.02.40.36
        for <it@sublift.ie>
        (Google Transport Security);
        Tue, 12 Nov 2019 02:40:36 -0800 (PST)
Received-SPF: pass (google.com: example of sales+bncbcb3zg7v6mnbbjeyvlxakgqe2rwanzi@example.com designates 209.85.220.69 as permitted sender) client-ip=209.85.220.69;
Authentication-Results: mx.google.com;
       arc=pass (i=2 spf=pass spfexample=our_shared_server);
       spf=pass (google.com: example of sales+bncbcb3zg7v6mnbbjeyvlxakgqe2rwanzi@example.com designates 209.85.220.69 as permitted sender) smtp.mailfrom=sales+bncBCB3ZG7V6MNBBJEYVLXAKGQE2RWANZI@example.com
ARC-Seal: i=2; a=rsa-sha256; t=1573555236; cv=pass;
        d=google.com; s=arc-20160816;
        b=AFvKNFKKKd+cTeGeS69+AqLOH/SZX9ku5Tl1wAJju+UPieNQ+EbhNSmbTWmBNXM5sU
         J2Cv8XaS8/YyiQSPXA5S3YywPa5/cMjl7SAzrOEQ8ip6XqJesJvfnl13v0WlIzMdL/ez
         IXvZclHuKUYeWf/2FiJKac5fvfAe9lSNVhiZUMntOO9C6SZ4FaUNYCXPhCP4nDaKdlbw
         1HPO4L9K0DbpUgrULwuyxrn6/c74+aBWp3zQVKGUR8WkAQNFbK2WczieoC37oQb97++t
         LLEag0/fbqFlHBbe6RDW9nu4DV+xdygpzVia0ZtpunlOQNMzHdqMtr2cCCcTlv9yZrq7
         TQjg==
ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post
         :list-id:mailing-list:precedence:date:message-id:mime-version
         :reply-to:from:subject:to;
        bh=Ibrhyqugys65Y8gzuSGloywNGoGY735hgcq1co4q+S8=;
        b=tl6rdsQkSCuMrMzwtYjrFqLxOs4ULy4avLhUfo4X0oA4qBh0jr4scaIhqnavMVsFf9
         9+5oojLOdX+N0ERH4NiTRXjxZIDaDPaCFnpPWcLUVtvri2mj6oAZwutpqXeXDnmOSWG3
         jIza7m36fEpwzttv+dsY53Prz4dSuNRvSwMfpKqDbZNEvraAJDHGJHkEWeCviXsfPEeD
         fqJqs1KaTnoL34EXbbl6V+US0yGeZ8+aYluqjmMvuAwIHf+YUfXJGEvToHI5ePFbFMRA
         b2MHypEDhsV1IH8e+bExAN5fotOa+Z6fPWCgwIkzD6tyDuhUlBgvppowz22wtFjt+8pO
         3/Lg==
ARC-Authentication-Results: i=2; mx.google.com;
       spf=pass (google.com: example of our_account@our_shared_server designates 81.88.62.181 as permitted sender) smtp.mailfrom=our_account@our_shared_server
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:to:subject:from:reply-to:mime-version:message-id
         :date:x-original-sender:x-original-authentication-results:precedence
         :mailing-list:list-id:x-spam-checked-in-group:list-post:list-help
         :list-archive:list-subscribe:list-unsubscribe;
        bh=Ibrhyqugys65Y8gzuSGloywNGoGY735hgcq1co4q+S8=;
        b=hvY5DqV35j9YgMHtcIWa/jwiBl3ymOb73MLuD47HnYCV4sQuixJDn2dF/10Nbgf+tE
         fLZMlODwKxQdl7CWeTfE8qQ63y3ipPK3I0JrG8wE9R0NrJfgTbgbh/loqWl5azT7DtS3
         SC3ArO+c6qgWlvUAocV8uIHmj6cYdRzb4PnopqUlAu85Gl3pjZ/zvIQkUwVjZeWwX74Y
         TWBQARVdGffN64l0DCYVuncsiF8wm5Mvc+A9gJCX7BSU2BSCNfAnBIp8Eu4YtTDnfdkl
         JIRO8O5RLCCMoNQxGuzWwKD+uRY0aJ2R2p9x9JPTbY5EPqgzBLPRpaZaWdNOx0eV4Yee
         JWkA==
X-Gm-Message-State: APjAAAUMKCJmaOwi4KPGSc+OEF7i6wHpGyVCQMH04KnB1oITD1IWmx51 Tt3YJZ970VySfVgslwxfUWrOolNg
X-Google-Smtp-Source: APXvYqxBgjxy7ZmTX8y13XVszPIbuCpHbuqu/FSXEaDemu50dSm0jsNbiWyaWNQ3RWFbr6h8zbbQZQ==
X-Received: by 2002:a7b:c7c7:: with SMTP id z7mr3057676wmk.133.1573555236294;
        Tue, 12 Nov 2019 02:40:36 -0800 (PST)
X-BeenThere: sales@example.com
Received: by 2002:adf:f74c:: with SMTP id z12ls1801906wrp.10.gmail; Tue, 12 Nov 2019 02:40:35 -0800 (PST)
X-Received: by 2002:a5d:4b05:: with SMTP id v5mr4137078wrq.210.1573555235752;
        Tue, 12 Nov 2019 02:40:35 -0800 (PST)
ARC-Seal: i=1; a=rsa-sha256; t=1573555235; cv=none;
        d=google.com; s=arc-20160816;
        b=y1naNum4xW3tsMG3wPcDM1RwyX2OQ3SaChp7jlb3ZW4oGXExBL3kKLhixVT1Z9DAri
         +UnJVlnpeoh0Kx4v9sQB9ddhxCPPQlIRCHKWhtIr2zCrsItX3ODh2Rb/8JfxIW9E4FbH
         yba1H/TzGIcdMZXPQ38FVQwq1Q38FPpFs1+EfHV+Lubpkcrfuyy2gWKgi3cub9kl/laz
         9w42Y//Y8M5KUR4VHnAMcuVh5sQxad/26Tp220QtQiZgxYTqdZZifjQd1g7hT+msups7
         Q+voJdIwBuYaQ9T3KMW9lUM88wbNnKCBdZ4EShX8kKPJj52aZhKph05OhppvEOTbhs+G
         z1xQ==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=date:message-id:mime-version:reply-to:from:subject:to;
        bh=daDFaRYMOJpKRlRfbGKf2aIGxQj8IKYJpLd93m4btCM=;
        b=a504IJnGdbjiRO+veK8xPAMZrl8foX/NAew/pNX/OsGhYzSlgc+jy41vYemo1CXPom
         LzLzsj7ploMCdcKGMih0s88gWDJ127AUktuYYto6nZ4EzCQxebOsSNf4hkWFeQQaq5cK
         TG8PJYP+B+rXJn4GqPjGY12Uee8Fad7cfG9qShnmbvtjKgKb5MUyApi1pwNNX5IZUP2Y
         r6jR2WFteD4u9vfovY7eqxsims/RjtqgFq/cTkzssjrelg21UPb0opQvYWectewJ5cY1
         xjArgLix9Kfu+Yxc4UN+rAtosfjVN/h6NMDDX2bQJf2FK5mIZ3kfAQCayKaEdE51Lrys
         B8hQ==
ARC-Authentication-Results: i=1; mx.google.com;
       spf=pass (google.com: example of our_account@our_shared_server designates 81.88.62.181 as permitted sender) smtp.mailfrom=our_account@our_shared_server
Received: from hostingsmtp.register.it (hostingsmtp92.register.it. [81.88.62.181])
        by mx.google.com with ESMTPS id y6si2240288wmc.143.2019.11.12.02.40.35
        for <sales@example.com>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Tue, 12 Nov 2019 02:40:35 -0800 (PST)
Received-SPF: pass (google.com: example of our_account@our_shared_server designates 81.88.62.181 as permitted sender) client-ip=81.88.62.181;
Received: from private ([172.22.16.122]) by cmsmtp with ESMTP id UTb9iOMwlYLluUTb9iEdpb; Tue, 12 Nov 2019 11:40:35 +0100
Received: from our_account by our_shared_server with local (Exim 4.92) (envelope-from <our_account@our_shared_server>) id 1iUTb9-002mpw-8E; Tue, 12 Nov 2019 11:40:35 +0100
X-RID-REQUEST_URI: www.example.com/beta/admin/sales/invoice.php?id=IE0811190629
X-RID-HTTP_HOST: www.example.com
To: customer@gmail.com
Subject: New Invoice from example.
X-PHP-Script: www.example.com/beta/admin/sales/invoice.php for 86.45.249.162
X-PHP-Filename: /home/our_account/public_html/beta/admin/sales/invoice.php REMOTE_ADDR: 86.45.249.162
From: support@example.com
Reply-To: support@example.com
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="25a0b6981ab0acdbcaacc68fe4994b02"
Message-Id: <E1iUTb9-002mpw-8E@our_shared_server>
Date: Tue, 12 Nov 2019 11:40:35 +0100
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
X-AntiAbuse: Primary Hostname - our_shared_server
X-AntiAbuse: Original example - www.example.com
X-AntiAbuse: Originator/Caller UID/GID - [754 497] / [47 12]
X-AntiAbuse: Sender Address example - our_shared_server
X-Get-Message-Sender-Via: our_shared_server: authenticated_id: our_account/from_h
X-Source: 
X-Source-Args: 
X-Source-Dir: example.com:/public_html/beta/admin/sales
X-CMAE-Envelope: MS4wfL0gy4GxoHsyyUQGrMTaqxh/6qCROIdJJMpuX7KJAKp3suZnPt0rsgCiXoQPZ1cq9Y2VDDMNcE0sFQm+GgTA/9bmUdtOITTjkTzncjKCr+IeRb6W1yGf nX7zxY3z/ZDx28i7T84m7pT1xHK6s+o8lGkfAsN9hN9aJyj4O/x33VVQVW547IvJyrebTAXh2xB2NQ2sls52aOBBV6a7vb2bORQ=
X-Original-Sender: support@example.com
X-Original-Authentication-Results: mx.google.com;
       spf=pass (google.com: example of our_account@our_shared_server designates 81.88.62.181 as permitted sender) smtp.mailfrom=our_account@our_shared_server
Precedence: list
Mailing-list: list sales@example.com; contact sales+owners@example.com
List-ID: <sales.example.com>
X-Spam-Checked-In-Group: sales@example.com
X-Google-Group-Id: 751537614565
List-Post: <https://groups.google.com/a/example.com/group/sales/post>, <mailto:sales@example.com>
List-Help: <https://support.google.com/a/example.com/bin/topic.py?topic=25838>, <mailto:sales+help@example.com>
List-Archive: <https://groups.google.com/a/example.com/group/sales/>
List-Subscribe: <https://groups.google.com/a/example.com/group/sales/subscribe>, <mailto:sales+subscribe@example.com>
List-Unsubscribe: <mailto:googlegroups-manage+751537614565+unsubscribe@googlegroups.com>, <https://groups.google.com/a/example.com/group/sales/subscribe>

--25a0b6981ab0acdbcaacc68fe4994b02
Content-type: text/html; charset=UTF-8
<!DOCTYPE html><html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /></head><body style='background-color: #ddd;color:#333'><div style='width:550px;margin:auto;background-color:#fff;'><div style='text-align:center;width:500px;margin:auto;background-color:#fff;padding-left:25px;padding-right:25px;'><a class='logo' href='https://example.com/'><img src='https://example.com/media/example_logo_bg.png' alt='example.com' border='0' /></a></div><div style='width:450px;margin:auto;background-color:#fff;padding-left:25px;padding-right:25px;vertical-align:top;'>Paddy Hallihan,<br /><br />Please see below a link to your invoice.<br /><br /><a href='https://www.example.com/docs/invoices/IE0811190629.pdf'>https://www.example.com/docs/invoices/IE0811190629.pdf</a><br /><br />If you have any questions or if there is anything more we can do for you please let us know.</div><script type="application/json+trustpilot">{"recipientName": "Paddy&nbsp;Hallihan","recipientEmail": "phallihan@gmail.com","referenceId": "IE0811190629","products": [{"productUrl": "https://www.example.com/product/test1/1","imageUrl": "https://www.example.com/media/product_images/test1_0.jpg","name": "A2 - Quality Test Product - Not For Sale","sku": "402",},]}</script><div style='text-align:center;width:500px;margin:auto;background-color:#fff;padding:25px;'><span style='display:inline-block;text-align:center;vertical-align:top;'><img src='https://example.com/media/icons/phone.png' width='50px' height='50px'><br>International: <a href='tel:+35319012030'>+353 1 901 2030</a><br>United Kingdom: <a href='tel:+442036422217'>+44 20 3642 2217</a><br>United States: <a href='tel:+442036422217'>+1 888 213 8714</a></span><span style='display:inline-block;text-align:center;vertical-align:top;'><img src='https://example.com/media/icons/message.png' width='50px' height='50px'><br>Sales: <a href='mailto:sales@example.com' target='_blank'>sales@example.com</a><br>Support: <a href='mailto:support@example.com' target='_blank'>support@example.com</a></span></div></div></body></html>


--25a0b6981ab0acdbcaacc68fe4994b02--

我知道有 phpmailer 之类的工具,但我想弄清楚这一点,以便了解发生了什么。

编辑

这里有更多信息:

  • 我有几个不同的区域发送电子邮件,但页眉和页脚始终相同,只是电子邮件的正文发生了变化,因此我有$message .= $message_header.$message_content.$message_footer . "\r\n\r\n";

  • 我还使用gettext (https://www.gnu.org/software/gettext/) 翻译消息内容变量。这些翻译很好,我可以在收到的空白电子邮件的邮件标题中看到它。

  • 就像我说的,我从我们的系统发送了几封电子邮件,有问题的都来自我的管理子目录,但是,我还有一个组件子目录,其中包含一个邮件列表注册组件在我的主目录中间接(包含在另一个包含的文件中),这工作正常。

编辑 2

我通过删除以下几行解决了一半

$message = "--".$uid."\r\n";
$message .= "Content-type:text/html; charset=UTF-8" . "\r\n";

通过改变:

$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n";

到:

$headers .= "Content-Type: text/html;charset=UTF-8" . "\r\n";

我之所以有这个首先是因为我之前一直在添加附件。

$message .= $message_header.$message_content.$trustpilot_script.$message_footer . "\r\n\r\n";

// $message .= "--".$uid."\r\n";
// $message .= "Content-Type: application/octet-stream; name=\"".$file_name.".pdf\"\r\n";
// $message .= "Content-Transfer-Encoding: base64" . "\r\n";
// $message .= "Content-Disposition: attachment; filename=\"".$file_name.".pdf\"\r\n";
// $message .= $content."\r\n";
// $message .= "--".$uid."--";

mail($to, $subject, $message, $headers);

我只是没有在我的原始问题中包含注释掉的代码部分,但是有没有办法让它也能够发送附件?

【问题讨论】:

  • 我认为问题可能与您在消息中的内容有关
  • @Ingus,我认为你是对的,我将 $message .= $message_header.$message_content.$trustpilot_script.$message_footer . "\r\n\r\n"; 替换为 $message .= "test" . "\r\n\r\n"; 似乎没问题,你知道从你在可能导致这种情况的消息头?

标签: php email html-email


【解决方案1】:

我不知道你为什么会得到这个。 但我以这种方式做了同样的事情,希望你可能需要这个。

if(isset($_POST['submit'])){
    $to = 'email';
    $subject = 'Subject';
    $message = $_POST['message'];
    $header = "From " . ' <' . $email . '>';
    if (mail($to, $subject, $message, $header)) {
        echo 'Thank You For Your Mail';
     }else{
        echo 'Sorry Can Not Sent Your Mail';
      }

}

【讨论】:

  • 感谢您的评论,但我认为这不是问题所在,我的实际代码还有很多工作要做,它包含在 if() 条件中,并进行了一些检查以查看我的变量是否被设置。另外,mail() 函数上的 if()else() 并不是真正必要的,因为我知道它正在发送,并且您的 echo 无论如何都会给出相同的输出,我唯一注意到的不同之处是 周围的大括号但这不应该有所作为
  • 尝试使用不同的服务器。