python3.5 email实现发送邮件功能


Posted in Python onMay 22, 2018

本文实例为大家分享了python3.5 email发送邮件的具体代码,供大家参考,具体内容如下

直接套用代码即可

from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email import encoders
import smtplib
import time


def send_mail(subject):
 email_host = '' # 服务器地址
 sender = '' # 发件人
 password = '' # 密码,如果是授权码就填授权码
 receiver = '' # 收件人

 msg = MIMEMultipart()
 msg['Subject'] = subject # 标题
 msg['From'] = '' # 发件人昵称
 msg['To'] = '' # 收件人昵称

 signature = '''
\n\t this is auto test report!
\n\t you don't need to follow
'''
 # text = MIMEText(signature, 'plain') # 签名
 # msg.attach(text)

 # 正文-图片 只能通过html格式来放图片,所以要注释25,26行
 mail_msg = '''
<p>\n\t this is auto test report!</p>
<p>\n\t you don't need to follow</p>
<p><a href="http://blog.csdn.net/wjoxoxoxxx" rel="external nofollow" >我的博客:</a></p>
<p>截图如下:</p>
<p><img src="cid:image1"></p>
'''
 msg.attach(MIMEText(mail_msg, 'html', 'utf-8'))
 # 指定图片为当前目录
 fp = open(r'111.jpg', 'rb')
 msgImage = MIMEImage(fp.read())
 fp.close()
 # 定义图片 ID,在 HTML 文本中引用
 msgImage.add_header('Content-ID', '<image1>')
 msg.attach(msgImage)

 ctype = 'application/octet-stream'
 maintype, subtype = ctype.split('/', 1)
 # 附件-图片
 image = MIMEImage(open(r'111.jpg', 'rb').read(), _subtype=subtype)
 image.add_header('Content-Disposition', 'attachment', filename='img.jpg')
 msg.attach(image)
 # 附件-文件
 file = MIMEBase(maintype, subtype)
 file.set_payload(open(r'320k.txt', 'rb').read())
 file.add_header('Content-Disposition', 'attachment', filename='test.txt')
 encoders.encode_base64(file)
 msg.attach(file)

 # 发送
 smtp = smtplib.SMTP()
 smtp.connect(email_host, 25)
 smtp.login(sender, password)
 smtp.sendmail(sender, receiver, msg.as_string())
 smtp.quit()
 print('success')

if __name_- == '__main__':
 now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
 subject = now + '自动化测试报告'
 send_mail(subject)

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

Python 相关文章推荐
python类参数self使用示例
Feb 17 Python
Python中使用Flask、MongoDB搭建简易图片服务器
Feb 04 Python
用Python中的字典来处理索引统计的方法
May 05 Python
Python对文件操作知识汇总
May 15 Python
Python设计模式之门面模式简单示例
Jan 09 Python
python中的字符串内部换行方法
Jul 19 Python
对python读写文件去重、RE、set的使用详解
Dec 11 Python
详解python读取和输出到txt
Mar 29 Python
Keras 数据增强ImageDataGenerator多输入多输出实例
Jul 03 Python
Python3爬虫中关于Ajax分析方法的总结
Jul 10 Python
Python爬虫之Selenium下拉框处理的实现
Dec 04 Python
Python+MySQL随机试卷及答案生成程序的示例代码
Feb 01 Python
python模块smtplib实现纯文本邮件发送功能
May 22 #Python
python邮件发送smtplib使用详解
Jun 16 #Python
Python3多线程操作简单示例
May 22 #Python
Python3基于sax解析xml操作示例
May 22 #Python
Python smtplib实现发送邮件功能
May 22 #Python
linux下python使用sendmail发送邮件
May 22 #Python
Python实现的文本对比报告生成工具示例
May 22 #Python
You might like
第九节--绑定
2006/11/16 PHP
解析PayPal支付接口的PHP开发方式
2010/11/28 PHP
PHP 验证码的实现代码
2011/07/17 PHP
关于更改Zend Studio/Eclipse代码风格主题的介绍
2013/06/23 PHP
PHP连接MySQL数据库的三种方式实例分析【mysql、mysqli、pdo】
2019/11/04 PHP
jQuery使用手册之三 CSS操作
2007/03/24 Javascript
jQuery在vs2008及js文件中的无智能提示的解决方法
2010/12/30 Javascript
jQuery输入城市查看地图使用介绍
2013/05/08 Javascript
avascript中的自执行匿名函数应用示例
2014/09/15 Javascript
jQuery中nextAll()方法用法实例
2015/01/07 Javascript
jquery正则表达式验证(手机号、身份证号、中文名称)
2015/12/31 Javascript
举例讲解jQuery对DOM元素的向上遍历、向下遍历和水平遍历
2016/07/07 Javascript
js阻止移动端页面滚动的两种方法
2017/01/25 Javascript
十大热门的JavaScript框架和库
2017/03/21 Javascript
js学习总结_轮播图之渐隐渐现版(实例讲解)
2017/07/17 Javascript
关于Stream和Buffer的相互转换详解
2017/07/26 Javascript
Vue通过URL传参如何控制全局console.log的开关详解
2017/12/07 Javascript
NodeJs搭建本地服务器之使用手机访问的实例讲解
2018/05/12 NodeJs
Vue Components 数字键盘的实现
2019/09/18 Javascript
[02:16]DOTA2英雄基础教程 干扰者
2014/01/15 DOTA
[01:24:09]Ti4 冒泡赛第二轮DK vs C9 1
2014/07/14 DOTA
GitHub 热门:Python 算法大全,Star 超过 2 万
2019/04/29 Python
win10下python2和python3共存问题解决方法
2019/12/23 Python
Python类中self参数用法详解
2020/02/13 Python
pycharm实现在虚拟环境中引入别人的项目
2020/03/09 Python
用python爬虫批量下载pdf的实现
2020/12/01 Python
Merchant 1948澳大利亚:新西兰领先的鞋类和靴子供应商
2018/03/24 全球购物
介绍一下Python下range()函数的用法
2013/11/07 面试题
客服实习的个人自我鉴定
2013/10/20 职场文书
土木工程师职业规划范文
2014/03/07 职场文书
人才市场接收函
2015/01/30 职场文书
费城故事观后感
2015/06/10 职场文书
2019年励志签名:致拼搏路上的自己
2019/10/11 职场文书
比较几种Redis集群方案
2021/06/21 Redis
Nginx的基本概念和原理
2022/03/21 Servers
Python&Matlab实现灰狼优化算法的示例代码
2022/03/21 Python