Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29755

base64 encoding not taking mime message

$
0
0

I am trying to send an oauth gmail using python and am not able to create MimeMessages that agree with Google's API. After creating a sample message, I use base64 to encode it as a string. However, I come up with the error: TypeError: a bytes-like object is required, not 'str'

The line at the top of the stack:

return {'raw': base64.urlsafe_b64encode(message_str)}

I've tried using different versions of encoding (encoders.encode_base64(message), message.as_string().encode("utf-8"), etc.) and have tried converting the message.as_string() to bytes (as the error message suggests) but am met with different error messages from Google, saying the encoding doesn't meet their requirements, which are "MIME email messages compliant with RFC 2822 and encoded as base64url strings."

My entire function is as follows.

def create_message(sender, to, subject, message_text):

    message = MIMEText(message_text)
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject
    message_str = message.as_string()
    return {'raw': base64.urlsafe_b64encode(message_str)}

I have no idea why this shouldn't work. It is copy-pasted from the tutorial. I am running python 3.7.2


Viewing all articles
Browse latest Browse all 29755

Trending Articles