Compose an email with markdown and send it with a Python script
Tags: python, discuss, tutorial, beginners
Published: 2021-04-09 04:59:10

## Recap

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.

{% link aahnik/how-to-send-emails-with-python-simply-explained-for-beginners-hea %}


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7m587kwr82reydz857j0.png)
 

## Markdown

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](https://www.markdownguide.org/cheat-sheet) because:
- it is used to write posts in dev.to
- 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`.

```shell
❯ pip install Markdown
```

## Write the code

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

Delete the old `send.py` 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 `settings.py`

```python
# settings.py

HOST = 'smtp.gmail.com'
PORT = 587
SENDER = 'youremail@gmail.com'
PASSWORD = 'your12434(893**!@password'
RECIPIENT = 'some@example.com'
MESSAGE_FILE = 'compose.md'
DISPLAY_NAME = 'Mr. Bean'
```

Now let's write down the `send.py`

```python
# send.py

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 = file.read()


server = SMTP(host=HOST, port=PORT)
server.connect(host=HOST, port=PORT)
server.ehlo()
server.starttls()
server.ehlo()
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")

multipart_msg.attach(part1)
multipart_msg.attach(part2)


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

print('Sent email successfully!')


```

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

```shell
❯ python send.py
Sent email successfully!
```

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](https://aahnik.dev) and you can follow me on [GitHub](https://github.com/aahnik) and [dev.to](https://dev.to/aahnik) to stay updated with my latest repos and articles.