Search code examples
phpemailgmail-apibcc

Add Bcc to email sent with Gmail API with PHP


So, I followed everything is this guide, and yes, I was able to send emails, but I'm trying to add Bcc emails, because its a very long list and I dont want them to show in the email's recipients list.

When sending with PHPMailer everything is fine, this question seems to have the answer to my problem, but as much as I have tried, I haven't found an answer.

How I'm doing it right now:

$client = getClient();
if (is_string($client)){
    exit ($client);
}
$service = new Google_Service_Gmail($client);
$user = 'me';

$strSubject = 'Correo GMail API' . date('M d, Y h:i:s A');
$strRawMessage = "From: Lauro Campos<[email protected]>\r\n";
$strRawMessage .= "To: Metro Gio <[email protected]>\r\n";
$strRawMessage .= "Bcc: <[email protected]>,<[email protected]>,<[email protected]>\r\n";
$strRawMessage .= 'Subject: =?utf-8?B?' . base64_encode($strSubject) . "?=\r\n";
$strRawMessage .= "MIME-Version: 1.0\r\n";
$strRawMessage .= "Content-Type: text/html; charset=utf-8\r\n";
$strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
$strRawMessage .= "mensaje <b>de prueba!\r\n";
// The message needs to be encoded in Base64URL
$mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
$msg = new Google_Service_Gmail_Message();
$msg->setRaw($mime);
//The special value **me** can be used to indicate the authenticated user.
$service->users_messages->send("me", $msg);

The email does get sent and every recipient gets the email, BUT Bcc are shown in the email recipient list as:

para Metro, bcc: cjlindorv, bcc: cfgonzalezr, bcc: mí

When I use PHPMailer sending through SMTP, and use email->AddBCC() method, in the email only the address added in $email->AddAddress() method is shown:

para Metro

I want to do the same but with Gmail API, like I said, this question seems to have the answer, but I need more information.

He says:

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();

I would think that the Message() class he is talking about would be Google_Service_Gmail_Message(), but it doesn't contain such methods, this is where I'm missing something.

Can anyone help please?


Solution

  • To anyone else who thinks he is facing the same problem, just let me tell you there is no problem, everything is working fine.

    The code works properly, BUT if you are doing like I did, and you are using your Gmail account to send the emails and including yourself in the recipients, when you get the email, you can see the Bcc list as the email came from your account.

    I only figured this out after @ficuscr asked his question in the comments and I went to confirm with the "to" recipient if he could see the Bcc recipients, he couldn't, nor did the other Bcc recipients.

    I hope this helps someone to save a few hours of research.