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

Lau*_*182 1 php email bcc gmail-api

所以,我遵循了本指南的所有内容,是的,我能够发送电子邮件,但我正在尝试添加密件抄送电子邮件,因为它的列表很长,我不希望它们显示在电子邮件的收件人列表中。

使用 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);
Run Code Online (Sandbox Code Playgroud)

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

para Metro, bcc: cjlindorv, bcc: cfgonzalezr, bcc: mí
Run Code Online (Sandbox Code Playgroud)

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

para Metro
Run Code Online (Sandbox Code Playgroud)

我想做同样的事情,但使用 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();
Run Code Online (Sandbox Code Playgroud)

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

有人可以帮忙吗?

Lau*_*182 5

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

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

我是在@ficuscr 在评论中提出他的问题后才弄清楚的,我去与“to”收件人确认他是否可以看到密件抄送收件人,他看不到,其他密件抄送收件人也没有。

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