python发送邮件的实例代码(支持html、图片、附件)

yizhihongxing

Python发送邮件的实例代码(支持HTML、图片、附件)攻略

Python是一种强大的编程语言,可以用于发送电子邮件。Python的smtplib和email库提供了发送电子邮件的功能。本文将详细讲解Python发送邮件的实例代码,包括发送纯文本邮件、发送HTML邮件、发送带图片的邮件、发送带附件的邮件等。

步骤1:导入库

在发送邮件之前,我们需要导入smtplib和email库。以下是导入库的示例代码:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication

在上面的代码中,我们导入了smtplib、MIMEText、MIMEMultipart、MIMEImage和MIMEApplication等库,以便发送不同类型的邮件。

步骤2:连接SMTP服务器

在发送邮件之前,我们需要连接SMTP服务器。以下是连接SMTP服务器的示例代码:

smtp_server = 'smtp.gmail.com'
smtp_port = 587
smtp_username = 'your_email@gmail.com'
smtp_password = 'your_password'

smtp_connection = smtplib.SMTP(smtp_server, smtp_port)
smtp_connection.starttls()
smtp_connection.login(smtp_username, smtp_password)

在上面的代码中,我们使用Gmail作为SMTP服务器,并使用starttls()函数启用TLS加密。然后,我们使用login()函数登录SMTP服务器。

步骤3:发送邮件

在连接SMTP服务器后,我们可以发送邮件。以下是发送邮件的示例代码:

发送纯文本邮件

from_email = 'your_email@gmail.com'
to_email = 'recipient_email@gmail.com'
subject = 'Test Email'
body = 'This is a test email.'

msg = MIMEText(body)
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject

smtp_connection.sendmail(from_email, to_email, msg.as_string())

在上面的代码中,我们使用MIMEText()函数创建了一个纯文本邮件,并使用sendmail()函数发送了邮件。

发送HTML邮件

from_email = 'your_email@gmail.com'
to_email = 'recipient_email@gmail.com'
subject = 'Test Email'
body = '<h1>This is a test email.</h1>'

msg = MIMEMultipart('related')
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject

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

smtp_connection.sendmail(from_email, to_email, msg.as_string())

在上面的代码中,我们使用MIMEMultipart()函数创建了一个包含HTML内容的邮件,并使用sendmail()函数发送了邮件。

发送带图片的邮件

from_email = 'your_email@gmail.com'
to_email = 'recipient_email@gmail.com'
subject = 'Test Email'
body = '<h1>This is a test email.</h1><img src="cid:image1">'

msg = MIMEMultipart('related')
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject

with open('image.jpg', 'rb') as f:
    image_data = f.read()

image = MIMEImage(image_data)
image.add_header('Content-ID', '<image1>')
msg.attach(image)

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

smtp_connection.sendmail(from_email, to_email, msg.as_string())

在上面的代码中,我们使用MIMEMultipart()函数创建了一个包含图片的邮件,并使用sendmail()函数发送了邮件。我们使用MIMEImage()函数将图片添加到邮件中,并使用Content-ID标识图片。然后,我们将HTML内容添加到邮件中。

发送带附件的邮件

from_email = 'your_email@gmail.com'
to_email = 'recipient_email@gmail.com'
subject = 'Test Email'
body = 'This is a test email.'

msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject

with open('document.pdf', 'rb') as f:
    document_data = f.read()

document = MIMEApplication(document_data)
document.add_header('Content-Disposition', 'attachment', filename='document.pdf')
msg.attach(document)

text = MIMEText(body)
msg.attach(text)

smtp_connection.sendmail(from_email, to_email, msg.as_string())

在上面的代码中,我们使用MIMEMultipart()函数创建了一个包含附件的邮件,并使用sendmail()函数发送了邮件。我们使用MIMEApplication()函数将附件添加到邮件中,并使用Content-Disposition标识附件。然后,我们将文本内容添加到邮件中。

步骤4:关闭SMTP连接

在发送邮件后,我们需要关闭SMTP连接。以下是关闭SMTP连接的示例代码:

smtp_connection.quit()

在上面的代码中,我们使用quit()函数关闭SMTP连接。

示例1:发送纯文本邮件

以下是一个发送纯文本邮件的示例代码:

import smtplib
from email.mime.text import MIMEText

smtp_server = 'smtp.gmail.com'
smtp_port = 587
smtp_username = 'your_email@gmail.com'
smtp_password = 'your_password'

smtp_connection = smtplib.SMTP(smtp_server, smtp_port)
smtp_connection.starttls()
smtp_connection.login(smtp_username, smtp_password)

from_email = 'your_email@gmail.com'
to_email = 'recipient_email@gmail.com'
subject = 'Test Email'
body = 'This is a test email.'

msg = MIMEText(body)
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject

smtp_connection.sendmail(from_email, to_email, msg.as_string())

smtp_connection.quit()

在上面的代码中,我们使用smtplib和email库发送了一封纯文本邮件。

示例2:发送带图片的邮件

以下是一个发送带图片的邮件的示例代码:

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

smtp_server = 'smtp.gmail.com'
smtp_port = 587
smtp_username = 'your_email@gmail.com'
smtp_password = 'your_password'

smtp_connection = smtplib.SMTP(smtp_server, smtp_port)
smtp_connection.starttls()
smtp_connection.login(smtp_username, smtp_password)

from_email = 'your_email@gmail.com'
to_email = 'recipient_email@gmail.com'
subject = 'Test Email'
body = '<h1>This is a test email.</h1><img src="cid:image1">'

msg = MIMEMultipart('related')
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = subject

with open('image.jpg', 'rb') as f:
    image_data = f.read()

image = MIMEImage(image_data)
image.add_header('Content-ID', '<image1>')
msg.attach(image)

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

smtp_connection.sendmail(from_email, to_email, msg.as_string())

smtp_connection.quit()

在上面的代码中,我们使用smtplib和email库发送了一封带图片的邮件。我们使用MIMEImage()函数将图片添加到邮件中,并使用Content-ID标识图片。然后,我们将HTML内容添加到邮件中。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python发送邮件的实例代码(支持html、图片、附件) - Python技术站

(0)
上一篇 2023年5月15日
下一篇 2023年5月15日

相关文章

  • Python入门_浅谈数据结构的4种基本类型

    Python入门_浅谈数据结构的4种基本类型 在使用Python进行编程时,了解数据结构的基本类型是非常重要的。 Python语言支持以下四种基本类型: 列表(List) 元组(Tuple) 集合(Set) 字典(Dictionary) 列表(List) 定义: 列表是Python中最基本的数据结构之一,可以作为一个有序的序列,内容可以包含不同类型的元素。 …

    python 2023年5月14日
    00
  • 值得收藏的10道python 面试题

    作为网站的作者,我们推出了一篇名为“值得收藏的10道Python面试题”的文章,旨在帮助学习Python语言的人更好地准备面试。下面将对这篇文章的内容进行完整的讲解,包括题目解析、示例说明和答案解释。 1.判断字符串是否为回文 该题要求判断给定的字符串是否为回文字符串(即正着和倒着读都一样),其解法如下: def is_palindrome(s): &quo…

    python 2023年6月5日
    00
  • python 在sql语句中使用%s,%d,%f说明

    Python中可以使用%s,%d,%f等占位符表示字符串、整数和浮点数,以便于在SQL语句中动态地插入传递的值。下面是详细讲解: 字符串占位符%s 在SQL语句中,可以使用%s占位符表示动态传递的字符串。在Python编程中,可以使用字符串拼接或格式化字符串的方式来动态生成SQL语句。例如: name = ‘Lucy’ age = 20 sql = &quo…

    python 2023年5月18日
    00
  • 详解Python中的进程和线程

    详解Python中的进程和线程 在Python中,进程和线程都是用来实现多任务编程的机制。但是它们之间有着很大的区别,下面我们就来详细讲解Python中的进程和线程。 进程 进程是操作系统中进行资源分配和调度的基本单位。每一个进程都有自己独立的内存空间,不同进程之间互相独立运行,互不干扰。Python通过os模块提供的fork()函数来创建进程,如下所示: …

    python 2023年5月14日
    00
  • Python:检查“字典”是否为空似乎不起作用

    【问题标题】:Python: Checking if a ‘Dictionary’ is empty doesn’t seem to workPython:检查“字典”是否为空似乎不起作用 【发布时间】:2023-04-06 13:36:02 【问题描述】: 我正在尝试检查字典是否为空,但它的行为不正常。它只是跳过它并显示 ONLINE 除了显示消息之外没有…

    Python开发 2023年4月7日
    00
  • 浅谈python中的错误与异常

    当我们在Python中编写代码时,错误和异常是常见的问题。错误是指程序在编译或运行时出现的问题,例如语法错误、类型错误等。而异常是指程序在运行时出现的问题,例如除以零、索引错误等。当程序出现错误或异常时,程序会停止运行并输出错误信息。以下是浅谈Python中错误与异常的完整攻略。 错误和异常的区别 在Python中,错误和异常是不同的概念。错误是指程序在编译…

    python 2023年5月13日
    00
  • Python网络爬虫实例讲解

    Python网络爬虫实例讲解 目录 简介 环境搭建 常用Python爬虫库的介绍 爬虫实例1:爬取网易云音乐评论 爬虫实例2:爬取豆瓣电影TOP250 1. 简介 Python是一门易学易用的编程语言,也是一门广泛应用于数据科学、人工智能等领域的语言。由于其开源、强大的库支持以及优秀的数据处理能力,Python在大数据分析、机器学习、自然语言处理等领域得到了…

    python 2023年5月14日
    00
  • 浅析Python 3 字符串中的 STR 和 Bytes 有什么区别

    浅析Python 3 字符串中的 STR 和 Bytes 有什么区别 在 Python 3 中,STR 和 Bytes 是最基础和常用的两个数据类型之一,它们之间的区别是非常重要的。在本文中,我们将深入浅出地讲解 STR 和 Bytes 的含义、区别以及在 Python 中的使用。 STR 和 Bytes 的含义 STR STR 是字符串类型,在 Pytho…

    python 2023年5月31日
    00
合作推广
合作推广
分享本页
返回顶部