[轉載]python使用python smtplib庫發郵件新增cc,bcc
阿新 • • 發佈:2018-11-04
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 = '[email protected]' to_reciver = ['[email protected]', ] # 接收郵件,可設定為你的QQ郵箱或者其他郵箱 cc_reciver = ['[email protected]', ] 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('[email protected]', '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')
作者:蘇徽.W
出處:http://www.cnblogs.com/perfe/
本文版權歸作者和部落格園共有,歡迎轉載,希望大家能夠多多評論交流哦。