【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:\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')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM