本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

使用PHPMailer格式化Gmail API的MIME消息时,如何发送到密件抄送地址?

发布于2021-04-13 23:14     阅读(374)     评论(0)     点赞(20)     收藏(4)


我正在使用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地址。

当我使用gmail Web界面发送电子邮件并在其中提供“密件抄送”地址时,在发送的邮件标头中确实看到了“未公开的收件人”,并且邮件传输到密件抄送地址。

有人知道此问题的解决方法吗?


解决方案


PHPMailer会在内部跟踪BCC收件人,如果您要使用PHPMailer发送邮件,它将在SMTP信封中指定BCC收件人

但是,当您从PHPMailer提取原始消息时,您将丢失PHPMailer跟踪的内部收件人列表。原始消息不包括BCC信息。To:Cc:标题将包括相应的接收者和GMAIL API可能使用这些标题来推断预期的收件人。

要添加密件抄送收件人,您需要在发送邮件之前使用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();


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.phpheidong.com/blog/article/39599/8b3b1f1a3c261bdd3b97/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

20 0
收藏该文
已收藏

评论内容:(最多支持255个字符)