Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

In the last section, we mentioned that there are different types of emails for different scenarios, which we can roughly divide into:

  1. Plain text mail;
  2. HTML mail;
  3. Mail with attachments.

We have learned how to send plain text messages using smtplib and the Email module. In this section, we will learn how to send HTML messages and add attachments to messages.

1. Email introduction in HTML format

An HTML message is a web-like message that contains HTML (hypertext) links that can be clicked to go to another page, images, sounds, etc. Compared with ordinary text mail, HTML mail can make the mail content colorful, in addition to text, but also can have sound image links and so on.

Usually, HTML emails are used in promotional activities, e-commerce and other content, in order to better display the content of emails and interact with users.

2. Send an HTML email

Send the mail as shown in the following code:

import smtplib
from email.mime.text import MIMEText
host_server = 'smtp.qq.com'  # host address
# Sender email
sender = "[email protected]"
Sender email password and authorization code
code = "xlogucqphohxcabi"
# the recipient
user = "[email protected]"
Prepare email data
# Email header
mail_title = "Second Email"
# content
mail_content = "" "< p > HTML email content < / p > < hr / > < p > < a href = "http://www.baidu.com" > baidu about < / a > < / p > < ul > < li > top1 < / li > < li > ranked by < / li > < / ul > "" "
# SMTP
smtp = smtplib.SMTP(host_server)
# login
smtp.login(sender, code)
# to send
msg = MIMEText(mail_content, 'html'.'utf-8')
msg['Subject'] = mail_title
msg['From'] = sender
msg['To'] = user
smtp.sendmail(sender, user, msg.as_string())

Copy the code

Code explanation: On the basis of sending ordinary mail code in the last small, will send content from ordinary text to HTML format content, first modify mail_content set to HTML content, modify MIMEText build parameter to HTML, other content unchanged. After the execution, open the received mail and receive the second mail, as shown in the figure below.

3. Email introduction with attachments

An email attachment is an attached file sent with an email. Attachments include audio, video, documents, pictures, and other files that are allowed to be sent (note that.exe files are not allowed to be sent). The attachment location of the mail with attachments is shown in the figure below.

4. Send an email with attachments

Send the mail as shown in the following code:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
host_server = 'smtp.qq.com'  # host address
# Sender email
sender = "[email protected]"
# Sender email authorization code
code = "xlogucqphohxcabi"
# the recipient
user = "[email protected]"
Prepare email data
# Email header
mail_title = "The third email"
# Email content
mail_content = "Please check the attachment for details!"
# SMTP
smtp = smtplib.SMTP(host_server)
# login
smtp.login(sender, code)
# Build attachments
attachment=MIMEApplication(open('newinfo.xlsx'.'rb').read())
Add a title to the attachment
attachment.add_header('Content-Disposition'.'attachment',filename='data.xlsx')
msg=MIMEMultipart()Build an instance with attachments
# Email header
msg['Subject'] = mail_title
# the sender
msg['From'] = sender
# the recipient
msg['To'] = user
# to send
smtp.sendmail(sender, user, msg.as_string())

Copy the code

On the basis of sending ordinary mail code, import the email module MIMEMultipart and MIMEApplication for building attachments. First, the attachment is encapsulated by MIMEApplication. Newinfo. XLSX is the name of the local file, and data. XLSX is the name displayed after it is sent to the mailbox of the other party. Build an instance with attachments through MIMEMultipart, otherwise unchanged. After the execution, open the received mail and receive the third mail, as shown in the figure below.