Python發郵件示例

g3mc 10年前發布 | 2K 次閱讀 Python

[root@his-web152 scripts]# more sendmail.py

!/usr/bin/env python2

coding: utf-8

import smtplib import datetime

from email.mime.text import MIMEText from email.header import Header

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

sender = 'monitor@163.com' receiver = ['ywb@163.com','xxx@163.com'] subject = 'Pro_Weekly_DBmonitor' smtpserver = 'mail.163.com' username = 'monitor' password = 'secrets'

f = open("/dba/dbaBackup/backup.log", "r")

content = f.read()

f.close()

content = 'More see the attached file'

msg = MIMEMultipart('related') msg['Subject'] = 'PostgreSQL_MONITOR'

--獲取時間 today=datetime.datetime.now().strftime('%Y-%m-%d')

--要發送的附件 filename='/home/postgres/report/DB'+str(today)+'.report.txt'

att = MIMEText(open(file_name,'rb').read(), 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="DB_report.txt"' msg.attach(att)

--發送所需要調用的smtp smtp = smtplib.SMTP() smtp.connect('mail.163.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit()</pre>

 本文由用戶 g3mc 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!