Python Gmail Api Create Draft Reply -
i using create draft function googles api documentation:
https://developers.google.com/gmail/api/v1/reference/users/drafts/create
whenever send message following appear in email message text when go 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 of text.
what trying create draft email reply existing email seem new draft text above (no to/from/subject fields populated).
here's function i'm using:
import base64 email.mime.audio import mimeaudio email.mime.base import mimebase email.mime.image import mimeimage email.mime.multipart import mimemultipart email.mime.text import mimetext import mimetypes import os def createdraft(service, user_id, message_body): """create , insert draft email. print returned draft's message , id. args: service: authorized gmail api service instance. user_id: user's email address. special value "me" can used indicate authenticated user. message_body: body of email message, including headers. returns: draft object, including draft id , 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 error: print('an error occurred: %s' % error) return none
and here's how call it:
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 4 days without success i'd appreciate help.
update:
so seems might need use createmessage function (based on comments below); doesn't seem work in python3.
i changed:
return {'raw': base64.urlsafe_b64encode(message.as_string())}
to:
return {'raw': str(base64.urlsafe_b64encode(message.as_string().encode("utf-8")))}
in attempt make work 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 comments left. right on different points.
the problem was using different example 1 on docs page.
i found code here worked: https://developers.google.com/gmail/api/guides/drafts
it can used so:
msg = create_message('you@gmail.com','them@company.com','re: software developer - hertford','this test') create_draft(service,'me', msg)
Comments
Post a Comment