Search code examples
pythongmail-apimime

How to customize gmail subject by using python


I want to style gmail subject header while sending email using python. I am using python email.mime.text. There are quite many solutions that covers for styling the message body by using mime.MultiPart but I cannot find for styling the subject. In below code, I want to style for the subject.

        message = MIMEText(msg_body)
        message['To'] = to 
        message['From'] = sender  
        message['Subject'] = subject

        message = {'message': {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode(),
                               'threadId': threadId}}

Any idea, how to do it ?


Solution

  • No, you cannot style an email subject.

    An email subject is a plain text string, defined by RFC-2822 as a text field. Any markup you might add to it will be rendered as is, not interpreted.

    Email body is a different thing, in a MIME-formatted email it may come in multiple formats, including text/html, which would be rendered as HTML by a client that knows how to do so (e.g. Gmail).

    If you want to embellish your email's subject, add emoji characters or other Unicode characters:

    🌲🎉 Happy New Year! ❄❄❄

    These are going to be rendered by Unicode-capable clients (again, such as Gmail).