python自定義發郵件功能

#!/usr/bin/env python #-*- coding:utf-8 -*- import socket import struct import fcntl import smtplib import time from email.mime.text import MIMEText from email.header import Header today = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 獲取主機名 hostname = socket.gethostname() #獲取主機IP,注意網卡名稱 def getip(ethname): s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0X8915, struct.pack('256s', ethname[:15]))[20:24]) if __name__=='__main__': ip = getip('eth0') sender = '[email protected]' receiver1 = '[email protected]' receiver2 = '[email protected]' subject = 'aliyun-%s backup is Successful'%hostname smtpserver = 'smtp.exmail.qq.com' username = '[email protected]' password = 'xxxxxx' msg = MIMEText( "At {}: \n 主機IP:%s \n 項目:xxx backup is Successful".format(today)%ip, 'text', 'utf-8' ) msg['From'] = Header("備份中心通知", 'utf-8') msg['To'] = Header(receiver1, 'utf-8') msg['To'] = Header(receiver2, 'utf-8') msg['Subject'] = Header(subject, 'utf-8') smtp = smtplib.SMTP() smtp.connect( smtpserver ) smtp.login( username, password ) smtp.sendmail( sender, receiver1, msg.as_string() ) smtp.sendmail( sender, receiver2, msg.as_string() ) smtp.quit()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章