将密件抄送添加到使用PHP的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>rn";
$strRawMessage .= "To: Metro Gio <greyeso@concredito.com.mx>rn";
$strRawMessage .= "Bcc: <cjlindorv@concredito.com.mx>,<cfgonzalezr@concredito.com.mx>,<jlcampost@concredito.com.mx>rn";
$strRawMessage .= 'Subject: =?utf-8?B?' . base64_encode($strSubject) . "?=rn";
$strRawMessage .= "MIME-Version: 1.0rn";
$strRawMessage .= "Content-Type: text/html; charset=utf-8rn";
$strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "rnrn";
$strRawMessage .= "mensaje <b>de prueba!rn";
// 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发送,并使用电子邮件->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((类应该是Google_Service_Gmail_Message((,但它不包含这样的方法,这就是我缺少的地方。

有人能帮忙吗?

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

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

我是在@ficuscr在评论中问了他的问题后才知道这一点的,我去和"收件人"确认他是否能看到密件抄送收件人,他不能,其他密件抄送的收件人也不能。

我希望这能帮助人们节省几个小时的研究时间。

最新更新