php 使用PHPMailer为Gmail API格式化MIME消息时如何发送到BCC地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28779854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send to BCC address when using PHPMailer to format MIME message for Gmail API?
提问by cloudxix
I am using PHPMailer to build an email message. I am using PHPMailer only for MIME message formatting, not sending.
我正在使用 PHPMailer 来构建电子邮件。我仅将 PHPMailer 用于 MIME 消息格式,而不是发送。
I then extract the raw message from the PHPMailer object before passing it on to the Gmail API for processing.
然后我从 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("[email protected]", "From Name");
//Set an alternative reply-to address
$mail->addReplyTo("[email protected]", "Reply Name");
//Set to address
$mail->addAddress("[email protected]", "Some Name");
//Set CC address
$mail->addCC("[email protected]", "Some CC Name");
//Set BCC address
$mail->addBCC("[email protected]", "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);
According to PHPMailer docs, CC and BCC only function for sending in the Win32 environment.
根据 PHPMailer 文档,CC 和 BCC 仅用于在 Win32 环境中发送。
However, my MIME formatted messages transmit successfully via the Gmail API to the "TO" and "CC" addresses, but not the "BCC" address.
但是,我的 MIME 格式的邮件通过 Gmail API 成功传输到“TO”和“CC”地址,而不是“BCC”地址。
To summarize, When I send email using this codeand I provide a 'BCC' address to the Gmail API, I do notsee 'undisclosed-recipients' in the sent message header, and the message is nottransmitted to the BCC address.
总之,当我用发送电子邮件的代码,我提供了一个“密件抄送”地址Gmail的API,我没有看到在发送的邮件标题“未公开的收件人”,消息不发送到BCC地址。
When I send email using the gmail web interfaceand I provide a 'BCC' address there, I dosee 'undisclosed-recipients' in the sent message header, and the message istransmitted to the BCC address.
当我使用gmail 网络界面发送电子邮件并在那里提供“密件抄送”地址时,我确实在发送的邮件标题中看到了“未公开的收件人”,并且邮件会传输到密件抄送地址。
Does anyone know of a workaround for this issue?
有没有人知道这个问题的解决方法?
回答by MCToon
PHPMailer will track the BCC recipients internally and if you were to send the message with PHPMailer it would specify the BCC recipients during the SMTP envelope.
PHPMailer 将在内部跟踪 BCC 收件人,如果您使用 PHPMailer 发送邮件,它将在SMTP 信封期间指定 BCC 收件人。
However, when you extract the raw message from PHPMailer you lose the internal recipient list that PHPMailer was tracking. The raw messagedoes not include the BCC information. The To:
and Cc:
headers will include the appropriate recipients and the GMAIL API probably uses these headers to infer the intended recipients.
但是,当您从 PHPMailer 提取原始邮件时,您将丢失 PHPMailer 正在跟踪的内部收件人列表。该原始消息不包括BCC信息。在To:
和Cc:
标题将包括相应的接收者和GMAIL API可能使用这些标题来推断预期的收件人。
To add in the BCC recipients you will need to use the GMAIL API to add these recipients before sending the message.
要添加密件抄送收件人,您需要在发送邮件之前使用 GMAIL API 添加这些收件人。
You didn't provide your GMAIL API code but it might follow this outline:
您没有提供 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("[email protected]");
# send the message
$message->send();
回答by user12220234
回答by Matheus Valin
To anyone who find this question but it not using the Gmail api to send it, only using the PhpMailer to build a raw MIME message:
对于发现此问题但不使用 Gmail api 发送它,仅使用 PhpMailer 构建原始 MIME 消息的任何人:
if you set $phpMailer->isMail() (yes that's a setter) it will include the BCC: in the raw MIME message.
如果您设置 $phpMailer->isMail() (是的,这是一个设置器),它将在原始 MIME 消息中包含 BCC: 。
I suppose it makes no difference if the phpMailer object is set to SMTP or mail method since you'll not use it to actually send the email.
我想如果 phpMailer 对象设置为 SMTP 或 mail 方法没有区别,因为您不会使用它来实际发送电子邮件。