参考 教程
另添加关机时发送邮件。打包成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()
网友评论