Gmail Api创建草稿回复 [英] Gmail Api Create Draft Reply

查看:77
本文介绍了Gmail Api创建草稿回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google API文档中的创建草稿功能:

I am using the create draft function from googles API documentation:

https://developers.google.com/gmail /api/v1/reference/users/drafts/create

每当我发送邮件时,当我进入gmail时,都会在电子邮件文本中显示以下内容:

Whenever I send a message I get the following appear in my email message text when I go into gmail:

hello world
Date: Mon, 11 Sep 2017 15:31:19 +0200
Message-Id: <CAKPeGO69TbbigNFrK8T37fYgPzCfZwVf=p0gkvJbZF6duwWsdw@mail.gmail.com>
From: myemailaddress@gmail.com

我不知道为什么要得到所有这些文本.

I don't know why I'm getting all of this text.

我想做的是创建对现有电子邮件的电子邮件草稿答复,但是我似乎得到的只是上面文本的新草稿(没有填充到/来自/主题字段).

What I am trying to do is to create a draft email reply to an existing email but all I seem to get is a new draft with the text above (no to/from/subject fields populated).

这是我正在使用的功能:

Here's the function I'm using:

import base64
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import mimetypes
import os

def CreateDraft(service, user_id, message_body):
   """Create and insert a draft email. Print the returned draft's message and id.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    message_body: The body of the email message, including headers.

  Returns:
    Draft object, including draft id and message meta data.
  """
    try:


        draft = service.users().drafts().create(userId=user_id, body=message_body).execute()

        print('Draft id: %s\nDraft message: %s' % (draft['id'], draft['message']))

        return draft

    except errors.HttpError as error:
        print('An error occurred: %s' % error)
        return None

这就是我的称呼:

 gdraft.CreateDraft(service, user_id='me', message_body=
    {
        'message':
            {'raw': 'aGVsbG8gd29ybGQ=',
             'threadId': '15e5bdc650b1a068',
             'payload': {
                 "mimeType": "multipart/alternative",
                 "headers": [
                     {
                         "name": "In-Reply-To",
                         "value": "<16DCF6644C054E39B1F7F901BDD08EA2@466209web4@mail.gmail.com>"
                     },
                     {
                         "name": "References",
                         "value": '<16DCF6644C054E39B1F7F901BDD08EA2@466209web4@mail.gmail.com>'
                     },
                     {
                         "name": "Message-ID",
                         "value": "<16DCF6644C054E39B1F7F901BDD08EA2@466209web4@mail.gmail.com"
                     },
                     {
                         "name": "Subject",
                         "value": "Re: Software Developer - Hertford"
                     }
                 ]
             }
             }
    })

我已经尝试了四天,但都没有成功,所以我非常感谢您的帮助.

I have been trying now for four days without a success so I'd really appreciate any help.

更新:

所以看来我可能需要使用createmessage函数(基于下面的评论);但是,这似乎在Python3中不起作用.

So it seems that I might need to use the createmessage function (based on the comments below); however this doesn't seem to work in Python3.

我更改了:

return {'raw': base64.urlsafe_b64encode(message.as_string())}

收件人:

 return {'raw': str(base64.urlsafe_b64encode(message.as_string().encode("utf-8")))}

尝试使其正常运行,但出现错误:

in an attempt to make it work but I am getting errors:

An error occurred: <HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/me/drafts?alt=json returned "Missing draft message">

推荐答案

谢谢大家留下的评论.您在不同的观点上都没事.

Thank you all for the comments you left. You were all right on different points.

问题是我使用的示例与文档"页面上的示例略有不同.

The problem was that I was using a slightly different example to the one on the Docs page.

我发现这里的代码有效: https://developers.google.com/gmail/api/guides/drafts

I found the code here worked: https://developers.google.com/gmail/api/guides/drafts

它可以像这样使用:

msg = create_message('YOU@gmail.com','THEM@company.com','Re: Some Subject','This is a test')

create_draft(service,'me',  msg)

这篇关于Gmail Api创建草稿回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆