windows开机邮件短信通知Python实现

2016年12月16日 6697点热度 0人点赞 1条评论

最近入手surface pro 4,自己的笔电将作为主力生产工具,surface pro 4将作为外出携带本。特别地,我给两台windows本配置了开机邮件和短信通知。

具体实现思路如下:

使用mailgun的API作为邮件接口,阿里大于短信API作为短信接口。为windows的任务计划程序添加触发事件。

首先mailgun的API封装,实现三个功能:

  • 发送不含附件邮件
  • 发送含附件邮件
  • 发送不含正文,正文以文本附件形式的油价
# coding=utf-8
import smtplib
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email import utils
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.utils import parseaddr, formataddr


def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))


def send_book(addr, message, file, book):
    stmp_user = "youmail@you.com"
    password = "yourpassd"
    smtp_server = "smtp.mailgun.org"
    from_addr = "监控中心<send@you.com>"
    to_addr = addr
    a = "you@mail.coom"
    filename = file
    book = book
    '''邮件正文'''
    msg = MIMEMultipart('mixed')
    msg['From'] = _format_addr(from_addr)
    msg['To'] = _format_addr(to_addr)
    msg['Subject'] = Header(book[:book.find('.')], 'utf-8').encode()
    msg['Date'] = utils.formatdate(localtime=1)

    msg.attach(MIMEText(message, 'plain', 'utf-8'))

    with open(filename, 'rb') as f:
        mime = MIMEBase('application', 'octet-stream', filename=book)
        # 加上必要的头信息:
        mime.add_header('Content-Disposition', 'attachment', filename=book)
        mime.add_header('Content-ID', '<0>')
        mime.add_header('X-Attachment-Id', '0')
        # 把附件的内容读进来:
        mime.set_payload(f.read())
        # 用Base64编码:
        encoders.encode_base64(mime)
        # 添加到MIMEMultipart:
        msg.attach(mime)

    try:
        server = smtplib.SMTP(smtp_server, 25)
        server.set_debuglevel(1)
        server.login(stmp_user, password)
        server.sendmail(stmp_user, [to_addr], msg.as_string())
        server.quit()
    except smtplib.SMTPException:
        print("Error: 无法发送邮件")


def send_verif(addr, message, file, book):
 stmp_user = "youmail@you.com"
 password = "yourpassd"
 smtp_server = "smtp.mailgun.org"
 from_addr = "监控中心<send@you.com>"
 to_addr = addr
 a = "you@mail.coom"
    filename = file
    mbook = book
    '''邮件正文'''
    msg = MIMEMultipart('mixed')
    msg['From'] = _format_addr(from_addr)
    msg['To'] = _format_addr(to_addr)
    msg['Subject'] = Header(mbook[:mbook.find('.')], 'utf-8').encode()
    msg['Date'] = utils.formatdate(localtime=1)

    msg.attach(MIMEText(message, 'plain', _charset='utf-8'))
    with open(filename, 'rb') as f:
        mime = MIMEBase('application', 'octet-stream', filename=Header(mbook, 'utf-8').encode())
        # 加上必要的头信息:
        mime.add_header('Content-Disposition', 'attachment', filename=Header(mbook, 'utf-8').encode())
        mime.add_header('Content-ID', '<0>')
        mime.add_header('X-Attachment-Id', '0')
        # 把附件的内容读进来:
        mime.set_payload(f.read())
        # 用Base64编码:
        encoders.encode_base64(mime)
        # 添加到MIMEMultipart:
        msg.attach(mime)

    try:
        server = smtplib.SMTP(smtp_server, 25)
        server.set_debuglevel(1)
        server.login(stmp_user, password)
        server.sendmail(stmp_user, [to_addr], msg.as_string())
        server.quit()
    except smtplib.SMTPException:
        print("Error: 无法发送邮件")


def send_msg(addr, message, subject):
 stmp_user = "youmail@you.com"
 password = "yourpassd"
 smtp_server = "smtp.mailgun.org"
 from_addr = "监控中心<send@you.com>"
 to_addr = addr
 a = "you@mail.coom"
    filename = file
    '''邮件正文'''
    msg = MIMEMultipart('mixed')
    msg['From'] = _format_addr(from_addr)
    msg['To'] = _format_addr(to_addr)
    msg['Subject'] = _format_addr(subject)
    msg['Date'] = utils.formatdate(localtime=1)

    msg.attach(MIMEText(message, 'plain', 'utf-8'))



    try:
        server = smtplib.SMTP(smtp_server, 25)
        server.set_debuglevel(1)
        server.login(stmp_user, password)
        server.sendmail(stmp_user, [to_addr], msg.as_string())
        server.quit()
    except smtplib.SMTPException:
        print("Error: 无法发送邮件")

 

阿里大于短信API封装

import top.api
import datetime
now = datetime.datetime.now().strftime("%Y-%m-%d%H:%M")
# configure
def AliSmsSender():
 appkey = 121212121
 secret = "adadaddqdqdqwwqqwdwq"

 req = top.api.AlibabaAliqinFcSmsNumSendRequest()
 req.set_app_info(top.appinfo(appkey, secret))

 req.extend = ""
 req.sms_type = "normal"
 req.sms_free_sign_name = "贺兰驿"
 req.sms_param = "{name:'南国羽',date:'%s',toid:'101',state:'登录'}" % now
 req.rec_num = "接收用短信"
 req.sms_template_code = "模板号"
 try:
     resp = req.getResponse()
     print (resp)
 except Exception, e:
     print (e)

 

API调用Python实现:

# coding=utf-8
import mailgun
import ctypes
import datetime
import AliSmsSender
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
whnd = ctypes.windll.kernel32.GetConsoleWindow()
if whnd != 0:
    ctypes.windll.user32.ShowWindow(whnd, 0)
    ctypes.windll.kernel32.CloseHandle(whnd)
mailgun.send_msg('admin@nanguoyu.com', u'您的surface pro 4已被登录'+now, 'warning')

AliSmsSender()

 

触发器VB实现:

Set ws = CreateObject("Wscript.Shell") 
ws.run "cmd /c E:\StartMission.py",vbhide

打开计算机管理-系统工具-任务计划程序,然后创建定时程序,设置用户登录时,启动该VB程序

 

这只是一种满足我自己小需求的快捷实现,可优化地方很多。

 

Dong Wang

Master student of computer science at Uppsala University in Sweden. My primary research interests are deep learning, computer vision, federated learning and internet-of-things.

文章评论

  • 子午坊

    ppython用的人越来越多

    2018年6月5日
  • 此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据