python发送带附件的邮件

简介:

Send email with attachment

 
  1. import smtplib 
  2. from email.MIMEMultipart import MIMEMultipart 
  3. from email.MIMEBase import MIMEBase 
  4. from email.MIMEText import MIMEText 
  5. from email.Utils import COMMASPACE, formatdate 
  6. from email import Encoders 
  7. import os 
  8.  
  9. def sendMail(to, subject, text, files=[],server="localhost"): 
  10.     assert type(to)==list 
  11.     assert type(files)==list 
  12.     fro = "Expediteur <expediteur@mail.com>" 
  13.  
  14.     msg = MIMEMultipart() 
  15.     msg['From'] = fro 
  16.     msg['To'] = COMMASPACE.join(to) 
  17.     msg['Date'] = formatdate(localtime=True
  18.     msg['Subject'] = subject 
  19.  
  20.     msg.attach( MIMEText(text) ) 
  21.  
  22.     for file in files: 
  23.         part = MIMEBase('application'"octet-stream"
  24.         part.set_payload( open(file,"rb").read() ) 
  25.         Encoders.encode_base64(part) 
  26.         part.add_header('Content-Disposition''attachment; filename="%s"' 
  27.                        % os.path.basename(file)) 
  28.         msg.attach(part) 
  29.  
  30.     smtp = smtplib.SMTP(server) 
  31.     smtp.sendmail(fro, to, msg.as_string() ) 
  32.     smtp.close() 
  33.  
  34.  
  35. sendMail( 
  36.         ["destination@dest.kio"], 
  37.         "hello","cheers"
  38.         ["photo.jpg","memo.sxw"
  39.     ) 

 本文转自阿汐 51CTO博客,原文链接:http://blog.51cto.com/axiii/301199,如需转载请自行联系原作者


相关文章
|
3月前
|
存储 搜索推荐 数据安全/隐私保护
python实战讲解之使用Python批量发送个性化邮件
python实战讲解之使用Python批量发送个性化邮件
|
2月前
|
存储 安全 计算机视觉
用 Python 脚本实现电脑唤醒后自动拍照 截屏并发邮件通知
用 Python 脚本实现电脑唤醒后自动拍照 截屏并发邮件通知
|
3月前
|
存储 Shell API
Python 自动化指南(繁琐工作自动化)第二版:十八、发送电子邮件和短信
Python 自动化指南(繁琐工作自动化)第二版:十八、发送电子邮件和短信
57 0
|
3月前
|
移动开发 Python HTML5
Python办公自动化【发送普通邮件、发送HTML邮件、发送附件邮件-smtplib、批量发送邮件-smtplib、发送邮件-zmail】(八)-全面详解(学习总结---从入门到深化)
Python办公自动化【发送普通邮件、发送HTML邮件、发送附件邮件-smtplib、批量发送邮件-smtplib、发送邮件-zmail】(八)-全面详解(学习总结---从入门到深化)
42 0
|
5月前
|
人工智能 安全 程序员
使用 ChatGPT 帮助小学生编程入门系列之二:使用 Python 编程发送电子邮件
使用 ChatGPT 帮助小学生编程入门系列之二:使用 Python 编程发送电子邮件
55 0
|
8月前
|
测试技术 数据安全/隐私保护 Python
Python快速上手系列--邮件发送--详解篇
Python快速上手系列--邮件发送--详解篇
99 0
|
8月前
|
网络协议 网络安全 数据安全/隐私保护
|
8月前
|
数据采集 Python
技巧 | python定时发送邮件(自动添加附件)针不戳
技巧 | python定时发送邮件(自动添加附件)针不戳
技巧 | python定时发送邮件(自动添加附件)针不戳
|
9月前
|
Python
python发送带中文附件名的邮件
写项目练手时遇到的一个有趣事
90 0

热门文章

最新文章