Send rich text multimedia email in Python

Upasana | November 02, 2019 | 3 min read | 132 views


Here, we are going to learn how to send rich text multimedia email in python. We will be using smtplib for email via GMAIL.

Define sender & recipient

fromaddr = "xxx@gmail.com" (1)
toaddr = "yyy@gmail.com" (2)

msg = MIMEMultipart()

msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Today's morning quote" (3)
1 Email of sender
2 Email of recipient
3 Subject of Email

Define body of the email

body = "Never lose hope" (1)
msg.attach(MIMEText(body, 'plain'))
1 Body of the email

Setup Server and send email

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "****password****")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

Now the whole script for sending email will be like below :

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


fromaddr = "xxx@gmail.com"
toaddr = "yyy@gmail.com"

msg = MIMEMultipart()

msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Today's morning quote"

body = "Never Lose hope"
msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "***password***")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

Since we are sending email via a gmail, google will not allow you to log in via smtplib because it has flagged this login as less secure.

So In order to allow us to send email via gmail, you will have to login to your gmail or sender’s gmail account and then visit this link :

When you will visit this link, you will see this

Allow less secure apps

Click on the slider to allow less secure apps. Once, this is ON we will be able to send email via gmail account.

App password setup in GMAIL

The preferred approach for sending emails using GMAIL is to setup App password using GMAIL security options.

Goto https://accounts.google.com and then select Security and then choose Signing in to Google and then select App passwords.

google app passwords

In the App passwords screen, select app as Mail and device as Custom Device, provide name something as "java mail program"

google generate password

Then click on generate. This will generate app password which should be used instead of real account password in email SMTP authentication.

Run the script and check if received email

Run the file and you shall have received email

For testing, i had used same email account for sending and receiving emails.

sc2

As you can see, we have received email which means this is working great. Lets move on to our next step of sending rich text in the email.

Sending rich text in email

To send rich text in email, we just need to define html over the content and then define body as html.

You can use any html editor online to build some html. Sample is given below :

html = """
<h1 style="text-align: center;"><span style="color: #ff6600;"><strong>Morning Quote</strong></span></h1>
<h4><span style="color: #008000;"><strong>I believe that everything happens for a reason. People change so that you can learn to let go, things go wrong so that you appreciate them when they're right, you believe lies so you eventually learn to trust no one but yourself, and sometimes good things fall apart so better things can fall together.</strong></span></h4>
<p>&nbsp;</p>
"""

msg.attach(MIMEText(html, 'html'))

Whole script will be something like below :

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


fromaddr = "xxx@gmail.com"
toaddr = "yyy@gmail.com"

msg = MIMEMultipart()

msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Morning Quote"

html = """
<h1 style="text-align: center;"><span style="color: #ff6600;"><strong>Morning Quote</strong></span></h1>
<h4><span style="color: #008000;"><strong>I believe that everything happens for a reason. People change so that you can learn to let go, things go wrong so that you appreciate them when they're right, you believe lies so you eventually learn to trust no one but yourself, and sometimes good things fall apart so better things can fall together.</strong></span></h4>
<p>&nbsp;</p>
"""

msg.attach(MIMEText(html, 'html'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "***password***")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

Run the script to see if you have received the email. You shall receive

sc3

I hope this helped you. Thanks for reading the article.


Top articles in this category:
  1. Python send GMAIL with attachment
  2. Find extra long factorials in python
  3. Python coding challenges for interviews
  4. Flask Interview Questions
  5. SVM after LSTM deep learning model for text classification
  6. Write a program for Hailstone Sequence in Python
  7. Sequence of Differences in Python

Recommended books for interview preparation:

Find more on this topic: