The Seven Year Itch.

MENU

Python 编写自动发送邮件脚本

August 12, 2016 • Read: 256 • 闲记

弃用,超级超级超级简洁,超级超级厉害的第三方 ->见文章->

   #-*- encoding: utf-8 -*-
import os, sys
import smtplib
from smtplib import SMTP_SSL
from email.header import Header
from email.mime.text import MIMEText




mailInfo = {
    "from": "xxxxxxxx@qq.com",
    "to": "xxxxxxx@163.com",
    "hostname": "smtp.qq.com",
    "username": "xxxxxxx@qq.com",
    "password": "这里的密码是写你开启smtp服务生成的密码",
    "mailsubject": "安全警告",
    "mailtext": "Your ssh is error ",
    "mailencoding": "utf-8"
}

if __name__ == '__main__':
    smtp = SMTP_SSL(mailInfo["hostname"])
    smtp.set_debuglevel(1)
    smtp.ehlo(mailInfo["hostname"])
    smtp.login(mailInfo["username"],mailInfo["password"])

    
    msg = MIMEText(mailInfo["mailtext"],"text",mailInfo["mailencoding"])
    msg["Subject"] = Header(mailInfo["mailsubject"],mailInfo["mailencoding"])
    msg["from"] = mailInfo["from"]
    msg["to"] = mailInfo["to"]
    smtp.sendmail(mailInfo["from"], mailInfo["to"], msg.as_string())

    smtp.quit()

配合ssh登录判别脚本,实现警报发送邮件功能!

QQ图片20160812150443.png

如果异地登录,发送报警邮件!

QQ截图20160812150017.png

Tags: None
Archives QR Code
QR Code for this page
Tipping QR Code