通过Gmail API创建包含收件人的Gmail草稿

时间:2014-08-25 21:29:45

标签: ruby google-api gmail gmail-api google-api-ruby-client

我一直试图弄清楚如何自动将收件人添加到使用Gmail API通过其Ruby库创建的草稿电子邮件中。我可以毫无问题地创建草稿,但设置收件人会给我带来麻烦,而且我找不到任何好的示例来展示添加电子邮件特定内容的最佳方式。

使用Google API游乐场并提取已创建的草稿,结构看起来应该与下面显示的类似,但是无论何时创建草稿,都没有收件人。

  @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 个答案:

答案 0 :(得分:5)

'raw'应该包含整个(RFC822)电子邮件,包含正文和标题。不要使用'payload.headers'结构,解析后的格式仅用于在message.get()中返回。

所以对于'raw'你想要Base64.urlsafe_encode64()这样的字符串: “收件人:someguy@example.com \ r \ nFrom:myself@example.com \ r \ n主题:我的主题\ r \ n \ r \ nBody到这里”

相关问题