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 fromsmptplib
- we then defined the variables
HOST
,PORT
,SENDER
andPASSWORD
- we then created an object of the
SMTP
class namedserver
- Now call multiple server functions:
-
connect
ehlo
starttls
ehlo
login
-
- define the
RECIPIENT
andMESSAGE
- send the email by calling
server.sendmail
To see the code, read my previous article.
How to send emails with Python? Simply explained for beginners
Aahnik Daw ・ Apr 7 '21
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).
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.
I am Aahnik Daw and you can follow me on GitHub and dev.to to stay updated with my latest repos and articles.