python smtplib模块实现发送邮件带附件sendmail
程序员文章站
2022-08-28 09:25:30
本文实例为大家分享了python smtplib实现发送邮件的具体代码,供大家参考,具体内容如下
#!/usr/bin/env python
# -*- co...
本文实例为大家分享了python smtplib实现发送邮件的具体代码,供大家参考,具体内容如下
#!/usr/bin/env python # -*- coding: UTF-8 -*- from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from email.utils import COMMASPACE,formatdate from email import encoders import os def send_mail(server, fro, to, subject, text, files=[]): assert type(server) == dict assert type(to) == list assert type(files) == list msg = MIMEMultipart() msg['From'] = fro msg['Subject'] = subject msg['To'] = COMMASPACE.join(to) #COMMASPACE==', ' msg['Date'] = formatdate(localtime=True) msg.attach(MIMEText(text)) for f in files: part = MIMEBase('application', 'octet-stream') #'octet-stream': binary data part.set_payload(open(f, 'rb').read()) encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f)) msg.attach(part) import smtplib smtp = smtplib.SMTP(server['name'], server['port']) smtp.ehlo() smtp.starttls() smtp.ehlo() smtp.login(server['user'], server['passwd']) smtp.sendmail(fro, to, msg.as_string()) smtp.close() if __name__=='__main__': server = {'name':'mail.server.com', 'user':'chenxiaowu', 'passwd':'xxxx', 'port':25} fro = 'chenxiaowu@163.com' to = ['xxxx@163.com'] subject = '脚本运行提醒' text = 'mail content' files = ['top_category.txt'] send_mail(server, fro, to, subject, text, files=files)
从网上找了些资料,不会有个别错误,上面代码经调试测试通过。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Python使用smtplib模块发送电子邮件的流程详解
-
Python使用smtplib模块发送电子邮件的流程详解
-
用smtplib和email封装python发送邮件模块类分享
-
python中使用smtplib和email模块发送邮件实例
-
Python基于smtplib实现异步发送邮件服务
-
python email smtplib模块发送邮件代码实例
-
python smtplib模块发送SSL/TLS安全邮件实例
-
用smtplib和email封装python发送邮件模块类分享
-
Codeigniter实现发送带附件的邮件
-
二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)