Advertisement
Guest User

Untitled

a guest
Mar 31st, 2017
1,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. # coding:utf-8
  2. """
  3. send mail with excel attachement
  4. """
  5. import csv
  6. import os
  7. import smtplib
  8. from email import encoders
  9. from email.mime.base import MIMEBase
  10. from email.mime.multipart import MIMEMultipart
  11. import smtplib
  12. from email.header import Header
  13. from email.mime.text import MIMEText
  14.  
  15.  
  16.  
  17.  
  18.  
  19. def send_main():
  20. """
  21. 给多人发送带附件的邮件
  22. :param file:
  23. :return:
  24. """
  25. server = smtplib.SMTP('smtp.163.com', 25)
  26. server.login('18818261892@163.com', 'LBQ139196')
  27.  
  28. msg = MIMEMultipart()
  29.  
  30. # 构造附件
  31. att1 = MIMEText(open('fiz.csv','rb').read(), 'base64', 'gb2312')
  32. att1["Content-Type"] = 'application/octet-stream'
  33.  
  34. # fileName以数据加日期命名
  35. fileName = "top_10_ip".decode('utf-8').encode('gbk') + ".csv"
  36. att1["Content-Disposition"] = 'attachment; filename=%s' % fileName
  37. msg.attach(att1)
  38.  
  39. # 写入邮件正文
  40. text = " Hi,这是定时发送,若有任何问题请与我联系。谢谢!"
  41. # 邮件正文乱码,所以在这里指定编码
  42. part1 = MIMEText(text, 'plain', _charset='utf-8')
  43. msg.attach(part1)
  44.  
  45. msg['From'] = '18818261892@163.com <18818261892@163.com>'
  46. # 主题要注意不能乱写,不然会被当垃圾邮件
  47. msg['Subject'] = '这是最近的情况'
  48. # msg['To'] = u'fiz <1848406889@qq.com>'
  49. strTo = ['fiz.lin@1cloudtech.com', '1848406889@qq.com']
  50. msg['to'] = ','.join(strTo)
  51. # msg['To'] = [u'林炳强<fiz.lin@1cloudtech.com>',u'fiz <1848406889@qq.com>']
  52.  
  53. # 构造附件
  54.  
  55.  
  56. try:
  57. server.sendmail('18818261892@163.com', ','.join(strTo), msg.as_string())
  58. print("mail has been send successfully.")
  59. # server.quit()
  60. except smtplib.SMTPException as e:
  61. print(e)
  62.  
  63. if __name__ == '__main__':
  64. send_main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement