15

I am using PHPMailer to build an email message. I am using PHPMailer only for MIME message formatting, not sending.

I then extract the raw message from the PHPMailer object before passing it on to the Gmail API for processing.

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();
$mail->IsHTML(true);

//Disable SMTP debugging
// 0 = off (for production use)
$mail->SMTPDebug = 0;

//Set who the message is to be sent from
$mail->setFrom("fromaddress@domain.com", "From Name");

//Set an alternative reply-to address
$mail->addReplyTo("replyaddress@domain.com", "Reply Name");

//Set to address
$mail->addAddress("address@domain.com", "Some Name");

//Set CC address
$mail->addCC("ccaddress@ccdomain.com", "Some CC Name");

//Set BCC address
$mail->addBCC("bccaddress@ccdomain.com", "Some BCC Name");

//Set the subject line
$mail->Subject = "Test message";

//Set the body
$mail->Body = file_get_contents("/messagestore/some.html");

//Attach a file
$mail->addAttachment("/messagestore/some.pdf","some.pdf","base64","application/pdf");

//generate mime message
$mail->preSend();

//get the mime text
$mime = $mail->getSentMIMEMessage();

//do the google API dance
$newMailMessage = new Google_Service_Gmail_Message();
$data = base64_encode($mime);
$data = str_replace(array('+','/','='),array('-','_',''),$data); // url safe
$newMailMessage->setRaw($data);
$gmailService = new Google_Service_Gmail($google_client);
$gmailService->users_messages->send('me', $newMailMessage);

According to PHPMailer docs, CC and BCC only function for sending in the Win32 environment.

However, my MIME formatted messages transmit successfully via the Gmail API to the "TO" and "CC" addresses, but not the "BCC" address.

To summarize, When I send email using this code and I provide a 'BCC' address to the Gmail API, I do not see 'undisclosed-recipients' in the sent message header, and the message is not transmitted to the BCC address.

When I send email using the gmail web interface and I provide a 'BCC' address there, I do see 'undisclosed-recipients' in the sent message header, and the message is transmitted to the BCC address.

Does anyone know of a workaround for this issue?

cloudxix
  • 416
  • 1
  • 6
  • 11
  • 1
    from what I know, BCC address won't be in the sent message's raw header. By that recipients won't see the address in BCC when they got the mail – Hoàng Long Feb 28 '15 at 08:52
  • Yes, but in this case the TO and CC transmit successfully while the BCC doesn't. – cloudxix Feb 28 '15 at 09:03
  • It's strange then. I think "To: undisclosed recipients" is NOT part of the protocol, usually the mail server auto put it there if there is nothing in the TO and CC field. But you have addresses in the To and Cc field, so it will not appear. Have you check the spam folder of your bcc recipient's address? – Hoàng Long Feb 28 '15 at 09:15
  • 1
    I am in development so I checked the BCC recipient email account and the message was not there. I am testing with a 'free' google apps for business account. the production account is a paid account. i suspect that perhaps google will not let me send BCC email from a free account. I will test and report back – cloudxix Feb 28 '15 at 17:44
  • The 'undisclosed-recipients' string is an entirely arbitrary name for an empty address group. It has no particular meaning. – Synchro Feb 28 '15 at 19:20
  • In this context, I believe that 'undisclosed-recipients' indicates whether or not Gmail processed a BCC address element in an email message. – cloudxix Mar 01 '15 at 10:21

3 Answers3

38

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("secret.recipient@google.com");

# send the message
$message->send();
Community
  • 1
  • 1
MCToon
  • 638
  • 5
  • 10
  • 3
    Thanks! This is the solution. I don't have enough reputation to vote you up, but this solved my problem. – cloudxix Apr 17 '15 at 01:58
  • 7
    I now have enough reputation to vote you up - thanks again - still using this solution and it still works. – cloudxix Apr 11 '20 at 16:25
  • 4
    @cloudxix You came back 5 years later to thank him, you're a great person. – Alexis Philip Jul 30 '20 at 08:48
  • 1
    @AlexisPhilip I still use this code on a daily basis and remain grateful for a solution to a difficult problem. Now if I could only boot my iMac Pro into Linux. – cloudxix Jul 31 '20 at 13:07
5

To anyone who finds this question but is not using the Gmail api to send it, only using the PhpMailer to build a raw MIME message:

if you set $phpMailer->isMail() (yes that's a setter) it will include the BCC: in the raw MIME message.

I suppose it makes no difference if the phpMailer object is set to SMTP or mail method since you'll not use it to actually send the email.

Matheus Valin
  • 191
  • 2
  • 8
  • 3
    The original solution to this problem recently stopped working for me when using the Gmail API and PHPMailer. Thanks to your comment, my research determined that `->isMail()` must be added ***after*** `->isSMTP()` ***and*** `isHTML(true)`, or the Gmail API will not send a copy of the email to the BCC address. After I added `->isMail()`, BCC started working again with Gmail and PHPMailer. – cloudxix Apr 22 '21 at 22:20
2

Just add

$mail->addBCC('bcc@example.com');
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • 4
    This looks like something already covered in the accepted answer - is there anything new you want to add to that? – Nico Haase Oct 15 '19 at 12:01