发送邮件小工具(python)

#!/usr/bin/python
# -*- coding:UTF-8 -*-
import sys
import smtplib
import email.mime.multipart
import email.mime.text

server = 'smtp.163.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'] = '18521309001@163.com'
    msg['To'] = 'xusujuan1989@126.com'
    user = '18521309001'
    pwd = '123qwe'  #客户端授权密码,非邮箱密码
    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)

 

posted @ 2017-11-17 15:35  Claire_xu  阅读(340)  评论(0编辑  收藏  举报