如何通过GMAIL API发送带有BCC标题的电子邮件?

时间:2015-11-30 15:28:10

标签: ruby-on-rails ruby gmail gmail-api google-api-client

如何通过GMAIL API发送带有BCC的电子邮件?我发送电子邮件给TO或CC,但BCC不起作用。我使用Base64.urlsafe_encode64(email.to_s)并且此代码创建没有BCC的字符串。我的工作代码示例:

    email = Mail.new
    email.date = Time.now
    email.subject = subject
    email.to = email_array_to_email_to(to)
    email.cc = email_array_to_email_to(cc)
    email.bcc = email_array_to_email_to(bcc)
    email.reply_to = email_array_to_email_to(reply_to)
    email.html_part do
      body message
    end
    request = {
        api_method: @google_api.users.messages.to_h['gmail.users.messages.send'],
        parameters: { userId: 'me' },
        body_object: {
            raw: Base64.urlsafe_encode64(email.to_s)
        },
    }

我是否必须再次拨打GMAIL API并发送此电子邮件,其中包含主题ID和BCC为TO? 我使用google-api-client 0.7.1

编辑: 邮件对象:

#<Mail::Message:70336725981360,
Multipart: true,
Headers: <Date: Tue,
01 Dec 2015 14:09:08 +0100>,
<Reply-To: >,
<To: ["quatermain32 <my_email@gmail.com>"]>,
<Cc: ["quatermain32 <my_email@gmail.com>"]>,
<Bcc: ["my_email@gmail.com"]>,
<Subject: Test subject>,
<Content-Type: multipart/mixed>>

to_s的邮件对象:

"Date: Tue, 01 Dec 2015 14:09:08 +0100\r\n
To: my_email <my_email@gmail.com>\r\n
Cc: my_email <my_email@gmail.com>\r\n
Message-ID: <565d9c6e3cf0b_058b7@Olivers-MacBook-Pro.local.mail>\r\n
Subject: Test subject\r\n
Mime-Version: 1.0\r\n
Content-Type: multipart/mixed;\r\n
 boundary=\"--==_mimepart_565d9bf468e77_cb0d35e200577a\";\r\n
 charset=UTF-8\r\n
 Content-Transfer-Encoding: 7bit\r\n
\r\n
\r\n
----==_mimepart_565d9bf468e77_cb0d3ff88645e200577a\r\n
Content-Type: text/html;\r\n
 charset=UTF-8\r\n
 Content-Transfer-Encoding: 7bit\r\n
\r\n
<p>Test content</p>\r\n
----==_mimepart_565d9bf468e77_cb0d3ff88645e200577a--\r\n
"

1 个答案:

答案 0 :(得分:3)

您必须手动将bcc标头添加到电子邮件中,它不会发送给收件人。与gmail-ruby-api相同,https://github.com/jhk753/gmail-ruby-api/blob/e0d62a751bc31397926c5800532f26e185e00b16/lib/gmail/message.rb

encoded = mail.encoded
if bcc = mail.bcc.join(",").presence
  encoded.prepend "Bcc: #{bcc}\n"
end
... send email ...