Python发送邮件 smtplib 是 Python 用来发送邮件的模块,email 是用来处理邮件消息
这里,我们使用装饰器 来制作发送邮件的功能
一、 文本信息 我们使用MIMEText
来发送文本信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 from smtplib import SMTPfrom email.mime.text import MIMEText from email.header import Header from email.utils import formataddr from functools import wrapsreceivers = ['a.l.kun@qq.com' ,] subject = '测试邮件' def decorate (fun_ ): username = '2855595515@qq.com' password_pass = 'meryyupicqjjciei' smtp = SMTP('smtp.qq.com' ) smtp.login(username, password_pass) @wraps(fun_ ) def func_mail (*args, **kwargs ): fun_(smtp, username, *args, **kwargs) smtp.quit() smtp.close() return func_mail @decorate def mail (smtp, username ): for receiver_ in receivers: msgRoot = MIMEText(f"这是一个测试邮件,不要回复哦~~~" , "html" , "utf-8" ) msgRoot["Subject" ] = Header(subject, "utf-8" ) msgRoot['From' ] = formataddr(("A.L.Kun" , username)) msgRoot['To' ] = formataddr((receiver_.split("@" )[0 ], receiver_)) smtp.sendmail(username, receiver_, msgRoot.as_string()) print (receiver_.split("@" )[0 ], ':发送完成' ) if __name__ == '__main__' : mail()
使用装饰器的好处,可以动态创建一个发送邮件的对象,同时可以不用关注邮件的配置,只要关注邮件的内容
二、 图片信息 我们可以使用MIMEImage
来读取图片数据,然后进行发送
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 from smtplib import SMTPfrom email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage from email.header import Header from email.utils import formataddr from functools import wrapsimport osreceivers = ['a.l.kun@qq.com' ,] subject = '测试邮件' pic_path = "./test.jpeg" def decorate (fun_ ): username = '2855595515@qq.com' password_pass = 'mdsrecuicqjjciei' smtp = SMTP('smtp.qq.com' ) smtp.login(username, password_pass) @wraps(fun_ ) def func_mail (*args, **kwargs ): fun_(smtp, username, *args, **kwargs) smtp.quit() smtp.close() return func_mail @decorate def mail (smtp, username ): for receiver_ in receivers: msgRoot = MIMEMultipart("related" ) msgRoot["Subject" ] = Header(subject, "utf-8" ) msgRoot['From' ] = formataddr(("A.L.Kun" , username)) msgRoot['To' ] = formataddr((receiver_.split("@" )[0 ], receiver_)) pic_data = open (pic_path, "rb" ).read() """以附件的形式发送图片""" send_img_f = MIMEImage(pic_data) send_img_f.add_header('Content-Disposition' , 'attachment' , filename=('utf-8' , '' , os.path.basename(pic_path))) msgRoot.attach(send_img_f) """将图片添加到正文中""" send_img_c = MIMEImage(pic_data) send_img_c.add_header("Content-Id" , "<img1>" ) msgRoot.attach(send_img_c) img_text = f""" <p>这是一张图片:</p> <br><img src="cid:img1" width="300" alt={os.path.basename(pic_path)} ></br> """ msgRoot.attach(MIMEText(img_text, "html" , "utf-8" )) smtp.sendmail(username, receiver_, msgRoot.as_string()) print (receiver_.split("@" )[0 ], ':发送完成' ) if __name__ == '__main__' : mail()
三、 附件 我们使用MIMEApplication
来发送附件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 from smtplib import SMTPfrom email.mime.multipart import MIMEMultipartfrom email.header import Headerfrom email.mime.application import MIMEApplication from email.utils import formataddrfrom functools import wrapsimport osreceivers = ['a.l.kun@qq.com' ,] subject = '测试邮件' file_lis = ["./test2.pptx" , "./test.jpeg" ] def decorate (fun_ ): username = '2855595515@qq.com' password_pass = 'mwdftrtycqjjciei' smtp = SMTP('smtp.qq.com' ) smtp.login(username, password_pass) @wraps(fun_ ) def func_mail (*args, **kwargs ): fun_(smtp, username, *args, **kwargs) smtp.quit() smtp.close() return func_mail @decorate def mail (smtp, username ): for receiver_ in receivers: msgRoot = MIMEMultipart("related" ) msgRoot["Subject" ] = Header(subject, "utf-8" ) msgRoot['From' ] = formataddr(("A.L.Kun" , username)) msgRoot['To' ] = formataddr((receiver_.split("@" )[0 ], receiver_)) for file in file_lis: msgFile = MIMEApplication(open (file, "rb" ).read()) msgFile.add_header('Content-Disposition' , 'attachment' , filename=('utf-8' , '' , os.path.basename(file))) msgRoot.attach(msgFile) smtp.sendmail(username, receiver_, msgRoot.as_string()) print (receiver_.split("@" )[0 ], ':发送完成' ) if __name__ == '__main__' : mail()