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

作者:lijiao 时间:2023-06-14 15:58:59 

本文实例为大家分享了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)

来源:https://blog.csdn.net/wjoxoxoxxx/article/details/77944126

标签:python,email,邮件
0
投稿

猜你喜欢

  • asp 快钱网关接口 支付宝接口 财付通接口代码

    2011-03-08 10:55:00
  • python中加背景音乐如何操作

    2023-11-09 02:54:51
  • Python heapq使用详解及实例代码

    2023-03-07 14:36:56
  • 深入理解ASP中FSO的神奇功能

    2007-09-18 12:22:00
  • ASP实现网站智能分词搜索

    2007-10-18 13:50:00
  • php解析字符串里所有URL地址的方法

    2023-08-19 08:31:38
  • python爬虫爬取淘宝商品信息(selenum+phontomjs)

    2022-07-07 05:40:52
  • vue+php实现的微博留言功能示例

    2023-11-18 01:47:02
  • 如何配置一个稳定的SQL Server数据库

    2008-12-09 14:07:00
  • Python BeautifulReport可视化报告代码实例

    2023-11-12 14:53:13
  • Python 解析pymysql模块操作数据库的方法

    2021-05-07 07:46:13
  • Python+logging输出到屏幕将log日志写入文件

    2023-07-19 05:29:21
  • JavaScript版无组件上传类

    2007-10-06 23:16:00
  • 服务端XMLHTTP(ServerXMLHTTP in ASP)进阶应用-User Agent伪装

    2008-11-11 12:29:00
  • 如何基于Python制作有道翻译小工具

    2023-10-01 21:00:58
  • 深入浅析Django MTV模式

    2021-08-18 07:55:32
  • Python中号称神仙的六个内置函数详解

    2021-02-04 13:41:22
  • 页面表达常用方式

    2010-05-27 12:42:00
  • Python深入学习之上下文管理器

    2023-03-27 19:09:43
  • Python数据分析 Numpy 的使用方法

    2023-01-14 15:12:36
  • asp之家 网络编程 m.aspxhome.com