将密件抄送添加到使用 Gmail API 和 PHP 发送的电子邮件中

Add Bcc to email sent with Gmail API with PHP

所以,我遵循的一切都是这个 guide,是的,我能够发送电子邮件,但我正在尝试添加密件抄送电子邮件,因为它的列表很长,我不希望他们这样做显示在电子邮件的收件人列表中。

用 PHPMailer 发送时一切正常,这个 似乎可以解决我的问题,但尽管我已经尝试了很多,但我还没有找到答案。

我现在的表现如何:

$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<jlcampost@concredito.com.mx>\r\n";
$strRawMessage .= "To: Metro Gio <greyeso@concredito.com.mx>\r\n";
$strRawMessage .= "Bcc: <cjlindorv@concredito.com.mx>,<cfgonzalezr@concredito.com.mx>,<jlcampost@concredito.com.mx>\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);

电子邮件确实已发送,每个收件人都收到了电子邮件,但密件抄送在电子邮件收件人列表中显示为:

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

当我使用 PHPMailer 通过 SMTP 发送,并使用 email->AddBCC() 方法时,在电子邮件中只显示在 $email->AddAddress() 方法中添加的地址:

para Metro

我想做同样的事情,但是对于 Gmail API,就像我说的,这个 似乎有答案,但我需要更多信息。

他说:

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

我认为他正在谈论的 Message() class 应该是 Google_Service_Gmail_Message(),但它不包含此类方法,这就是我遗漏的地方。

有人可以帮忙吗?

对于其他认为自己面临同样问题的人,让我告诉你没有问题,一切正常。

代码可以正常工作,但是如果你像我一样做,并且你正在使用你的 Gmail 帐户发送电子邮件并将你自己包括在收件人中,当你收到电子邮件时,你可以看到密件抄送列表为该电子邮件来自您的帐户。

我是在@ficuscr 在评论中问了他的问题后才弄明白的,我去确认 "to" 收件人是否可以看到 Bcc 收件人,他看不到,其他 Bcc 也没有收件人。

我希望这可以帮助某人节省几个小时的研究时间。