Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error sending mail with Google API - "'raw' RFC822 payload message string or uploading message via /upload/* URL required"

I am trying to send a message via the Google API in python, and am trying to run an example taken almost directly from the Google example page.

def CreateMessage(sender, to, subject, message_text):
    message = MIMEText(message_text)
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject
    return {'raw': base64.urlsafe_b64encode(message.as_string().replace('message','resource').encode('ascii'))}

But when I try to send it

    message = CreateMessage(sender, to, subject, message_text)
    message = service.users().messages().send(body=list(message),userId='me').execute()

I get the error : "'raw' RFC822 payload message string or uploading message via /upload/* URL required"

From other posts it seems like Google is expecting an attachment. Is there something wrong with the MIMEText making it expect one, and if so, how do I fix it?

like image 709
kainC Avatar asked Jun 28 '16 00:06

kainC


2 Answers

I was able to get the example working by removing the changing create_message to:

create_message = {
        'raw': encoded_message
    }
like image 175
Sero Avatar answered Sep 24 '22 23:09

Sero


list(message) is not necessary and is giving the API a body of:

[{"raw": "b64 content..."}]

just do:

...messages().send(body=message, userId='me'...
like image 24
Jay Lee Avatar answered Sep 24 '22 23:09

Jay Lee