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 相关文章推荐
在Heroku云平台上部署Python的Django框架的教程
Apr 20 Python
python中format()函数的简单使用教程
Mar 14 Python
python实现音乐下载器
Apr 15 Python
python3连接MySQL数据库实例详解
May 24 Python
Python利用Django如何写restful api接口详解
Jun 08 Python
通过python爬虫赚钱的方法
Jan 29 Python
详解python破解zip文件密码的方法
Jan 13 Python
python实现ftp文件传输系统(案例分析)
Mar 20 Python
python读取mysql数据绘制条形图
Mar 25 Python
python能自学吗
Jun 18 Python
如何更换python默认编辑器的背景色
Aug 10 Python
python代码实现图书管理系统
Nov 30 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
php注入实例
2006/10/09 PHP
PHP判断一个gif图片是否为动态图片的方法
2014/11/19 PHP
laravel实现上传图片,并且制作缩略图,按照日期存放的代码
2019/10/16 PHP
jQuery 1.8 Release版本发布了
2012/08/14 Javascript
扩展js对象数组的OrderByAsc和OrderByDesc方法实现思路
2013/05/17 Javascript
jQuery oLoader实现的加载图片和页面效果
2015/03/14 Javascript
jQuery版本升级踩坑大全
2016/01/12 Javascript
详解Vue 事件驱动和依赖追踪
2017/04/22 Javascript
JavaScript变量类型以及变量作用域详解
2017/08/14 Javascript
js实现图片放大展示效果
2017/08/30 Javascript
vue2.0 datepicker使用方法
2018/02/04 Javascript
浅谈vue.use()方法从源码到使用
2019/05/12 Javascript
Node 代理访问的实现
2019/09/19 Javascript
Python实现的彩票机选器实例
2015/06/17 Python
Python爬取网页中的图片(搜狗图片)详解
2017/03/23 Python
Python文件的读写和异常代码示例
2017/10/31 Python
Python爬取当当、京东、亚马逊图书信息代码实例
2017/12/09 Python
为什么str(float)在Python 3中比Python 2返回更多的数字
2018/10/16 Python
python启动应用程序和终止应用程序的方法
2019/06/28 Python
Python实现的企业粉丝抽奖功能示例
2019/07/26 Python
pytorch多GPU并行运算的实现
2019/09/27 Python
Python3.7基于hashlib和Crypto实现加签验签功能(实例代码)
2019/12/04 Python
pytorch 中pad函数toch.nn.functional.pad()的用法
2020/01/08 Python
Python如何在DataFrame增加数值
2020/02/14 Python
Python如何合并多个字典或映射
2020/07/24 Python
Hotels.com南非:酒店预订
2017/11/02 全球购物
美国优质宠物用品购买网站:Muttropolis
2020/02/17 全球购物
国际商务系学生个人的自我评价
2013/11/26 职场文书
应届生的求职推荐信范文
2013/11/30 职场文书
高一新生军训感言
2014/03/02 职场文书
《天安门广场》教学反思
2014/04/23 职场文书
放飞理想演讲稿
2014/09/09 职场文书
详解MySQL事务的隔离级别与MVCC
2021/04/22 MySQL
Redis 的查询很快的原因解析及Redis 如何保证查询的高效
2022/03/16 Redis
python字符串的一些常见实用操作
2022/04/06 Python
Python中request的基本使用解决乱码问题
2022/04/12 Python