Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I create HTML email drafts in Gmail using Python?

I can successfully create plaintext email drafts in Gmail using this snippet:

from email.message import Message
import imaplib
import time

message = Message()

    # Create message
    message["To"] = "john.doe@gmail.com"
    message["Subject"] = "Subject line"
    message.set_payload("This is the email <a href=#>body</a>")
    utf8_message = str(message).encode("utf-8")

    # Send message
    status, data = imap_ssl.append('"[Google Mail]/Drafts"', "", imaplib.Time2Internaldate(time.time()), utf8_message)

However, it displays as:

This is the email <a href=#>body</a>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a way to get it to display as this?

This is the email body

I’ve tried playing with MIMEText, which I can successfully use to send HTML emails via smtplib, but I’m not sure if it’s possible with imaplib.

>Solution :

Just add a content-type header

    message["Content-Type"] = "text/html;charset=UTF-8"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading