Python基于smtplib模块发送邮件代码实例
作者:--TINGXIN-- 发布时间:2022-09-18 11:07:49
标签:Python,smtplib,模块,邮件
smtplib模块负责发送邮件:是一个发送邮件的动作,连接邮箱服务器,登录邮箱,发送邮件(有发件人,收信人,邮件内容)。
email模块负责构造邮件:指的是邮箱页面显示的一些构造,如发件人,收件人,主题,正文,附件等。
email模块下有mime包,mime英文全称为“Multipurpose Internet Mail Extensions”,即多用途互联网邮件扩展,是目前互联网电子邮件普遍遵循的邮件技术规范。
该mime包下常用的有三个模块:text,image,multpart。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
#邮件服务器信息
smtp_server = "smtp.qq.com"
port = 465 # For starttls
sender_email = "12345689@qq.com"
password="" #get password from mailsetting
#发送邮件信息,可以发送给多个收件人
receivers=["12345689@163.com","12345689@qq.com"]
subject="This is import Python SMTP 邮件(文件传输) 多媒体测试"
# message = MIMEText(text, "plain", "utf-8") #文本邮件
message = MIMEMultipart()
message["Subject"] = Header(subject, "utf-8")
message["from"] = sender_email
message["to"] = ",".join(receivers)
# 邮件正文内容
text="""
Dear Sir:
how are you ? \n
for detail information pls refer to attach1。\n
The files you need are as followed.\n
If you have any concern pls let me known.\n
enjoy your weekend.\n
BEST REGARDS \n
"""
# message.attach(MIMEText('for detail information pls refer to attach1。\n The files you need are as followed. \n If you have any concern pls let me known. \n enjoy your weekend', 'plain', 'utf-8')
message.attach(MIMEText(text,'plain','utf-8'))
# 构造附件1
attach_file1='IMG1965.JPG'
attach1 = MIMEText(open(attach_file1, 'rb').read(), 'base64', 'utf-8')
attach1["Content-Type"] = 'application/octet-stream'
attach1["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file1)
message.attach(attach1)
# 构造附件2
attach_file2='YLJ.jpg'
attach2 = MIMEText(open(attach_file2, 'rb').read(), 'base64', 'utf-8')
attach2["Content-Type"] = 'application/octet-stream'
attach2["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file2)
message.attach(attach2)
# Try to log in to server and send email
# server = smtplib.SMTP_SSL(smtp_server,port)
server = smtplib.SMTP_SSL(smtp_server,port)
try:
server.login(sender_email, password)
server.sendmail(sender_email,receivers,message.as_string())
print("邮件发送成功!!!")
print("Mail with {0} & {1} has been send to {2} successfully.".format(attach_file1,attach_file2,receivers))
except Exception as e:
# Print any error messages to stdout
print("Error: 无法发送邮件")
print(e)
finally:
server.quit()
结果
邮件发送成功!!!
Mail with IMG1965.JPG & IMG1963.jpg has been send to ['12345689@163.com', '12345689@qq.com'] successfully.
来源:https://www.cnblogs.com/tingxin/p/12961901.html


猜你喜欢
- python中迭代器和iter()函数迭代器为类序列对象提供了一个类序列的接口。python的迭代无缝地支持序列对象,而且它还允许程序员迭代
- 基本属性定义当前地牢的等级,地图长宽,房间数量,房间的最小最大长度,如下class Map: def __init
- 本文实例讲述了python实现telnet客户端的方法。分享给大家供大家参考。具体如下:python实现的telnet客户端程序,pytho
- scrapy是用python开发的爬虫框架,从网上查了安装方法,感觉都说的挺复杂,而且很多教程都很有年头了,于是记录了自己的安装过程。首先安
- (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接) 网站的数据库连接语句为:Ser
- 问题你想反方向迭代一个序列解决方案使用内置的 reversed() 函数,比如:>>> a = [1, 2, 3, 4]&
- 需求背景公司前端使用 Highcharts 构建图表,图表的图例支持点击显示或隐藏相应的指标。现在有需求后端需要存储用户在前端点击后显示图表
- mysql 配置白名单访问的步骤1.登录mysql -uroot -pmysql2.切换至mysql库use mysql;3.查看有白名单权
- 使用 filters 实现 英文字母 转大写1、template :<di
- 本文实例讲述了JavaScript点击按钮后弹出透明浮动层的方法。分享给大家供大家参考。具体分析如下:这里实现点击后页面变灰色,并用JS弹出
- Python 正则表达式正则表达式本身是独立于编程语言的知识,但是它又依附于编程语言,基本上我们所使用的编程语言都提供了对它的实现,当然了,
- 本文实例讲述了Python常用模块sys,os,time,random功能与用法。分享给大家供大家参考,具体如下:sys:介绍:主要包含涉及
- 背景最近在负责开发维护的一款数据平台,有一个功能是把数据从某个源头数据源(如常规的JDBC数据源,MySQL,Oracle等)推到目地数据源
- 这是支持的下载版本,去官网下载2020.3及以上(2021-03-18测试破解有效)官网下载地址:https://www.jetbrains
- 本文实例讲述了Python3基于sax解析xml操作。分享给大家供大家参考,具体如下:python使用SAX解析xmlSAX是一种基于事件驱
- 问题tensor详细数值 不能直接print打印:import tensorflow as tfx = tf.constant(1)prin
- 这篇文章主要介绍了通过实例了解Python str()和repr()的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- 客户端调用XMLHTTP的过程很简单,只有5个步骤: 1、创建XMLHTTP对象 2、打开与服务端的连接,同时定义指令发送方式,服务网页(U
- 一、图图:数据(张量Tenrsor)+ 操作(节点Operation) (静态)图可以用:1、默认图;2、自定义图。1、默认图查看默认图的方
- 一旦被黑客获取到webshell,黑客就知道了你的sqlserver管理员密码,如果sqlserver再没有经过安全设置那么黑客很容易就提权