利用python 删除腾讯企业邮箱中多余的邮件

一、背景

小编公司所有的是腾讯企业邮箱,因为公司自动化 所以总是自动给邮箱发送很多多余的邮件,但是有必须需要接收,于是小编写了一个脚本,定时清理这个邮件。

二、思路

首先,登录肯定是破不了的,于是小编采用的是cookie保持登录。

三、代码

# author:Administrator
# datetime:2019/4/10 12:31
# project_name:python_code_warehouse
# file_name:del_tengxun_email
# email:[email protected] 删除腾讯企业邮箱邮件 只需要替换self.cookie 以及sid即可,另外需要设置self.del_emails_user
import requests
from lxml import etree
import time
import logging
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

LOGGER = logging.getLogger("运行日志:")
LOGGER.setLevel(logging.DEBUG)
HANDLER = logging.FileHandler("log.txt", encoding='utf-8')
HANDLER.setLevel(logging.DEBUG)
CONHANDLER = logging.StreamHandler()
CONHANDLER.setLevel(logging.INFO)
FORMATER = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
LOGGER.addHandler(HANDLER)
LOGGER.addHandler(CONHANDLER)
HANDLER.setFormatter(FORMATER)
CONHANDLER.setFormatter(FORMATER)


class DelTengXunEmail(object):
    def __init__(self):
        self.host = "https://exmail.qq.com"
        self.header = {
            'User-Agent': 'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/73.0.3683.86Safari/537.36'}
        self.cookie = {'你的cookie': 'xxx'}
        self.sid = "你的sid"
        """需要删除的邮件发送者: 如果删除全部 ,设置成空元组即可"""
        self.del_emails_user = ()

    def del_tengxun_email(self):
        cnt = 0  # 循环次数变量
        while True:
            for i in range(100):
                LOGGER.info("当前正在删除%d页 邮件" % (i + 1))
                params = {'filetype': '', 'showattachtag': '', 'listmode': '', 'flag': '', 'fun': '', 'category': '',
                          'searchmode': '', 'stype': '', 'grpid': '', 'AddrID': '', 'ftype': '', 'page': i,
                          'folderid': '1', 'sid': self.sid, 's': 'inbox'}
                str_html = requests.get(url="https://exmail.qq.com/cgi-bin/mail_list", params=params, verify=False,
                                        headers=self.header, cookies=self.cookie).text
                email_info = self.get_email_info_by_strhtml(str_html)
                self.del_email_by_emails_info_list(*email_info)
                if i == 99:
                    cnt = cnt + 1
                    LOGGER.info("第%s次循环100页 结束" % cnt)
                if cnt == 50:  # 五次循环遍历后退出
                    LOGGER.info("遍历50次 100页循环完成  程序结束")
                    exit()

    def get_email_info_by_strhtml(self, str_html):
        """

        :param str_html: string类型的html
        :return: 返回email信息 [{email_id:'邮件id',email_in_user:'邮件发送者','email_name':'邮件标题','email_time':'邮件收到日期'},{}]
        """
        email_info = []
        email_dict = {}
        email_id = etree.HTML(str_html).xpath(
            '//table[@cellspacing="0"]/tr/td[@class="cx"]/input/@value')
        email_in_user = etree.HTML(str_html).xpath(
            '//table[@cellspacing="0"]/tr/td[@class="cx"]/input/@fa')
        email_name = etree.HTML(str_html).xpath(
            '//table[@cellspacing="0"]/tr/td[@class="gt tf"]/div[1]/u/text()')

        email_time = etree.HTML(str_html).xpath(
            '//table[@cellspacing="0"]/tr/td[@class="dt"]/div/text()')
        email_time = self.remove_blank_space(*email_time)
        for i in range(len(email_id)):
            email_dict["email_id"] = email_id[i]
            email_dict["email_in_user"] = email_in_user[i]
            email_dict["email_name"] = email_name[i]
            email_dict["email_time"] = email_time[i]
            email_info.append(email_dict)
            email_dict = {}
        if len(email_info) == 0 and "收件箱" not in str(str_html):
            LOGGER.error("cookie和sid失效,请更新!!")
            exit()
        return email_info

    @staticmethod
    def remove_blank_space(*ak):
        """将序列里面的每个值,去除空格

        :param ak: 接受的序列
        :return:序列
        """
        sequence = []
        for a in ak:
            sequence.append("".join(str(a).split()))
        return sequence

    def del_email_by_emails_info_list(self, *emails_info_list):
        """删除邮件

        :param emails_info_list: emails_info_list信息列表
        """
        del_emails_ids = ""
        for emails in emails_info_list:
            if len(self.del_emails_user):
                if emails.get("email_in_user") in self.del_emails_user:
                    LOGGER.info("收集到邮件(详情信息为{})".format(emails))
                    del_emails_ids = del_emails_ids + "%s&mailid=" % (emails.get("email_id"))
            else:
                LOGGER.info("收集到邮件(详情信息为{})".format(emails))
                del_emails_ids = del_emails_ids + "%s&mailid=" % (emails.get("email_id"))

        if del_emails_ids == "":
            LOGGER.info("当前页数无符合条件邮件....")
        else:
            self.del_email_by_email_id(del_emails_ids[:-8])

    def del_email_by_email_id(self, email_id):
        """删除邮件

        :param email_id: 邮件id
        """
        url = self.host + "/cgi-bin/mail_mgr?sid=%s" % self.sid
        body = {'mailaction': 'mail_del', 'location': 'mail_list', 'mailid': email_id,
                # email_id为多个mailid = ZC0714-VV3rH0RJdhxHZU8VQNECC8d&mailid=ZC0514-srpFfiXe8JrACiB6VNMdA8d&mailid=ZC0714-Xlb5_qV0LUcdeFIIZODKG8d&mailid=ZC0714-9Py_B1yNjOa8dlwGVOapE8d
                't': 'mail_mgr2', 'resp_charset': 'UTF8', 'ef': 'js', 'sid': self.sid}
        str_body = self.dict_to_str(**body)
        flag = requests.post(url=url, cookies=self.cookie, headers=self.header, verify=False, data=str_body).text
        time.sleep(2)
        if "mail_del successful" in str(flag):
            LOGGER.info("合计 %d 邮件删除成功!!" % str_body.count("mailid"))
        else:
            LOGGER.error("邮件删除失败! {}".format(flag))
            exit()

    @staticmethod
    def dict_to_str(**kw):
        """字典转换成字符串 key1=vlaue1&key2=value2

        :param kw: 字典
        """
        str_dict = ""
        for key in kw:
            str_dict = str_dict + "%s=%s&" % (key, kw.get(key))
        return str_dict[:-1]


if __name__ == "__main__":
    xiejiangpeng = DelTengXunEmail()
    xiejiangpeng.del_tengxun_email()

四、删除记录

利用python 删除腾讯企业邮箱中多余的邮件_第1张图片

利用python 删除腾讯企业邮箱中多余的邮件_第2张图片

五、构建定时任务

在jenkins中 

利用python 删除腾讯企业邮箱中多余的邮件_第3张图片利用python 删除腾讯企业邮箱中多余的邮件_第4张图片

 

时隔多日,现在增加了一波从已删除的界面恢复邮件到收件箱的功能,优化了部分逻辑。记住将sid和cookie填自己 如果有疑问的可以联系微信17673231234

# author:Administrator
# datetime:2019/4/10 12:31
# project_name:python_code_warehouse
# file_name:del_tengxun_email
# email:[email protected] 删除腾讯企业邮箱邮件 只需要替换self.cookie 以及sid即可,另外需要设置self.del_emails_user
import requests
from lxml import etree
import time
import logging
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

LOGGER = logging.getLogger("运行日志:")
LOGGER.setLevel(logging.DEBUG)
HANDLER = logging.FileHandler("log.txt", encoding='utf-8')
HANDLER.setLevel(logging.DEBUG)
CONHANDLER = logging.StreamHandler()
CONHANDLER.setLevel(logging.INFO)
FORMATER = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
LOGGER.addHandler(HANDLER)
LOGGER.addHandler(CONHANDLER)
HANDLER.setFormatter(FORMATER)
CONHANDLER.setFormatter(FORMATER)


class TengXunEmail(object):
    def __init__(self):
        self.host = "https://exmail.qq.com"
        self.header = {
            'User-Agent': 'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/73.0.3683.86Safari/537.36'}
        self.cookie = {'你的cookie': '你的cookie'}
        self.sid = "你的sid"
        """需要删除的邮件发送者: 如果删除全部 ,设置成空元组即可"""
        self.del_emails_user = ("[email protected]", "[email protected]")
        """不需要恢复的邮件发送者:如果需要恢复所有的,设置成空元组即可"""
        self.recovery_emails_user = ("[email protected]", "[email protected]", "[email protected]")

    def del_tengxun_email(self):
        cnt = 0  # 循环次数变量
        while True:
            for i in range(100000):
                params = {'filetype': '', 'showattachtag': '', 'listmode': '', 'flag': '', 'fun': '', 'category': '',
                          'searchmode': '', 'stype': '', 'grpid': '', 'AddrID': '', 'ftype': '', 'page': i,
                          'folderid': '1', 'sid': self.sid, 's': 'inbox'}
                str_html = requests.get(url="https://exmail.qq.com/cgi-bin/mail_list", params=params, verify=False,
                                        headers=self.header, cookies=self.cookie).text
                if "没有邮件" in str_html:
                    cnt = cnt + 1
                    break
                LOGGER.info("当前正在删除%d页 邮件" % (i + 1))
                email_info = self.get_email_info_by_strhtml(str_html)
                self.del_email_by_emails_info_list(*email_info)
                if cnt == 5:  # 五次循环遍历后退出
                    LOGGER.info("遍历%d 次 循环完成  程序结束" % cnt)
                    exit()

    def get_email_info_by_strhtml(self, str_html):
        """

        :param str_html: string类型的html
        :return: 返回email信息 [{email_id:'邮件id',email_in_user:'邮件发送者','email_name':'邮件标题','email_time':'邮件收到日期'},{}]
        """
        email_info = []
        email_dict = {}
        email_id = etree.HTML(str_html).xpath(
            '//table[@cellspacing="0"]/tr/td[@class="cx"]/input/@value')
        email_in_user = etree.HTML(str_html).xpath(
            '//table[@cellspacing="0"]/tr/td[@class="cx"]/input/@fa')
        email_name = etree.HTML(str_html).xpath(
            '//table[@cellspacing="0"]/tr/td[@class="gt tf"]/div[1]/u/text()')

        email_time = etree.HTML(str_html).xpath(
            '//table[@cellspacing="0"]/tr/td[@class="dt"]/div/text()')
        email_time = self.remove_blank_space(*email_time)
        for i in range(len(email_id)):
            email_dict["email_id"] = email_id[i]
            email_dict["email_in_user"] = email_in_user[i]
            email_dict["email_name"] = email_name[i]
            email_dict["email_time"] = email_time[i]
            email_info.append(email_dict)
            email_dict = {}
        if len(email_info) == 0 and "收件箱" not in str(str_html):
            LOGGER.error("cookie和sid失效,请更新!!")
            exit()
        return email_info

    @staticmethod
    def remove_blank_space(*ak):
        """将序列里面的每个值,去除空格

        :param ak: 接受的序列
        :return:序列
        """
        sequence = []
        for a in ak:
            sequence.append("".join(str(a).split()))
        return sequence

    def del_email_by_emails_info_list(self, *emails_info_list):
        """删除邮件

        :param emails_info_list: emails_info_list信息列表
        """
        del_emails_ids = ""
        for emails in emails_info_list:
            if len(self.del_emails_user):
                if emails.get("email_in_user") in self.del_emails_user:
                    LOGGER.info("收集到邮件(详情信息为{})".format(emails))
                    del_emails_ids = del_emails_ids + "%s&mailid=" % (emails.get("email_id"))
            else:
                LOGGER.info("收集到邮件(详情信息为{})".format(emails))
                del_emails_ids = del_emails_ids + "%s&mailid=" % (emails.get("email_id"))

        if del_emails_ids == "":
            LOGGER.info("当前页数无符合条件邮件....")
        else:
            self.del_email_by_email_id(del_emails_ids[:-8])

    def del_email_by_email_id(self, email_id):
        """删除邮件

        :param email_id: 邮件id
        """
        url = self.host + "/cgi-bin/mail_mgr?sid=%s" % self.sid
        body = {'mailaction': 'mail_del', 'location': 'mail_list', 'mailid': email_id,
                # email_id为多个mailid = ZC0714-VV3rH0RJdhxHZU8VQNECC8d&mailid=ZC0514-srpFfiXe8JrACiB6VNMdA8d&mailid=ZC0714-Xlb5_qV0LUcdeFIIZODKG8d&mailid=ZC0714-9Py_B1yNjOa8dlwGVOapE8d
                't': 'mail_mgr2', 'resp_charset': 'UTF8', 'ef': 'js', 'sid': self.sid}
        str_body = self.dict_to_str(**body)
        flag = requests.post(url=url, cookies=self.cookie, headers=self.header, verify=False, data=str_body).text
        time.sleep(2)
        if "mail_del successful" in str(flag):
            LOGGER.info("合计 %d 邮件删除成功!!" % str_body.count("mailid"))
        else:
            LOGGER.error("邮件删除失败! {}".format(flag))
            exit()

    @staticmethod
    def dict_to_str(**kw):
        """字典转换成字符串 key1=vlaue1&key2=value2

        :param kw: 字典
        """
        str_dict = ""
        for key in kw:
            str_dict = str_dict + "%s=%s&" % (key, kw.get(key))
        return str_dict[:-1]

    def recovery_email(self):
        cnt = 0  # 循环次数变量
        while True:
            for i in range(100000):
                params = {'filetype': '', 'showattachtag': '', 'listmode': '', 'flag': '', 'fun': '', 'category': '',
                          'searchmode': '', 'stype': '', 'grpid': '', 'AddrID': '', 'ftype': '', 'page': i,
                          'folderid': '5', 'sid': self.sid, 's': 'inbox'}
                str_html = requests.get(url="https://exmail.qq.com/cgi-bin/mail_list", params=params, verify=False,
                                        headers=self.header, cookies=self.cookie).text
                if "没有邮件" in str_html:
                    cnt = cnt + 1
                    break
                LOGGER.info("当前正在恢复%d页 邮件" % (i + 1))
                email_info = self.get_email_info_by_strhtml(str_html)
                self.recovery_email_by_emails_info_list(*email_info)
                if cnt == 5:  # 五次循环遍历后退出
                    LOGGER.info("遍历%d 次 循环完成  程序结束" % cnt)
                    exit()

    def recovery_email_by_emails_info_list(self, *emails_info_list):
        recovery_emails_ids = ""
        for emails in emails_info_list:
            if len(self.recovery_emails_user):
                if emails.get("email_in_user") not in self.recovery_emails_user:
                    LOGGER.info("收集到邮件(详情信息为{})".format(emails))
                    recovery_emails_ids = recovery_emails_ids + "%s&mailid=" % (emails.get("email_id"))
            else:
                LOGGER.info("收集到邮件(详情信息为{})".format(emails))
                recovery_emails_ids = recovery_emails_ids + "%s&mailid=" % (emails.get("email_id"))

        if recovery_emails_ids == "":
            LOGGER.info("当前页数无符合条件邮件....")
        else:
            self.recovery_email_by_email_id(recovery_emails_ids[:-8])

    def recovery_email_by_email_id(self, email_id):
        url = self.host + "/cgi-bin/mail_mgr?sid=%s" % self.sid
        body = {'location': 'mail_list', 'mailid': email_id,  # emaild是多个emaildid的组合
                'mailaction': 'mail_move',
                'destfolderid': '1', 't': 'mail_mgr2', 'resp_charset': 'UTF8', 'ef': 'js', 'sid': self.sid}
        str_body = self.dict_to_str(**body)
        flag = requests.post(url=url, cookies=self.cookie, headers=self.header, verify=False, data=str_body).text
        time.sleep(2)
        if "已将邮件成功移动" in str(flag):
            LOGGER.info("合计 %d 邮件恢复到收件箱成功!!" % str_body.count("mailid"))
        else:
            LOGGER.error("邮件恢复到收件箱失败! {}".format(flag))
            exit()


if __name__ == "__main__":
    xiejiangpeng = TengXunEmail()
    xiejiangpeng.recovery_email()
    # xiejiangpeng.del_tengxun_email()

恢复就运行xiejiangpeng.recovery_email() 如果是删除就运行xiejiangpeng.del_tengxun_email()

利用python 删除腾讯企业邮箱中多余的邮件_第5张图片

你可能感兴趣的:(python,小技巧)