一时兴起,70周年国庆无事可做,写个程序,自己给自己发送邮件玩,是不是很像程序猿干得事情?🤪
所谓的发送邮件,就是使用遵循一定的通讯规则的通信传输方式,一般情来说,通常使用smtp协议进行邮件发送,pop3协议接收邮件,使用smtp协议发送邮件给邮件服务器时规定:
1、使用"ehlo"命令和连接上的smtp服务器打招呼
2、使用"auth login"登录Smtp服务器
3、指明邮件的发件人和收件人mail from:rcpt to:
4、编写邮件内容,编写格式要按规则
邮件头使用下面的三个字段来指明from字段用于指明邮件的发送人to字段用于指明邮件的收件人subject字段用于指明邮件的主题
邮件的内容包含了这些信息之后才是一封格式良好的邮件。
①、输入"data"命令
data
②、编写邮件内容
from:<发送者邮箱> ----邮件头
to:<接收者邮箱> ----邮件头
subject:邮件标题 ----邮件头
-----空行
far far away, long long ago ----邮件的具体内容
5、通知服务器内容完毕
.
6、断开与邮件服务器连接
quit
简言之,smtp发送邮件就是使用命令给服务器发送一些内容,而这些内容和命令是有格式要求的;所以如果想发送邮件的话,就是把要发送的内容,按照格式整理好,再通过相应的命令发送给smtp服务器就行了。如果自己从头动手写,可能就比较麻烦了,好在python最大的好处就是裤子多啊,没有特别扩展,使用Python的smtplib包发送,使用email包处理邮件内容(包括附件),然后,为了最大复用,封装成类,为了灵活发送各种邮件,带附件,不带附件,带图片,不带图片… 诸如此类,除了邮件接收人,邮件标题,其他内容都设置为可单独配置的方法调用方法即可发送,使用还算方便,独乐乐不如众乐乐,拿出来,分享给大家,欢迎投🌹拍🧱,🥚就不要了,挺贵得😋,好了,直接上福利分享源码:
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 4 '''邮件发送封装 5 依赖 smtplib 和 email包 6 ''' 7 8 # Author: zendchina <faq@quxue.net.cn> 9 # version: 国庆特别版 10 11 import smtplib 12 13 from email.header import Header 14 from email.mime.text import MIMEText 15 from email.mime.image import MIMEImage 16 from email.mime.multipart import MIMEMultipart 17 18 19 class PyMailer(): 20 smtp = '这里配置smtp服务器地址' 21 smtp_port = 25 22 23 sender = '这里写smtp服务器帐号' 24 pwd = '密码哦' 25 26 receiver = "这里可以设置一个默认发送,提高容错性,增强健壮性" 27 28 # 设置总的邮件体对象,对象类型为mixed 29 msg = MIMEMultipart('mixed') 30 CODING = 'utf-8' 31 subject = "这里是默认的邮件标题" 32 33 def __init__(self, reciever="", subject=""): 34 self.receiver = reciever if reciever else self.receiver 35 self.subject = subject if subject else self.subject 36 37 def setMsg(self): 38 """ 39 添加邮件发送的必要信息 40 """ 41 self.msg['From'] = '%s<%s>' % (self.sender, self.sender) 42 self.msg['To'] = self.receiver 43 44 # 邮件的主题,显示在接收邮件的预览页面 45 self.msg['subject'] = Header(self.subject, self.CODING) 46 47 def body(self, text): 48 """ 49 添加文本内容 50 :param text: 51 :return: 52 """ 53 text = MIMEText(text, 'plain', self.CODING) 54 self.msg.attach(text) 55 56 def attach_html(self, html, attach_name): 57 """ 58 添加html 59 :param html: 60 :param attach_name: 61 :return: 62 """ 63 html = MIMEText(html, 'html', 'utf-8') 64 # 添加Mine头信息,声明附件类型,以使附件打开时为html格式 65 html["Content-Disposition"] = 'attachment; filename="%s"' % attach_name 66 # 把构造的内容写到邮件体中 67 self.msg.attach(html) 68 69 def attach_img(self, img_path, attach_name): 70 """ 71 添加图片附件 72 :param img_path: 73 :param attach_name: 74 :return: 75 """ 76 image_file = open(img_path, 'rb').read() 77 image = MIMEImage(image_file) 78 image.add_header('Content-ID', '<image1>') 79 # 添加Mine头信息,防止文件打开乱码 80 image["Content-Disposition"] = 'attachment; filename="%s"' % attach_name 81 self.msg.attach(image) 82 83 def attach_file(self, original, attach_name): 84 """ 85 添加文件附件 86 :param original: 上传附件的本地路径 87 :param attach_name: 邮件中附件的名字 88 :return: 89 """ 90 txt_file = open(original, 'rb').read() 91 txt = MIMEText(txt_file, 'base64', 'utf-8') 92 txt["Content-Type"] = 'application/octet-stream' 93 # 添加Mine头信息,添加上传附件的类型及名称 94 txt.add_header('Content-Disposition', 'attachment', filename=attach_name) 95 self.msg.attach(txt) 96 97 def send_mail(self): 98 """ 99 执行发送 100 """ 101 self.setMsg() 102 try: 103 sftp = smtplib.SMTP(self.smtp, self.smtp_port) 104 sftp.login(self.sender, self.pwd) 105 sftp.sendmail(self.sender, self.receiver, self.msg.as_string()) 106 sftp.quit() 107 print("send success!") 108 except: 109 print("send failer") 110 下面是使用示例: 111 112 if __name__ == "__main__": 113 to = "要发送到的邮件地址" 114 info = "邮件正文内容" 115 subject = "邮件标题" 116 # 邮件html附件内容 117 link="https://blog.csdn.net/yageeart/article/category/9387999" 118 html_info = """ 119 <html> 120 <head> 121 <meta charset="UTF-8"> 122 <title>70周年国庆特别奉送</title> 123 <style> 124 body { 125 text-align: center; 126 } 127 </style> 128 </head> 129 <body> 130 <h1>70周年特别奉送</h1> 131 <a href="%s">自动化测试专题</a> 132 </body> 133 </html> 134 """%link 135 txt = r'D:\py\hello_world.txt' 136 img = r"d:\py\WX20190928-104723.png" 137 138 # 创建发送邮件对象 139 pm = PyMailer(subject=subject, reciever=to) 140 # 邮件正文 141 pm.body(info) 142 # 添加文件,不是必须的 143 pm.attach_file(txt, 'hello.txt') 144 pm.attach_file(txt, 'hello.txt') 145 # 添加邮件html附件,不是必须的,内容须是html源码 146 pm.attach_html(html_info, 'click me.html') 147 pm.attach_html(html_info, 'click me.html') 148 # 添加图片附件,不是必须的 149 pm.attach_img(img, "screenshot.png") 150 pm.attach_img(img, "screenshot.png") 151 # 发送邮件,必须的 152 pm.send_mail()
最后,如果你也想尝试自动化测试,或者多了解下自动化测试,一起来吧:
浙公网安备 33010602011771号