#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import smtplib
import email.mime.multipart
import email.mime.text
 
server = 'smtp.126.com'    #发送服务器
port = '25'                #发送端口
 
def sendmail(server,port,user,pwd,msg):
    smtp = smtplib.SMTP()
    smtp.connect(server,port)
    smtp.login(user, pwd)
    smtp.sendmail(msg['from'], msg['to'], msg.as_string())
    smtp.quit()
    print('邮件发送成功email has send out !')
 
 
if __name__ == '__main__':
    msg = email.mime.multipart.MIMEMultipart()
    msg['Subject'] = '服务器报警请注意!'
    msg['From'] = 'ron@126.com'      #发送者邮箱
    msg['To'] = 'xxx@139.com'        #接收者邮箱
    user = 'ron'                     #发送者邮箱用户名  
    pwd = 'password'                 #发送者邮箱密码
    content='%s\n%s' %('\n'.join(sys.argv[1:4]),' '.join(sys.argv[4:])) #格式处理,专门针对我们的邮件格式
 
    txt = email.mime.text.MIMEText(content, _charset='utf-8')
    msg.attach(txt)
 
    sendmail(server,port,user,pwd,msg)

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐