Gmail Api Create Draft Reply


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

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=p0gk[email protected]>
From: [email protected]

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 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.

    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.

    Draft object, including draft id and message meta data.

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

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

        return draft

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

And here’s how I call it:

 gdraft.CreateDraft(service, user_id='me', message_body=
            {'raw': 'aGVsbG8gd29ybGQ=',
             'threadId': '15e5bdc650b1a068',
             'payload': {
                 "mimeType": "multipart/alternative",
                 "headers": [
                         "name": "In-Reply-To",
                         "value": "<16DCF6644C054E39B1F7F901BDD08EA2@[email protected]>"
                         "name": "References",
                         "value": '<16DCF6644C054E39B1F7F901BDD08EA2@[email protected]>'
                         "name": "Message-ID",
                         "value": "<16DCF6644C054E39B1F7F901BDD08EA2@[email protected]"
                         "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.


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.

I changed:

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 returned "Missing draft message">
Asked By: K-Dawg



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.

I found the code here worked:

It can be used like so:

msg = create_message('[email protected]','[email protected]','Re: Some Subject','This is a test')

create_draft(service,'me',  msg)
Answered By: K-Dawg

thanks…also, I think one fix is needed based on a recent change to the code on that page:

Answered By: Andrew Paullin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.