美文网首页
发送指定邮箱信息来关闭电脑

发送指定邮箱信息来关闭电脑

作者: EZ | 来源:发表于2020-06-01 15:21 被阅读0次

参考 教程
另添加关机时发送邮件。打包成exe文件可发送邮箱来关机

主要还是怎么获得邮件内容,并根据定义的内容判断来是否进行关机等命令。

#!/usr/bin/env python
# coding: utf-8

import poplib
import os
import time
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
import smtplib
from email.mime.text import MIMEText
import datetime
#import requests


def sendmail(subject,content):
    email_host = 'smtp.qq.com' #可更改为其他邮箱
    email_user = 'your qq @qq.com'
    email_pwd = 'your passwordord'   
    maillist = '收件人'
    me = email_user
    #content = "this is a test"
    
    msg = MIMEText(content,'plain','utf-8')
    msg['Subject'] = subject
    msg['From'] = me
    msg['To'] = maillist
    
    smtp = smtplib.SMTP(email_host)
    smtp.login(email_user,email_pwd)
    smtp.sendmail(me,maillist,msg.as_string())
    smtp.quit()
    #print("email send success")


#编码转换函数
def decode_str(s):
    value,charset = decode_header(s)[0] #读取文本及编码方式
    #print(value) #b'\xe5\x85\xb3\xe6\x9c\xba'
    #print(charset) #utf-8
    if charset:
       value=  value.decode(charset) 
    #decoding, i forget to assign the decoding result to value ,decoding result is 关机
    #print('value=',value)
    return value


# 获取email 主题
def get_subject(msg):
    #get subject info
    Subject = msg.get('Subject')
    #print(Subject)
    #decode the subject
    Subject = decode_str(Subject) #get the email subject
   # print(Subject)
    return Subject


#judge the email subject 
def judge(Subject,e_addr):
    if (Subject == "关机" and e_addr== "发件人邮箱@qq.com"): 
        #same with or without brackets , if the subject is 关机
        return 1
    else:
        return 0


#searching the email_Subject
def Check_Subject(host,user,passwd):
    result = 0 
    try:
        pop_connect = poplib.POP3(host = host,timeout = 3)
        #print(pop_connect.getwelcome())
        
        pop_connect.user(user)
        pop_connect.pass_(passwd)
        #print('Messages: %s. Size: %s' % pop_connect.stat())
        #print(pop_connect.list())  
        #above emails info(b'+OK', [b'1 2748'], 8) |(b'+OK', [b'1 2747', b'2 2226'], 16)|<class 'tuple'>
        
        number = len(pop_connect.list()[1]) 
        #above 输出邮件id和 size1email [b'1 2747']  # 2 emails [b'1 2748', b'2 2747'] 
        print('number: ',number)
        #above how many email print('lenth of message: ',number)
        
        #search all emails
        for index in range(1,number+1):
            #get the first email
            #print(index,"="*30,pop_connect.retr(index)) #all info of the eamil
            msglines = pop_connect.retr(index)[1] # info of sender receiver and content et.al
            #print(index,"+"*20,msglines)  
            
            strr = b'\r\n'
            msg_content = strr.join(msglines).decode('utf-8') 
            #print('msg_conten 分行符',msg_content) #print infos in msg_conetent list
            #convert 
            msg = Parser().parsestr(msg_content) #parse email info 
            #print("msg"*5,msg)
            
            #get emial subject
            Subject = get_subject(msg) # call the function above,get email subject
            #print(Subject)
            #get email addr
            #print("frommmmmmmmmmmmmmm",msg.get('From')) 
            #frommmmmmmmmmmmmmm "=?gb18030?B?Z2VkYXBlbmc=?=" <>
            #print(parseaddr(msg.get('From'))) #tuple type after being parsed
            #('=?gb18030?B?Z2VkYXBlbmc=?=', '')
            email_addr = parseaddr(msg.get('From'))[1]
            #judge the email info 
            result = judge(Subject,email_addr) #call the function above , judge the subject and sender
            print(result)
            #print(index) #print the first message
            # action performed according the result 1 or 0
            if result == 1:
                pop_connect.dele(index)    #delet this  email for shutting down 
                break
        #sign out 
        pop_connect.quit() #quit after searching all the emails and return 1 or 0
        return result
    except Exception as e:
        #print(e)
        quit() #不确定作用


#main 
def main():
    host = 'pop.qq.com'
    user = 'qq@qq.com'
    password = '密码'
    aa = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    content = "您的电脑将被关机,关机时间约为:{}".format(aa)
    subject = '电脑关机提醒'
    while 1:
        result = Check_Subject(host,user,password)
        if result == 1: #result is 1 when  the subject  is "关机",
            sendmail(subject,content)  #send email 
            time.sleep(5)
            cmd = 'cmd /k shutdown /s /t 60'
            #print("get shutdown info")
            os.system(cmd)
            break  # when result is 1 , excuting the command abd break else sleep for 10s
        time.sleep(60)
            

main()


相关文章

  • 发送指定邮箱信息来关闭电脑

    参考 教程另添加关机时发送邮件。打包成exe文件可发送邮箱来关机 主要还是怎么获得邮件内容,并根据定义的内容判断来...

  • 一个简易邮件群发软件设计与实现

    1 需求概述 指定一批邮箱地址,使用指定的邮箱发送指定的内容。 2 功能需求 配置文件配置用于发送的邮箱信息 邮件...

  • 电脑开机后发送邮件至指定邮箱

    参照 原文 增加了在指定时间判断网络是否连接,只要是在指定时间联网就可以发送邮件,超过指定时间不发送邮件。具体代码...

  • 【素·日记录】2020年4月21日(周二)

    工作记录: 1.汇总整理网络宣传稿件并发送至指定邮箱。 2.编辑25信息《开学细部署,未雨先绸缪》,26期信息《开...

  • Python泡妹爬虫,用Python发送天气预报邮件!

    此次的目标是爬取指定城市的天气预报信息,然后再用Python发送邮件到指定的邮箱。 一,爬取天气预报 1、首先是爬...

  • python实现自动关机

    为了躺在床上能够关掉电脑,用python写了个小程序。原理:关机代码 在我要睡觉的时候给指定的邮箱发送 关机指令,...

  • Node.js使用nodemailer发送邮件

    使用场景: 服务器出错之后自动发送邮件到指定邮箱, 从而达到异常监控通知的效果 举例: 使用我的qq邮箱发送到我的...

  • 邮件发送

    应用场景:系统找回密码,严重码发送邮箱,或者预警信息发送通知者邮箱,并做日志记录 公共类:EmailHelper ...

  • 如何调用系统发送信息?

    第一种方法使用MFMessageComposeViewController系统的邮箱系统来发送信息 添加库: 引入...

  • [HOW] 如何将微信聊天记录导出

    方案一: 1、将微信绑定我的邮箱 2、选中要导出的聊天记录 3、通过邮箱发送到你的指定邮箱 4、打开邮箱即可 优点...

网友评论

      本文标题:发送指定邮箱信息来关闭电脑

      本文链接:https://www.haomeiwen.com/subject/bgqszhtx.html