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 <[email protected]>
Cc: "Rec2 ([email protected])" <[email protected]>
Bcc: [email protected]
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"
  }
}
5
  • 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, 2021 at 9: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, 2021 at 17:00
  • 1
    PHPMailer will automatically add To: undisclosed-recipients:; is AddAddress() has never been called
    – Dude
    Sep 21, 2021 at 20:07
  • Looks like a bug in gmail to me.
    – Synchro
    Sep 22, 2021 at 10:17
  • Any idea how to report this officially?
    – Dude
    Sep 22, 2021 at 14:47

1 Answer 1

0

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

$mail->AddAddress("undisclosed-recipients:;");
$mail->AddBCC("[email protected]");

Worked on this post:

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

5
  • 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, 2021 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, 2021 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, 2021 at 23:14
  • @Dude, it's very unfortunate if that's the case then.
    – NightEye
    Sep 21, 2021 at 23:28
  • @Dude, you can kindly file it at issuetracker.google.com
    – NightEye
    Sep 24, 2021 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.