Python基于smtplib模块发送邮件代码实例


Posted in Python onMay 29, 2020

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.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中AND、OR的一个使用小技巧
Feb 18 Python
Python实现的FTP通信客户端与服务器端功能示例
Mar 28 Python
pandas 使用apply同时处理两列数据的方法
Apr 20 Python
Python实现接受任意个数参数的函数方法
Apr 21 Python
使用Flask集成bootstrap的方法
Jul 24 Python
对python 树状嵌套结构的实现思路详解
Aug 09 Python
pytorch torchvision.ImageFolder的用法介绍
Feb 20 Python
python dict乱码如何解决
Jun 07 Python
PyCharm配置anaconda环境的步骤详解
Jul 31 Python
python3中celery异步框架简单使用+守护进程方式启动
Jan 20 Python
pycharm代码删除恢复的方法
Jun 26 Python
python数字图像处理实现图像的形变与缩放
Jun 28 Python
Django通过json格式收集主机信息
May 29 #Python
Python小白不正确的使用类变量实例
May 29 #Python
python小白切忌乱用表达式
May 29 #Python
Python爬虫实现百度翻译功能过程详解
May 29 #Python
Django中的模型类设计及展示示例详解
May 29 #Python
如何理解Python中包的引入
May 29 #Python
浅谈django channels 路由误导
May 28 #Python
You might like
Look And Say 序列php实现代码
2011/05/22 PHP
PHP版国家代码、缩写查询函数代码
2011/08/14 PHP
PHP COOKIE及时生效的方法介绍
2014/02/14 PHP
php二维码生成
2015/10/19 PHP
html超链接打开窗口大小的方法
2013/03/05 Javascript
jquery实现多行文字图片滚动效果示例代码
2014/10/10 Javascript
利用Angularjs和Bootstrap前端开发案例实战
2016/08/27 Javascript
浅析JavaScript中var that=this
2017/02/17 Javascript
layui 监听表格复选框选中值的方法
2018/08/15 Javascript
微信小程序实现联动选择器
2019/02/15 Javascript
vue项目前端微信JSAPI与外部H5支付相关实现过程及常见问题
2020/04/14 Javascript
uni-app使用微信小程序云函数的步骤示例
2020/05/22 Javascript
jQuery--遍历操作实例小结【后代、同胞及过滤】
2020/05/22 jQuery
详解vite2.0配置学习(typescript版本)
2021/02/25 Javascript
使用Python的web.py框架实现类似Django的ORM查询的教程
2015/05/02 Python
关于Python中浮点数精度处理的技巧总结
2017/08/10 Python
pandas 两列时间相减换算为秒的方法
2018/04/20 Python
python实现美团订单推送到测试环境,提供便利操作示例
2019/08/09 Python
python 实现批量替换文本中的某部分内容
2019/12/13 Python
Python的对象传递与Copy函数使用详解
2019/12/26 Python
PyCharm中Matplotlib绘图不能显示UI效果的问题解决
2020/03/12 Python
如何清空python的变量
2020/07/05 Python
pycharm 快速解决python代码冲突的问题
2021/01/15 Python
分享30个新鲜的CSS3打造的精美绚丽效果(附演示下载)
2012/12/28 HTML / CSS
罗德与泰勒百货官网:Lord & Taylor
2016/08/12 全球购物
澳洲女装时尚在线:Blue Bungalow
2018/05/05 全球购物
澳洲在线厨具商店:Kitchen Style
2018/05/05 全球购物
Vivo俄罗斯官方在线商店:中国智能手机品牌
2019/10/04 全球购物
部队万能检讨书
2014/02/20 职场文书
工伤赔偿协议书范本
2014/04/15 职场文书
爱护公物演讲稿
2014/09/09 职场文书
党员批评与自我批评
2014/10/15 职场文书
高校自主招生校长推荐信
2015/03/23 职场文书
热血教师观后感
2015/06/10 职场文书
python OpenCV学习笔记
2021/03/31 Python
Nginx如何配置Http、Https、WS、WSS的方法步骤
2021/05/11 Servers