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 del()函数用法
Mar 24 Python
使用SAE部署Python运行环境的教程
May 05 Python
深入理解Django中内置的用户认证
Oct 06 Python
Python多线程爬虫实战_爬取糗事百科段子的实例
Dec 15 Python
python opencv 图像拼接的实现方法
Jun 27 Python
django 使用全局搜索功能的实例详解
Jul 18 Python
Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str
Aug 07 Python
Python loguru日志库之高效输出控制台日志和日志记录
Mar 07 Python
Python批量修改xml的坐标值全部转为整数的实例代码
Nov 26 Python
selenium+超级鹰实现模拟登录12306
Jan 24 Python
Python实现byte转integer
Jun 03 Python
Python实现双向链表
May 25 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
中国收音机工业发展史
2021/03/02 无线电
一些常用的php函数
2006/12/06 PHP
组合算法的PHP解答方法
2012/02/04 PHP
解析php session_set_save_handler 函数的用法(mysql)
2013/06/29 PHP
php实现的支持断点续传的文件下载类
2014/09/23 PHP
PHP实现上传图片到数据库并显示输出的方法
2018/05/31 PHP
解决在Laravel 中处理OPTIONS请求的问题
2019/10/11 PHP
ThinkPHP 5 AJAX跨域请求头设置实现过程解析
2020/10/28 PHP
js中匿名函数的N种写法
2010/09/08 Javascript
用jQuery模拟页面加载进度条的实现代码
2011/12/19 Javascript
谈谈JavaScript中的函数与闭包
2013/04/14 Javascript
JavaScript中使用typeof运算符需要注意的几个坑
2014/11/08 Javascript
onmouseover事件和onmouseout事件全面理解
2016/08/15 Javascript
微信小程序 WXML、WXSS 和JS介绍及详解
2016/10/08 Javascript
Node.js 8 中的 util.promisify的详解
2017/06/12 Javascript
js实现随机数字字母验证码
2017/06/19 Javascript
不刷新网页就能链接新的js文件方法总结
2020/03/01 Javascript
Python 列表(List)操作方法详解
2014/03/11 Python
Python自动连接ssh的方法
2015/03/07 Python
python简单判断序列是否为空的方法
2015/06/30 Python
numpy找出array中的最大值,最小值实例
2018/04/03 Python
python3实现爬取淘宝美食代码分享
2018/09/23 Python
Python中浅拷贝copy与深拷贝deepcopy的简单理解
2018/10/26 Python
python使用tomorrow实现多线程的例子
2019/07/20 Python
Python实现的企业粉丝抽奖功能示例
2019/07/26 Python
HTML5 canvas基本绘图之绘制线条
2016/06/27 HTML / CSS
美特斯邦威官方商城:邦购网
2016/10/13 全球购物
高中生学习总结的自我评价范文
2013/10/13 职场文书
网吧收银员岗位职责
2013/12/14 职场文书
简历上的自我评价
2014/02/03 职场文书
车间机修工岗位职责
2014/02/28 职场文书
小学校园之星事迹材料
2014/05/16 职场文书
2014年最新党员对照检查材料汇总
2014/09/15 职场文书
使用PDF.js渲染canvas实现预览pdf的效果示例
2021/04/17 Javascript
详解Go语言Slice作为函数参数的使用
2021/07/02 Golang
Node与Python 双向通信的实现代码
2021/07/16 Javascript