4
votes

How can I send email with BCC via GMAIL API? I send emails to TO or CC but BCC doesn't work. I use Base64.urlsafe_encode64(email.to_s)and this code create string without BCC. My working code example:

    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)
        },
    }

Do I have to call again GMAIL API and send this email with thread id and BCC as TO? I use google-api-client 0.7.1

EDIT: Mail object:

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

Mail object with to_s:

"Date: Tue, 01 Dec 2015 14:09:08 +0100\r\n
To: my_email <[email protected]>\r\n
Cc: my_email <[email protected]>\r\n
Message-ID: <[email protected]>\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
Could you show us how email.to_s looks before encoding it? - Tholle
@Tholle yes, I added it to question. Is it ok? - quatermain
It seems like your <Bcc: ["[email protected]"]> in the Mail object is wrong. Try <Bcc: ["<[email protected]>"]> instead. - Tholle
it's same => #<Mail::Message:70354473411500, Multipart: true, Headers: <Date: Tue, 01 Dec 2015 16:29:10 +0100>, <Reply-To: >, <To: ["Kriska Quatermain <[email protected]>"]>, <Cc: >, <Bcc: ["[email protected] <[email protected]>"]>, <Subject: test bad bcc 2>, <Content-Type: multipart/mixed>> [2] pry(#<GoogleApi::Gmail::Message>)> email.bcc => ["[email protected]"] [3] pry(#<GoogleApi::Gmail::Message>)> email.to => ["[email protected]"] - quatermain
nope. It's same. I read it's because email protocol doesn't allow to show bcc in raw email and this is raw email. So I can do second request to google api, but I think it's the last option. Thank you - quatermain

1 Answers

6
votes

You have to manually add the bcc header to the email, it will not be sent to the recipients. Same as gmail-ruby-api does it 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 ...