Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send gmail messages with google-api-ruby-client '0.9.pre3'

Working through sending gmail with the newer google-api-ruby-client in a rails 4 application.

require 'google/apis/gmail_v1'

Gmail = Google::Apis::GmailV1

class MailService

  def initialize(params)
    @params = params
  end

  def call
    message = Gmail::Message.new
    service = Gmail::GmailService.new
    message.raw = (redacted)
    service.request_options.authorization = current_user.token.fresh_token
    result = service.send_user_message(current_user.email, message)
  end

end

And this is the result from the call to the API:

Sending HTTP post https://www.googleapis.com/gmail/v1/users/me/messages/send?
200
#<Hurley::Response POST https://www.googleapis.com/gmail/v1/users/me/messages/send == 200 (63 bytes) 858ms>
Success - #<Google::Apis::GmailV1::Message:0x007fc9cf9b52dd
 @id="15096369c05cdb1d",
 @thread_id="15096369c05cdb1d">

The raw message sends without issue from the API explorer but when executed from my application I get a bounce email in my inbox. In the above example the redacted sample is a valid RFC 2822 formatted base-64 url safe string and fresh_token represents the oauth2 access token for the current user.

A look at the bounced mail

Bounce <[email protected]>
2:43 PM (19 minutes ago)
to me

An error occurred. Your message was not sent.

Anyone have any thoughts? It seems like perhaps my (sender) email is being picked up in the raw message but not the recipient... Though I suppose the API could be forwarding the bounce based on my oauth access token.

I very much appreciate any help. Thanks!

EDIT: Solution was to pass the RFC 2822 string as raw property without base64 encoding.

like image 365
RangerRanger Avatar asked Oct 30 '22 17:10

RangerRanger


1 Answers

Steve Bazyl seems to be correct. The documentation on send_user_message is wrong as of (0.9.13). For raw, it says: "The entire email message in an RFC 2822 formatted and base64url encoded string. Returned in messages.get and drafts.get responses when the format=RAW parameter is supplied. Corresponds to the JSON property raw." As far as I can tell, this is simply incorrect.

I encountered this issue when updating from google-api-client 0.8 to 0.9 and removing the base64 encoding solved the problem. I.e. call in 0.8:

response = @service.execute(
  api_method: api.users.messages.to_h['gmail.users.messages.send'],
  body_object: {
    raw: Base64.urlsafe_encode64(mail.to_s)
  },
  parameters: {
    userId: 'me',
  }
)

became

message = { raw: mail.to_s }
res = @service.send_user_message('me', message, {})

in 0.9.

Reported as https://github.com/google/google-api-ruby-client/issues/474.

like image 65
Bittrance Avatar answered Nov 15 '22 06:11

Bittrance