How to send to BCC address when using PHPMailer to format MIME message for Gmail API?

PHPMailer will track the BCC recipients internally and if you were to send the message with PHPMailer it would specify the BCC recipients during the SMTP envelope.

However, when you extract the raw message from PHPMailer you lose the internal recipient list that PHPMailer was tracking. The raw message does not include the BCC information. The To: and Cc: headers will include the appropriate recipients and the GMAIL API probably uses these headers to infer the intended recipients.

To add in the BCC recipients you will need to use the GMAIL API to add these recipients before sending the message.

You didn’t provide your GMAIL API code but it might follow this outline:

$message = new Message();

# construct message using raw data from PHPMailer
$message->setSubjectBody(...);
$message->setTextBody(...);
$message->setHtmlBody(...);

# *** add the BCC recipients here ***
$message->addBcc("[email protected]");

# send the message
$message->send();

Leave a Comment

tech