使用PHPMailer格式化Gmail API的MIME邮件时如何发送到BCC地址?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用PHPMailer格式化Gmail API的MIME邮件时如何发送到BCC地址?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 PHPMailer来构建邮件.我只使用PHPMailer进行MIME消息格式化,而不是发送.

然后我从PHPMailer对象中提取原始消息,然后将其传递给Gmail API进行处理.

//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("fromaddress@domain.COM","From Name");

//Set an alternative reply-to address
$mail->addReplyTo("replyaddress@domain.com","Reply Name");

//Set to address
$mail->addAddress("address@domain.com","Some Name");

//Set CC address
$mail->addCC("cCADdress@ccdomain.com","Some CC Name");

//Set BCC address
$mail->addBCC("bccaddress@ccdomain.com","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);

根据PHPMailer文档,CC和BCC仅用于在Win32环境中发送.

但是,我的MIME格式邮件通过Gmail API成功传输到“TO”和“CC”地址,而不是“BCC”地址.

总而言之,当我使用此代码发送电子邮件并向Gmail API提供“BCC”地址时,我在发送的邮件标题中看不到“未公开的收件人”,并且邮件未传输到BCC地址.

当我使用gmail web界面发送电子邮件并在那里提供“BCC”地址时,我确实在发送的邮件标题中看到“未公开的收件人”,并且邮件被发送到BCC地址.

有谁知道这个问题的解决方法

解决方法

PHPMailer将在内部跟踪BCC收件人,如果您要使用PHPMailer发送消息,它将在 SMTP envelope期间指定BCC收件人.

但是,当您从PHPMailer中提取原始消息时,您将丢失PHPMailer正在跟踪的内部收件人列表. raw message包括BCC信息. To:和Cc:标头将包含相应的收件人,GMAIL API可能使用这些标头来推断预期的收件人.

添加BCC收件人,您需要使用GMAIL API在发送邮件之前添加这些收件人.

您没有提供GMAIL API代码,但可能会遵循以下大纲:

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

脚本宝典总结

以上是脚本宝典为你收集整理的使用PHPMailer格式化Gmail API的MIME邮件时如何发送到BCC地址?全部内容,希望文章能够帮你解决使用PHPMailer格式化Gmail API的MIME邮件时如何发送到BCC地址?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。