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: "test1@test.com", 
from: "test2@test.com", 
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 <nobody@gmail.com> - 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.

Carpela
  • 2,155
  • 1
  • 24
  • 55
  • 1
    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 http://stackoverflow.com/help/mcve on how to create a Minimal, Complete, and Verifiable example which helps others solve your problems. – Holger Just Oct 25 '16 at 19:01
  • 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... – Carpela Oct 26 '16 at 00:16
  • 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.. – Holger Just Oct 26 '16 at 10:50
  • 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? – Carpela Oct 26 '16 at 11:34
  • How about that? Not sure I've added much to anyone who understands the api client, but is that clearer for you? – Carpela Oct 26 '16 at 11:48

2 Answers2

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.

Carpela
  • 2,155
  • 1
  • 24
  • 55
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)
Yi Liu
  • 31
  • 1