Reputation: 4050
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
Upvotes: 0
Views: 569
Reputation: 11
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)
Upvotes: 1
Reputation: 935
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"
)
)
)
Upvotes: 1