0

I'm trying to send a simple email via the Gmail API that's failing because of the RFC822-compliant To: undisclosed-recipients:; header. Sending with a regular address in the To header works fine. The RFC822 message is created by PHPMailer and recovered using preSend() and getSentMIMEMessage().

What am I doing wrong?

The message

Date: Tue, 21 Sep 2021 09:13:17 +0000
From: Rec1 <fictive1@gmail.com>
Cc: "Rec2 (fictive2@gmail.com)" <fictive2@gmail.com>
Bcc: fictive3@gmail.com
Message-ID: <vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg@vps>
X-Mailer: PHPMailer 6.5.1 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: multipart/alternative;
 boundary="b1_vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg"
Content-Transfer-Encoding: 8bit
To: undisclosed-recipients:;
Subject: test

This is a multi-part message in MIME format.

--b1_vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg
Content-Type: text/plain; charset=us-ascii

body

--b1_vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg
Content-Type: text/html; charset=us-ascii

<div dir="ltr">body</div>


--b1_vysFE9wLykAQc73VcxjiTfjPaZQJ5ge7jXqWbNeg--

The answer

{
  "error": {
    "code": 400,
    "message": "Invalid To header",
    "errors": [
      {
        "message": "Invalid To header",
        "domain": "global",
        "reason": "invalidArgument"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}
Dude
  • 121
  • 8
  • Trying to actually send an email with no To addresses, a CC and a BCC address. Sending the same email from the mail.google.com web interface works fine and the original message reaches the destinations fine with the `To: undisclosed-recipients:;` header. – Dude Sep 21 '21 at 09:47
  • undisclosed recipients and BCC should basically be the same. Made it work with MailApp in apps script (which is basically similar to gmail api) with only BCC and no "To recipients". Header will show receiver's email as "to recipient" but only his/her email, not the others on the original bcc recipients. Is this not an option? – NightEye Sep 21 '21 at 17:00
  • 1
    PHPMailer will automatically add `To: undisclosed-recipients:;` is AddAddress() has never been called – Dude Sep 21 '21 at 20:07
  • Looks like a bug in gmail to me. – Synchro Sep 22 '21 at 10:17
  • Any idea how to report this officially? – Dude Sep 22 '21 at 14:47

1 Answers1

0

Try manually adding undisclosed-recipients:; then add bcc.

$mail->AddAddress("undisclosed-recipients:;");
$mail->AddBCC("username@domain.com");

Worked on this post:

You have different issues but it might also solve the issue regarding invalid to header if set manually.

NightEye
  • 10,634
  • 2
  • 5
  • 24
  • Tried that before posting the question and it doesn't work, just like the comments there state. `$mail->AddAddress("undisclosed-recipients:;");` returns false and the generated message contains the same To header that is not accepted by the Gmail API. – Dude Sep 21 '21 at 21:37
  • @Dude, If possible, can you try getting the mail with proper "to recipient" instead and modify that to mimic a properly sent email with only bcc and no "to recipient" in gmail? If that's possible. If it's me, that's where I would start. – NightEye Sep 21 '21 at 22:00
  • 1
    I tried and mail.gmail.com itself sends the email just like PHPMailer does - the To headers are identical, `To: undisclosed-recipients:;`. The same header is sent by them via their website, but not accepted via their API. – Dude Sep 21 '21 at 23:14
  • @Dude, it's very unfortunate if that's the case then. – NightEye Sep 21 '21 at 23:28
  • @Dude, you can kindly file it at https://issuetracker.google.com – NightEye Sep 24 '21 at 18:29