zoukankan      html  css  js  c++  java
  • 【python】使用python smtplib库发邮件添加cc,bcc

    #!/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: 附件为' + 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函数式编程(一):高级函数
    Python高级特性:列表生成式
    Python高级特性:迭代
    Python高级特性:切片
    Python学习笔记
    关于相机拍照获取图片onActivityResult返回data 为null的问题
    191019
    状语和状语从句
    191018
    191017
  • 原文地址:https://www.cnblogs.com/perfe/p/6605686.html
Copyright © 2011-2022 走看看