3
votes

I have been trying to figure out how to automatically add recipients to a Draft email that is created using the Gmail API through their Ruby library. I can create the draft without any issues but setting the recipients is causing me troubles and I haven't been able to find any good examples showing the best way to add email specific things.

Using the Google API playground and pulling in drafts that have already been created, it looks like the structure should be something similar to what is shown below, but whenever the draft is created, there are no recipients.

  @result = client.execute(
    :api_method => gmail.users.drafts.create,
    :parameters => {
      'userId' => "me"      
    },
    :body_object => {
      'message' => {
        'raw' =>  Base64.urlsafe_encode64('Test Email Message'),
        'payload' => {
          'headers' => 
          [
            {
              'name' => "To",
              'value' => "John Smith <[email protected]>"
            }
          ]
        }
      }
    }
  )
1
possible duplicate of Creating draft via Google Gmail APIrds
This question is different from that one. That question is just for creating a draft, whereas this one is adding recipients to a draft message which is difficult to figure out given the ambiguous documentation.Kevin

1 Answers

7
votes

'raw' should contain the entire (RFC822) email, complete with body and headers. Do not use the 'payload.headers' structure, that parsed format is only used for returning during message.get() presently.

so for 'raw' you'd want to Base64.urlsafe_encode64() a string like: "To: [email protected]\r\nFrom: [email protected]\r\nSubject: my subject\r\n\r\nBody goes here"