[Python] Python 发email 带附件 →→→→→进入此内容的聊天室

来自 , 2020-03-15, 写在 Python, 查看 183 次.
URL http://www.code666.cn/view/df0b8fb2
  1. from email.MIMEText import MIMEText
  2. from email.MIMEMultipart import MIMEMultipart
  3. import smtplib
  4.  
  5. mail_host = 'smtp.126.com'
  6. mail_user = 'xx@126.com'
  7. mail_pwd = 'xx'
  8. mail_to = 'xxzhao@gmail.com'
  9.  
  10.  
  11. msg = MIMEMultipart()
  12.  
  13. att = MIMEText(open('d:\\a.txt','rb').read(),'base64','gb2312')
  14. att["Content-Type"] = 'application/octet-stream'
  15. att["Content-Disposition"] = 'attachment;filename="hello.txt"'
  16. msg.attach(att)
  17.  
  18. message = 'content part'
  19. body = MIMEText(message)
  20. msg.attach(body)
  21. msg['To'] = mail_to
  22. msg['from'] = mail_user
  23. msg['subject'] = 'this is a python test mail'
  24.  
  25. try:
  26.     s = smtplib.SMTP()
  27.     s.connect(mail_host)
  28.     s.login(mail_user,mail_pwd)
  29.  
  30.     s.sendmail(mail_user,mail_to,msg.as_string())
  31.     s.close()
  32.  
  33.     print 'success'
  34. except Exception,e:
  35.     print e
  36.    
  37. #//python/1173

回复 "Python 发email 带附件"

这儿你可以回复上面这条便签

captcha