ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-07-14 15:30:30  阅读:275  来源: 互联网

标签:php mime gmail phpmailer 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();

标签:php,mime,gmail,phpmailer,bcc
来源: https://codeday.me/bug/20190714/1459156.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有