DEV Community

Cover image for Compose an email with markdown and send it with a Python script
Aahnik Daw
Aahnik Daw

Posted on

Compose an email with markdown and send it with a Python script


In the previous article of this series, we had written a simple script to send an email with Python.

Let's recap the steps:

  • we first imported SMTP class from smptplib
  • we then defined the variables HOST,PORT,SENDER and PASSWORD
  • we then created an object of the SMTP class named server
  • Now call multiple server functions:
    1. connect
    2. ehlo
    3. starttls
    4. ehlo
    5. login
  • define the RECIPIENT and MESSAGE
  • send the email by calling server.sendmail

To see the code, read my previous article.

Let's Enhance

In this article, we will take further what we have learned in the previous article.

We will add some new features to our script:

  • rich-text via markdown.
  • setting a subject of the email.
  • using an alias name for the sender (you can set the sender's display name to be anything, like "Mr. Bean", irrespective of your actual email address).

alt text


In the modern world, plain text looks boring. Professional emails have some logo, or title image, along with the richly formatted text.

Writing HTML is time-consuming, so we will use the markdown format, to compose beautiful emails.

If you are not familiar with the markdown formatting, then you should learn it because:

  • it is used to write posts in
  • it is commonly used to write the README file for your GitHub projects

To deal with markdown in python, we need the markdown package.

So install it via pip.

❯ pip install Markdown
Enter fullscreen mode Exit fullscreen mode

Write the code

Let's say we will write our email in a file called Our script should send the content of that file as an email to our recipient.

Delete the old that we have written last day. Let's refactor our code, to make it more mature and maintainable.

Let's define all our variables in a file called


HOST = ''
PORT = 587
PASSWORD = 'your12434(893**!@password'
Enter fullscreen mode Exit fullscreen mode

Now let's write down the


from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from smtplib import SMTP

import markdown

from settings import (HOST, PORT,
                      SENDER, DISPLAY_NAME, PASSWORD,
                      RECIPIENT, MESSAGE_FILE)

with open(MESSAGE_FILE) as file:
    message =

server = SMTP(host=HOST, port=PORT)
server.connect(host=HOST, port=PORT)
server.login(user=SENDER, password=PASSWORD)

multipart_msg = MIMEMultipart("alternative")

multipart_msg["Subject"] = message.splitlines()[0]
multipart_msg["From"] = DISPLAY_NAME + f' <{SENDER}>'
multipart_msg["To"] = RECIPIENT

text = message
html = markdown.markdown(text)

part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")


server.sendmail(SENDER, RECIPIENT, multipart_msg.as_string())

print('Sent email successfully!')

Enter fullscreen mode Exit fullscreen mode

That's it. Coding is over. Now let's run the script

❯ python
Sent email successfully!
Enter fullscreen mode Exit fullscreen mode

Your output will look like this 👆 if you did everything alright. The email will be sitting right in the recipient's inbox.

I am Aahnik Daw and you can follow me on GitHub and to stay updated with my latest repos and articles.

Top comments (8)

jbalagnaranin profile image
Jérémie Balagna-Ranin

Hello and thank you for this simple yet realllly useful article :D

Just a tip to complete the subject: following the reading, I did a quick search and found there're easy way to replace your personal email by a simple server on Linux :

@aahnik maybe for a fourth article ;)

aahnik profile image
Aahnik Daw

thats a great idea.

actually i have a plan to make a series on self hosted software.

self hosted cloud, email, and more.

will come some day in future.

you may follow me to get notified.

jbalagnaranin profile image
Jérémie Balagna-Ranin

Nice ! :D

I'll keep an eye on your future articles ;-)

cookrdan profile image

Awesome. Was wondering how to do the display name or “alias” and couldn’t figure it out before. I will give this a try. Thanks for the simple code and explanations.

Would love to see how to do all the different types of attachments.

I haven’t looked at my incomplete script for a while. I had inline images from markdown working. That took a little bit of juice if I remember. But attaching other things like PDFs or whatever I haven’t attempted yet.

aahnik profile image
Aahnik Daw

in the next (and last) article of this series, I will discuss about the attachment of files.

thank you for your kind feedback. ❤️

aahnik profile image
Aahnik Daw

You may follow me, to get notified of the next article of the series.

cjsmocjsmo profile image
Charlie J Smotherman

What would really be nice is to show how to recieve an email and respond to it with python (ie no services like Twillo)

pratap180290 profile image
Deepesh pratap singh

Really Great Sir,
How to do for multiple RECIPIENTS for bulk mailing?