python 自动监控最新邮件并读取的操作

作者:姜小丫 时间:2023-02-04 12:58:51 

我就废话不多说了,大家还是直接看代码吧~


#zmail库:可以用几行代码帮我们收取一封邮件
import zmail
#输入账号和密码
server=zmail.server('13163964546@qq.com','jie110341')
#获取最新的一封邮件
mail=server.get_latest()
#读取邮件
#zmail.show(mail)
#读取邮件的部分内容
print(mail['subject'])
......
#读取附件 邮件 存放路径 如果有同名文件则覆盖
zmail.save_acctachment(mail,target_path=None,overwrite=True)

需要在电脑上下载zmail库

补充:Python邮箱实施监控电脑

我就废话不多说了,大家还是直接看代码吧~


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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

来源:https://blog.csdn.net/weixin_45554040/article/details/102991398

标签:python,自动监控,邮件
0
投稿

猜你喜欢

  • MySQL数据库可以用任意ip连接访问的方法

    2024-01-19 21:00:02
  • python利用hook技术破解https的实例代码

    2021-10-24 17:20:15
  • Django中Migrate和Makemigrations实操详解

    2021-09-12 02:34:23
  • Pytorch自己加载单通道图片用作数据集训练的实例

    2022-02-06 22:56:03
  • 2行css代码屏蔽网页挂马

    2008-09-29 18:54:00
  • Python常见数据结构详解

    2021-10-28 22:07:33
  • 在Python中使用HTML模版的教程

    2023-01-17 23:30:38
  • 对Python 2.7 pandas 中的read_excel详解

    2023-09-10 04:14:49
  • SQL Server内存遭遇操作系统进程压榨案例分析

    2024-01-14 06:58:51
  • CodeIgniter启用缓存和清除缓存的方法

    2023-11-22 17:04:55
  • 只需要这一行代码就能让python计算速度提高十倍

    2023-01-30 15:16:02
  • layer弹出子iframe层父子页面传值的实现方法

    2024-04-19 09:56:10
  • Python descriptor(描述符)的实现

    2021-12-22 09:07:56
  • PHP抽象工厂模式Abstract Factory Pattern优点与实现方式

    2023-05-25 03:04:57
  • Python计算字符宽度的方法

    2021-02-13 20:25:28
  • 解决安装python库时windows error5 报错的问题

    2022-06-14 05:26:04
  • JS实现倒计时图文效果

    2024-04-28 09:48:28
  • Python中elasticsearch插入和更新数据的实现方法

    2023-02-25 02:14:20
  • Oracle中SQL语句连接字符串的符号使用介绍

    2024-01-21 06:12:11
  • Django实现视频播放的具体示例

    2022-11-04 22:26:35
  • asp之家 网络编程 m.aspxhome.com