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

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 …


This content originally appeared on DEV Community and was authored by Aahnik Daw

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.

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
  • setting a sender name (you can set sender name to anything, like Mr. Bean irrespective of your email address)

alt text

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 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.

❯ 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

# 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

# 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.

❯ 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.


This content originally appeared on DEV Community and was authored by Aahnik Daw


Print Share Comment Cite Upload Translate Updates
APA

Aahnik Daw | Sciencx (2021-04-09T04:59:10+00:00) Compose an email with markdown and send it with a Python script. Retrieved from https://www.scien.cx/2021/04/09/compose-an-email-with-markdown-and-send-it-with-a-python-script/

MLA
" » Compose an email with markdown and send it with a Python script." Aahnik Daw | Sciencx - Friday April 9, 2021, https://www.scien.cx/2021/04/09/compose-an-email-with-markdown-and-send-it-with-a-python-script/
HARVARD
Aahnik Daw | Sciencx Friday April 9, 2021 » Compose an email with markdown and send it with a Python script., viewed ,<https://www.scien.cx/2021/04/09/compose-an-email-with-markdown-and-send-it-with-a-python-script/>
VANCOUVER
Aahnik Daw | Sciencx - » Compose an email with markdown and send it with a Python script. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/09/compose-an-email-with-markdown-and-send-it-with-a-python-script/
CHICAGO
" » Compose an email with markdown and send it with a Python script." Aahnik Daw | Sciencx - Accessed . https://www.scien.cx/2021/04/09/compose-an-email-with-markdown-and-send-it-with-a-python-script/
IEEE
" » Compose an email with markdown and send it with a Python script." Aahnik Daw | Sciencx [Online]. Available: https://www.scien.cx/2021/04/09/compose-an-email-with-markdown-and-send-it-with-a-python-script/. [Accessed: ]
rf:citation
» Compose an email with markdown and send it with a Python script | Aahnik Daw | Sciencx | https://www.scien.cx/2021/04/09/compose-an-email-with-markdown-and-send-it-with-a-python-script/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.