Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28532
  • 博文数量: 9
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2015-11-09 17:27
个人简介

python技术爱好者,涉猎广泛,主攻大型网站架构与大数据。

文章分类
文章存档

2017年(3)

2015年(6)

我的朋友
最近访客

分类: Python/Ruby

2017-02-18 01:32:14

        互联网公司在运营的时候,时常需要一些统计数据,来获得一些反馈,或者作为运营的依据。我们公司的体量不是很大,所以数据统计,都是以邮件的方式,发送的。


点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # coding: utf-8

  3. '''
  4. 发送邮件'''

  5. import os
  6. import time
  7. import smtplib
  8. import mimetypes
  9. from email.header import Header
  10. from email.mime.text import MIMEText
  11. from email.mime.base import MIMEBase
  12. from email.mime.multipart import MIMEMultipart
  13. from email.encoders import encode_base64

  14. import config


  15. def mail_multipart(mail, **kwargs):
  16.     mail.update(kwargs) # enable key words arguments support.

  17.     server = smtplib.SMTP_SSL()
  18.     for i in range(5):
  19.         try:
  20.             server.connect('smtp.exmail.qq.com', 465)
  21.             server.login(
  22.                 config.QQ_MAIL_ACCOUNT,
  23.                 config.QQ_MAIL_PASSWORD)
  24.             break
  25.         except:
  26.             time.sleep(1)
  27.             continue
  28.     else:
  29.         raise StandardError(u'登录邮箱失败')
  30.     if 'to' not in mail:
  31.         raise StandardError(u'请指定收件人')
  32.     multipart = MIMEMultipart()
  33.     if 'attachment' in mail:
  34.         file_paths = mail['attachment']
  35.         for file_path in file_paths:
  36.             try:
  37.                 with open(file_path, 'rb') as f:
  38.                     data = f.read()
  39.                     file_name = os.path.basename(file_path)
  40.                     content_type = mimetypes.guess_type(file_name)[0]
  41.                     if content_type is None:
  42.                         content_type = 'application/octet-stream'
  43.                     file_body = MIMEBase(*content_type.split('/'))
  44.                     file_body.set_payload(data)
  45.                     file_body.add_header(
  46.                         'Content-Disposition',
  47.                         'attachment',
  48.                         filename=str(Header(file_name, 'UTF-8')))
  49.                     encode_base64(file_body)
  50.                     multipart.attach(file_body)
  51.             except IOError:
  52.                 continue
  53.     if 'text' in mail:
  54.         text = mail['text'] # utf-8 charset limited.
  55.         text = MIMEText(text, 'plain', 'utf-8')
  56.         multipart.attach(text)
  57.     if 'html' in mail:
  58.         html = mail['html']
  59.         html = MIMEText(html, 'html', 'utf-8')
  60.         multipart.attach(html)
  61.     receipts = mail['to']
  62.     sender = mail.get('from', config.QQ_MAIL_ACCOUNT)
  63.     multipart['From'] = sender
  64.     multipart['To'] = ','.join(receipts)
  65.     multipart['Subject'] = mail.get('subject')

  66.     for i in range(3):
  67.         try:
  68.             server.sendmail(
  69.                 sender, ','.join(receipts), multipart.as_string())
  70.             break
  71.         except:
  72.             continue
  73.     else:
  74.         raise StandardError(u'发送邮件失败')
  75.     server.close()


  76. if __name__ == '__main__':
  77.     pass



注意:
1、添加附件,指定filename时,需指定为UTF-8格式,现在网上大多数资料,都指定为gb2312,在大多数邮件客户端上都是可以正常显示的,但是在iOS自带的客户端就会乱码,所以需要UTF-8格式。
阅读(956) | 评论(0) | 转发(0) |
0

上一篇:8086

下一篇:Python爬取花骨朵影视排行数据

给主人留下些什么吧!~~