Sending an email with ruby gmail api v0.9
Asked Answered
P

2

6

Does anyone have a simple example as to how to send an email from scratch with the v0.9 API.

simply want an example of sending the following:

m = Mail.new(
to: "[email protected]", 
from: "[email protected]", 
subject: "Test Subject",
body:"Test Body")

Now to create that message object which is required to send,we can use:

msg = Base64.urlsafe_encode64 m.to_s

And then try to send (where message_object = msg):

client = Google::Apis::GmailV1::GmailService.new #Appropriately authorised
client.send_user_message("me", message_object)

The client wants an RFC822 compatible encoded string, which the above should be.

I've tried:

message_object = msg
=> Google::Apis::ClientError: invalidArgument: 'raw' RFC822 payload message string or uploading message via /upload/* URL required
message_object = raw:msg
=>ArgumentError: unknown keyword: raw
message_object = {raw:msg}
=>ArgumentError: unknown keyword: raw 
message_object = Google::Apis::GmailV1::Message.new(raw:msg)
=> #<Google::Apis::GmailV1::Message:0x007f9158e5b4b0 @id="15800cd7178d69a4", @thread_id="15800cd7178d69a4">
#But then I get Bounce <[email protected]> - An error occurred. Your message was not sent.

i.e. None of them work...

Sending the basic encded string (msg above) through the Gmail API interface tester here works.

I'm obviously missing something obvious here as to how to construct that object required to make it work through the API.

Pyxidium answered 25/10, 2016 at 18:16 Comment(5)
What exactly does not work? What do you expect to happen, what does actually happen? Which exceptions do you get? When running which exact code? Please see stackoverflow.com/help/mcve on how to create a Minimal, Complete, and Verifiable example which helps others solve your problems.Fourinhand
The clue was in the name of the method. Send message is supposed to send a message. Without going into a very long and drawn out walkthrough of google's oauth process that example is about as clear as I can make it. I am using a method from google's ruby client library. The client library for the Gmail API. Gmail is an email service One of the purposes of email is to send and electronic communication between two parties. The example above is an attempt to send an electronic communication between two parties. Really not sure how much clearer I could have been...Pyxidium
What is missing form your question is still what exactly does not work. What happens when you run the code? Do you get an exception? Any other behavior? How do you know that "it doesn't work"? Please refer to the article I linked above on how to wrote a good question that allows others to help you..Fourinhand
The problem is that you don't understand the question. That's fine. I don't expect you to have familiarity with googles api client. But that's what the question is about. If you have a passing familiarity with google-api-ruby-client v0.9 then the question will make perfect sense to you. If you don't have a passing familiarity with it, it's not going to make any sense... That said, I'd happily take advice on what you think would make it a better question. Are you saying that you literally don't understand it?Pyxidium
How about that? Not sure I've added much to anyone who understands the api client, but is that clearer for you?Pyxidium
P
10

Ok. So the answer... thanks for all your help Holger... Was that the documentation is wrong. It asks you to encode to base64. The base64 encoding is not required (it is done internally by the api client).

The correct way to send is

msg = m.encoded 
# or m.to_s
# this doesn't base64 encode. It just turns the Mail::Message object into an appropriate string.

message_object = Google::Apis::GmailV1::Message.new(raw:m.to_s)
client.send_user_message("me", message_object)

Hope that saves someone else from being patronised by an overzealous mod.

Pyxidium answered 26/10, 2016 at 13:18 Comment(0)
M
3

Carpela‘s answer works fine, but for the message_object, it's missing "raw" in Message.new. The correct code should as following:

message_object = Google::Apis::GmailV1::Message.new(raw: m.encoded) # or m.to_s
client.send_user_message('me', message_object)
Micrometeorology answered 28/4, 2017 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.