3

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 <john_smith.fake@gmail.com>"
            }
          ]
        }
      }
    }
  )
Kevin
  • 301
  • 2
  • 13
  • possible duplicate of [Creating draft via Google Gmail API](http://stackoverflow.com/questions/25493213/creating-draft-via-google-gmail-api) – rds Sep 28 '14 at 10:46
  • 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 Sep 29 '14 at 16:42

1 Answers1

7

'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: someguy@example.com\r\nFrom: myself@example.com\r\nSubject: my subject\r\n\r\nBody goes here"

Eric D
  • 6,901
  • 1
  • 15
  • 26
  • Thanks for the answer. I've switched the raw line to the following: 'raw' => Base64.urlsafe_encode64('To:test.email@gmail.com\r\nFrom:other.email@gmail.com\r\nSubject:Test\r\n\r\nTest Email') but I now get an error saying: {"error"=>{"errors"=>[{"domain"=>"global", "reason"=>"invalidArgument", "message"=>"Invalid to header"} Am I still missing something? – Kevin Aug 26 '14 at 13:32
  • 1
    Really? Hmm that to header seems fine. Do you have a space between "To: test.email@gmail.com"? When did you do it? Try again now to confirm that's still happening? – Eric D Aug 26 '14 at 16:00
  • It works! Maybe I did have a space, although I thought I checked for that. Thanks so much for your help! – Kevin Aug 26 '14 at 16:31
  • Hi @Kevin I have the same error 'Invalid to header', here my gist any idea?? https://gist.github.com/joselo/8e7464df88344c69a42b – joselo Nov 27 '14 at 21:44
  • @joselo I would format your 'msg' as a string rather than as an object. Try hardcoding the values as in this answer or my comment, just make sure there are no spaces anywhere and see if that helps you? – Kevin Dec 18 '14 at 21:06
  • @EricD Let's I just want to add only attachment in my draft, do I need to pass in raw? If yes can you please share the json format how should be the format there. I am trying with below format { 'raw' : base64encode of json attachment } attachment json:- { 'mimeType': 'image/jpeg', 'fileName': 'image_picker5608811674862066343.jpg', 'size': 2290285 'data': base64 of file } – Shakti S.P. Swain Aug 22 '20 at 03:52