Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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("[email protected]", "From Name");

//Set an alternative reply-to address
$mail->addReplyTo("[email protected]", "Reply Name");

//Set to address
$mail->addAddress("[email protected]", "Some Name");

//Set CC address
$mail->addCC("[email protected]", "Some CC Name");

//Set BCC address
$mail->addBCC("[email protected]", "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?

like image 497
cloudxix Avatar asked Feb 28 '15 08:02

cloudxix


People also ask

How can I send multiple emails in PHPMailer?

SendMail() code tar. gz"); // add attachments $mail->AddAttachment("/tmp/image. jpg", "new. jpg"); // optional name $mail->IsHTML(true); // set email format to HTML $mail->Subject = "Here is the subject"; $mail->Body = "This is the HTML message body <b>in bold!

Why it is advantages to use PHPMailer for sending and receiving email?

PHPMailer can use a non-local mail server (SMTP) if you have authentication. Further advantages include: It can print various kinds of error messages in more than 40 languages when it fails to send an email. It has integrated SMTP protocol support and authentication over SSL and TLS.

Where do I put PHPMailer?

PHPMailer will be installed and you'll be ready to use it. Composer will generate an “autoload. php” file you can use to include the installed libraries, in this case PHPMailer. This file is located under the “vendor” directory by default, although you can configure Composer to use a different directory name.


1 Answers

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();
like image 107
MCToon Avatar answered Oct 19 '22 20:10

MCToon