How to send emotive email content and attachments in Python

王林
Release: 2023-05-18 11:28:53
forward
1042 people have browsed it

1. Preparation

Before we start, we need to prepare some things. First, we need to install Python. Python can be downloaded from the official website.

How to send emotive email content and attachments in Python

Secondly, we need to install the smtplib library. This can be done in the terminal with the following command:

pip install smtplib
Copy after login

2. Create the email

Before sending the email, we need to create the email. For this, we need to use Python’s email library.

This library allows us to create various types of emails, including HTML formatted emails.

How to send emotive email content and attachments in Python

The following is a simple Python program for creating an email in HTML format:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# 创建一个带有HTML正文的新电子邮件
msg = MIMEMultipart()
msg['From'] = 'sender@example.com'
msg['To'] = 'receiver@example.com'
msg['Subject'] = 'HTML邮件'
# HTML正文
html = """
<html>
<body>
<h2>这是一个HTML邮件</h2>
<p>这是一个演示如何发送HTML格式电子邮件的示例。</p >
</body>
</html>
"""
# 将HTML正文添加到电子邮件中
msg.attach(MIMEText(html, &#39;html&#39;))
# 使用SMTP服务器发送电子邮件
server = smtplib.SMTP(&#39;smtp.gmail.com&#39;, 587)
server.starttls()
server.login(&#39;sender@example.com&#39;, &#39;password&#39;)
server.sendmail(&#39;sender@example.com&#39;, &#39;receiver@example.com&#39;, msg.as_string())
server.quit()
Copy after login
Copy after login

In this example, we first introduce the necessary Modules: smtplib, MIMEMultipart and MIMEText.

How to send emotive email content and attachments in Python

Next, we create a MIMEMultipart instance and set some email headers. The next step is to build an HTML body and then add it to the email using MIMEText.

Finally, we use the SMTP server to send the email.

3. Send an email

After we create the email, we need to send it using an SMTP server.

SMTP server is a protocol used to send email. In Python, you can use the smtplib module to send emails.

How to send emotive email content and attachments in Python

The following is a simple Python program for sending an email in HTML format:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# 创建一个带有HTML正文的新电子邮件
msg = MIMEMultipart()
msg[&#39;From&#39;] = &#39;sender@example.com&#39;
msg[&#39;To&#39;] = &#39;receiver@example.com&#39;
msg[&#39;Subject&#39;] = &#39;HTML邮件&#39;
# HTML正文
html = """
<html>
<body>
<h2>这是一个HTML邮件</h2>
<p>这是一个演示如何发送HTML格式电子邮件的示例。</p >
</body>
</html>
"""
# 将HTML正文添加到电子邮件中
msg.attach(MIMEText(html, &#39;html&#39;))
# 使用SMTP服务器发送电子邮件
server = smtplib.SMTP(&#39;smtp.gmail.com&#39;, 587)
server.starttls()
server.login(&#39;sender@example.com&#39;, &#39;password&#39;)
server.sendmail(&#39;sender@example.com&#39;, &#39;receiver@example.com&#39;, msg.as_string())
server.quit()
Copy after login
Copy after login

In this example, we first create a new MIMEMultipart object and set some email headers.

How to send emotive email content and attachments in Python

Next, we generate an HTML body and add it to the email using MIMEText. Finally, we use an SMTP server to send emails.

4. Add attachments

In addition to the HTML body, we can also add attachments to the email. For this, we need to use Python’s email library.

The following is a simple Python program for adding attachments to HTML-formatted emails:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
# 创建一个带有HTML正文和图片附件的新电子邮件
msg = MIMEMultipart()
msg[&#39;From&#39;] = &#39;sender@example.com&#39;
msg[&#39;To&#39;] = &#39;receiver@example.com&#39;
msg[&#39;Subject&#39;] = &#39;HTML邮件&#39;
# HTML正文
html = """
<html>
<body>
<h2>这是一个HTML邮件</h2>
<p>这是一个演示如何发送HTML格式电子邮件的示例。</p >
<p>下面是一张图片:</p >
< img src="cid:image1">
</body>
</html>
"""
# 将HTML正文添加到电子邮件中
msg.attach(MIMEText(html, &#39;html&#39;))
# 添加图片附件
with open(&#39;image.png&#39;, &#39;rb&#39;) as f:
    img_data = f.read()
img = MIMEImage(img_data)
img.add_header(&#39;Content-ID&#39;, &#39;<image1>&#39;)
msg.attach(img)
# 使用SMTP服务器发送电子邮件
server = smtplib.SMTP(&#39;smtp.gmail.com&#39;, 587)
server.starttls()
server.login(&#39;sender@example.com&#39;, &#39;password&#39;)
server.sendmail(&#39;sender@example.com&#39;, &#39;receiver@example.com&#39;, msg.as_string())
server.quit()
Copy after login

In this example, we first create a new MIMEMultipart object and set Some email headers.

Next, we will create an HTML body and attach it to the email using MIMEText. Next, we add an image attachment using MIMEImage and link it to the HTML body using Content-ID.

How to send emotive email content and attachments in Python

Finally, we use the SMTP server to send the email.

The above is the detailed content of How to send emotive email content and attachments in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!