• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python smtplib模块实现发送邮件带附件sendmail

python 搞代码 4年前 (2022-01-07) 57次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了python smtplib模块实现发送邮件带附件sendmail,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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<i style="color:transparent">来源gaodai$ma#com搞$$代**码)网</i>) == 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 = '[email protected]' to = ['[email protected]'] subject = '脚本运行提醒' text = 'mail content' files = ['top_category.txt'] send_mail(server, fro, to, subject, text, files=files) 

从网上找了些资料,不会有个别错误,上面代码经调试测试通过。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是python smtplib模块实现发送邮件带附件sendmail的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python smtplib模块实现发送邮件带附件sendmail
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址