Python邮箱实施监控电脑

本文介绍如何利用Python编程来实时监控电脑,并通过电子邮件通知用户关键事件。内容涵盖了Python邮件库的使用,以及如何设置监控脚本来捕获和报告系统信息。
摘要由CSDN通过智能技术生成
import smtplib
import poplib
import email
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import decode_header


def send_email(account, password, email_title, send_text="", file_names=None, file_dir="."):
    msg = MIMEMultipart()

    # msg = MIMEText(HTML, 'html')  -- 只能发送文本内容

    content = MIMEText(send_text, "plain", "utf-8")
    msg.attach(content)

    # 文件类型
    if isinstance(file_names, list):
        for file_name in file_names:
            send_file_path = file_dir + "/" + file_name
            part = MIMEApplication(open(send_file_path, 'rb').read())
            part.add_header('Content-Disposition', 'attachment', filename=file_name)
            msg.attach(part)
    elif isinstance(file_names, str):
        send_file_path = file_dir + "/" + file_names
        part = MIMEApplication(open(send_file_path, 'rb').read())
        part.add_header('Content-Disposition', 'attachment', filename=file_names)
        msg.attach(part)

    # msg['from'],msg['to']接收端显示的发件人与收件人
    msg['from'] = "奥巴马@163.com"
    msg['to'] = account
    msg['subject'] = email_title

    try:
        server = smtplib.SMTP()
        server.connect('smtp.163.com')
        server.login(account, password)
        # from_addr:发送地址; to_addrs:接收地址(字符串列表)
        server.sendmail(account, msg['to'].split(), msg.as_string())
    except Exception as e:
        print(e)


# 获取邮件标题
def get_email_subject(addr, password):
    # 设置连接网址,获取pop3协议的邮件读取对象
    read = poplib.POP3('pop.163.com', timeout=3600)

    # 输入邮件地址与邮件登录密码
    read.user(addr)  # 163邮箱用户名
    read.pass_(password)  # 163邮箱设置中的客户端授权密码

    # allEmails = (totalNum, totalSize)
    # 读取邮件信息(邮件总数,邮件尺寸)
    total_num, total_size = read.stat()

    # top(which,howmuch)
    #  获取最新的一封邮件(第几封邮件,获取多少封)
    top_email = read.top(total_num, 1)
    # print("***** start *****\n接收的数据为: {}\n*****  end  *****\n".format(top_email))
    #
    # print("***** start *****\n[解码前]获取的初始邮件内容: {}\n*****  end  *****\n".format(top_email[1]))

    # 解码邮件信息,将解码后的邮件信息存入tmp
    tmp = []
    for s in top_email[1]:
        tmp.append(s.decode())
    # print("***** start *****\n[解码后]的邮件内容为: {}\n*****   end   *****\n".format(tmp))

    # 将解码后的邮件内容拼接为字符串
    email_str = '\n'.join(tmp)
    # 将字符串类型解析为Message类型
    message = email.message_from_string(email_str)

    # print("***** start *****\n"
    #       "[解码前]的邮件字符串内容为: [数据类型]{}\n{}\n"
    #       "--------------------------------------------\n"
    #       "[解码后]的邮件字符串内容为: [数据类型]{}\n{}\n"
    #       "*****  end  *****\n"
    #       .format(type(email_str), email_str, type(message), message))

    # 获取邮件主题
    subject_str = message['subject']
    # print("***** start *****\n[解码前]邮件标题: {}\n*****  end  *****\n".format(subject_str))

    subject = decode_header(subject_str)
    # print("***** start *****\n[解码后]邮件标题: {}\n*****  end  *****\n".format(subject))

    content = subject[0][0]
    enc_type = subject[0][1]
    if enc_type:
        subject_decode = content.decode(enc_type)
    else:
        subject_decode = content
    return subject_decode, read, total_num
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值