Creating a Gmail Draft with Recipients through Gma

2019-02-19 10:11发布

问题:

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>"
            }
          ]
        }
      }
    }
  )

回答1:

'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"