0

I am trying to send a en email with an attachment via the GmailV1 API. However it just isn't working due to Missing Draft Message errors.

According to RubyDoc I tried to create a draft message as follows:

The GmailV1:GmailService.create_user_draft() methods takes in an identifier and a draft_object (accepting 'me' for the authorized user). A draft object (Google::Apis::GmailV1::Draft) takes a message in the form of Google::Apis::GmailV1::Message which in turn takes a payload in the form of Google::Apis::GmailV1::MessagePart which has the desired filename method.

So I ran this code:

##assume client is an authorized instance of Google::Apis::GmailV1:GmailService
msg_part = Google::Apis::GmailV1::MessagePart.new(filename: 'path/to/file')
msg = Google::Apis::GmailV1::Message.new(payload: msg_part)
draft = Google::Apis::GmailV1::Draft.new(message: msg)
client.create_user_draft('me', draft)

>> Google::Apis::ClientError: invalidArgument: Missing draft message

How Come?

Versions:

  • google-api-client 0.9.9

  • googleauth 0.5.1

  • ruby 2.3.1p112

2
  • Have you stumbled upon this great answer? Might give some clues. Commented Aug 15, 2016 at 14:13
  • 1
    I have but it refers to an old version of the API that is completely different now in every way, from authentication to making API requests. It doesn't apply / help here. Commented Aug 17, 2016 at 11:42

2 Answers 2

1

Using the GmailService class as described here I was able to save a draft using the code below. I think the key is that the raw keyword is required in the message.

result = service.create_user_draft(
  user_id,
  Google::Apis::GmailV1::Draft.new(
    :message => Google::Apis::GmailV1::Message.new(
      :raw => "To: [email protected]\r\nSubject: Test Message\r\n\r\nTest Body"
    )
  )
)
Sign up to request clarification or add additional context in comments.

Comments

1

I solved this problem creating first a Mail object with the 'mail' gem in this way:

require 'mail'

mail = Mail.new
mail['from'] = '[email protected]'
mail[:to]    = '[email protected]'
mail.subject = 'This is a test email'
mail.body 'this is the body'
mail.add_file("./path/to/file")
#... and other ...

then i converted this in a raw object:

raw_message = mail.to_s

then i create gmail message with this raw:

message = Google::Apis::GmailV1::Message.new(
  :raw => raw_message  
)

and finnaly:

draft = Google::Apis::GmailV1::Draft.new(message: message)
gmail.create_user_draft('me', draft)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.