Created
July 18, 2022 03:30
-
-
Save KhanhhNe/28c1a305d67f7eb5545804f0de2c6b08 to your computer and use it in GitHub Desktop.
Create a message object to use with Gmail API so you don't have to find in countless Google searches.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import email.policy | |
from email.mime.text import MIMEText | |
def create_message(sender, to, subject, message_text): | |
""" | |
Create an email message. | |
:param sender: Sender's email (can use format '{name} <{email}>' to show custom sender name) | |
:param to: Receiver's email | |
:param subject: The email subject | |
:param message_text: Email content (HTML supported) | |
:return: Message body (to use with Gmail API) | |
""" | |
message = MIMEText(message_text, 'html', policy=email.policy.default) | |
message['to'] = to | |
message['from'] = sender | |
message['subject'] = subject | |
raw_message = base64.urlsafe_b64encode(message.as_string().encode()).decode() | |
return { | |
'raw': raw_message | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment