【python】使用python smtplib库发邮件添加cc,bcc
2017-07-10 14:32
411 查看
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@author
@mail
@date 2017/03/16
发送邮件
'''
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import tools.log_tool as log
def send_email(file_path, day):
# 发送邮件
sender = 'xxx@qq.com'
to_reciver = ['xxx@qq.com', ] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
cc_reciver = ['xxx@qq.com', ]
reciver = to_reciver + cc_reciver
# 创建一个带附件的实例
message = MIMEMultipart()
subject = 'xx报表'
message['Subject'] = Header(subject, 'utf-8')
message['From'] = sender
message['To'] = ','.join(to_reciver)
message['Cc'] = ','.join(cc_reciver)
# 邮件正文内容
message.attach(MIMEText('Dear all:\n\n\t附件为' + day + 'xx项目报表,请查收,谢谢!', 'plain', 'utf-8'))
# 构造附件1,传送当前目录下的 chart_line.xlsx 文件
att1 = MIMEText(open(file_path, 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename=%s.xlsx' % (day)
message.attach(att1)
try:
smtpObj = smtplib.SMTP('xxx.com')
smtpObj.login('xxx@qq.com', 'xxxpsd')
smtpObj.sendmail(sender, reciver, message.as_string())
log.warning("邮件发送成功")
except smtplib.SMTPException as e:
log.error(e)
log.error("Error: 无法发送邮件")
if __name__ == '__main__':
send_email(r'xxxx', 'xxx')
# -*- coding: utf-8 -*-
'''
@author
@date 2017/03/16
发送邮件
'''
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import tools.log_tool as log
def send_email(file_path, day):
# 发送邮件
sender = 'xxx@qq.com'
to_reciver = ['xxx@qq.com', ] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
cc_reciver = ['xxx@qq.com', ]
reciver = to_reciver + cc_reciver
# 创建一个带附件的实例
message = MIMEMultipart()
subject = 'xx报表'
message['Subject'] = Header(subject, 'utf-8')
message['From'] = sender
message['To'] = ','.join(to_reciver)
message['Cc'] = ','.join(cc_reciver)
# 邮件正文内容
message.attach(MIMEText('Dear all:\n\n\t附件为' + day + 'xx项目报表,请查收,谢谢!', 'plain', 'utf-8'))
# 构造附件1,传送当前目录下的 chart_line.xlsx 文件
att1 = MIMEText(open(file_path, 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename=%s.xlsx' % (day)
message.attach(att1)
try:
smtpObj = smtplib.SMTP('xxx.com')
smtpObj.login('xxx@qq.com', 'xxxpsd')
smtpObj.sendmail(sender, reciver, message.as_string())
log.warning("邮件发送成功")
except smtplib.SMTPException as e:
log.error(e)
log.error("Error: 无法发送邮件")
if __name__ == '__main__':
send_email(r'xxxx', 'xxx')
相关文章推荐
- Python中list的append方法添加,和使用下标取得。之中的元素还可以是其他的数据结构
- [Python]使用smtplib类库发邮件
- Python中使用Threads和Queue给tornado添加客户端
- PyCharm添加及使用Python live templates
- 使用python发邮件
- 使用python smtp发邮件代码
- 使用 Python 添加手机聚合平台教程
- 使用DocStrings为python函数添加注释
- 使用python一步一步搭建微信公众平台(三)----添加用户关注后的欢迎信息与听音乐功能
- 使用python在wordpress博客网站添加新文章示例
- 使用swig为python添加c扩展总结
- Python发送邮件——email库、smtplib库的使用
- Windows下使用VS2008编译OpenCV 2.1 添加Intel TBB和Python支持
- Cocos2d-x 游戏开发之巧用CCDelayTIme实现多个action有序执行,使用CCCallFunc添加action回调
- 二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)
- Windows下使用VS2008编译OpenCV 2.1 添加Intel TBB和Python支持
- 使用python偷偷发邮件
- Python发送邮件――email库、smtplib库的使用
- 使用pyinstaller 2.1将python打包并添加版本信息和图标
- Python Tkinter Entry和Text的添加与使用