python提前邮件内容和附件(草稿版)

如果没安装python环境,可以参考:

    https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
     
    from email.parser import Parser
    from email.header import decode_header
    from email.utils import parseaddr
    import os,  re
    import poplib
    import time, pymysql
    import mysql.connector
    
     
    # 输入邮件地址, 口令和POP3服务器地址:
    # email = input('Email: ')
    # password = input('Password: ')
    # pop3_server = input('POP3 server: ')
     
    # 新浪邮箱测试通过, 密码使用登陆密码
    # email = "[email protected]"
    # password = "password"
    # pop3_server = "pop.sina.cn"
     
     
    # qq邮箱测试通过, 使用授权码, 使用ssl
    emailaddr = "[email protected]"
    password = "xx"
    pop3_server = "imap.exmail.qq.com"
    config = {
        'host': '10.0.6.225',
        'user': '22',
        'password': '22',
        'port': 8904,
        'database': 'test',
        'charset': 'utf8'
    }
    
    img_path = '/www/public/assets/images'
    img_url = 'http://xx.com/assets/images/'
    
    def charset_utf8(field):    #{{{
        if field == None:
            new_field = ''
        else:
            sub_field = re.sub(r',\.{3}', '', field)
            new_field = sub_field.encode('utf8').strip()
        return new_field
    #}}}
    
    def guess_charset(msg):
        charset = msg.get_charset()
        if charset is None:
            content_type = msg.get('Content-Type', '').lower()
            pos = content_type.find('charset=')
            if pos >= 0:
                charset = content_type[pos + 8:].strip()
        return charset
    
    ##过滤表情符号
    emoji_pattern = re.compile("["
            u"\U0001F600-\U0001F64F"  # emoticons
            u"\U0001F300-\U0001F5FF"  # symbols & pictographs
            u"\U0001F680-\U0001F6FF"  # transport & map symbols
            u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                               "]+", flags=re.UNICODE)
    
    
    def remove_emoji(text):
        return emoji_pattern.sub(r'', text)
    
     
    def decode_str(s):
        value, charset = decode_header(s)[0]
        if charset:
            value = value.decode(charset)
        return value
     
    def save_ticket(title, email, desc, name):
        title = pymysql.escape_string(title)
        name = pymysql.escape_string(remove_emoji(name))
        desc = pymysql.escape_string(desc)
     # 导入MySQL驱动:
    # 注意把password设为你的root口令:
        conn = mysql.connector.connect(**config)
        cursor = conn.cursor()
    # 创建user表:
        timer = time.time()
    ## 插入一行记录,注意MySQL的占位符是%s:
        sql = "insert into support_tickets (title, `desc`,tel,`user_name`, game_id,status,add_time,add_admin) values ('%s', '%s', '%s', '%s',204,1,%s,1)"
        print('开始工单入库')
        old = check_exist(mail, title)
        if old != None :
            print('该工单已经入过库了')
        else:
            # print(sql % (title, desc, email, name,timer))
            try:
                cursor.execute(sql % (title, desc, email, name, timer))
                tid = int(cursor.lastrowid)
                print('工单入库成功,id:%s' % tid)
            except BaseException as e:
                print('工单入库失败')
                print(e)
        # 提交事务:
            conn.commit()
            cursor.close()
            return tid
    
    def check_exist(mail, title):
        conn = mysql.connector.connect(**config)
        cursor = conn.cursor()
        sql = "select id from support_tickets where tel = '%s' and title='%s' and is_del=0 limit 1"
        cursor.execute(sql % (mail, title))
        count = cursor.fetchone()
        conn.close()
        return count
    
    def get_last_email():
        conn = mysql.connector.connect(**config)
        cursor = conn.cursor()
        sql = "select id from email_log  order  by id desc limit 1"
        cursor.execute(sql)
        count = cursor.fetchone()
        conn.close()
        if count != None:
            return count[0]
        else:
            return  None
    
    
    def email_log(index):
        print('记录抓取id:%s' % index)
        conn = mysql.connector.connect(**config)
        cursor = conn.cursor()
        sql = "insert into email_log (id) values (%s)"
        cursor.execute(sql % index)
        conn.commit()
        cursor.close()
        return int(cursor.lastrowid)
    
    def save_files(files, tid):
        files = files.strip('|')
        files = files.split('|')
        conn = mysql.connector.connect(**config)
        cursor = conn.cursor()
        for file in files:
            if file == None or file == '':
                continue
            # 创建user表:
            url = img_url+file
            timer = time.time()
            ## 插入一行记录,注意MySQL的占位符是%s:
            sql = "insert into support_ticket_files (tid, url,add_time,add_admin) values ('%s', '%s', '%s',1)"
            try:
                cursor.execute(sql % (tid, url, timer))
                tid = int(cursor.lastrowid)
                print('附件入库成功,id:%s' % tid)
            except BaseException as e:
                print('附件入库失败')
                print(e)
        # 提交事务:
        conn.commit()
        cursor.close()
    
    
    def get_content(msg, indent=0):
        global files
        global title
        global desc
        global mail
        global user_name
        if indent == 0:
            for header in ['From','Subject']:
                value = msg.get(header, '')
                if value:
                    if header == 'Subject':
                        value = decode_str(value)
                        if title.strip() =='':
                            title = value
                    else:
                        hdr, addr = parseaddr(value)
                        name = decode_str(hdr)
                        value = u'%s <%s>' % (name, addr)
                        if mail.strip() =='':
                            mail = addr
                        if user_name.strip()=='':
                            user_name = name
    #             print('%s%s: %s' % ('  ' * indent, header, value))
        if (msg.is_multipart()):
            parts = msg.get_payload()
            for n, part in enumerate(parts):
                fileName = part.get_filename()
                contentType = part.get_content_type()
                if bool(fileName):
                    try:
                        timer = time.time();
                        newfile = '%s%s%s'% (timer, index, '.png')
                        filePath = os.path.join(img_path, newfile)
                        if not os.path.isfile(filePath) :
                            fp = open(filePath, 'wb')
                            fp.write(part.get_payload(decode=True))
                            fp.close()
                        # print('图片路径:'+filePath)
                        files += newfile+'|'
                    except :
                        print('error')
                get_content(part, indent + 1)
        else:
            content_type = msg.get_content_type()
            if content_type == 'text/plain':
                content = msg.get_payload(decode=True)
                charset = guess_charset(msg)
                if charset:
                    content = content.decode(charset)
                    if desc.strip()=='':    
                        desc = content;
    #             print('%sText: %s' % ('  ' * indent, content + '...'))
                
    #         else:
    #             print('%sAttachment: %s' % ('  ' * indent, content_type))
    
     
     
    # 连接到POP3服务器:
    server = poplib.POP3(pop3_server)
    # qq需要使用ssl
    # server = poplib.POP3_SSL(pop3_server)
    # 可以打开或关闭调试信息:
    # server.set_debuglevel(1)
    # 可选:打印POP3服务器的欢迎文字:
    # print(server.getwelcome().decode('utf-8'))
    # 身份认证:
    server.user(emailaddr)
    server.pass_(password)
    # stat()返回邮件数量和占用空间:
    # print('Messages: %s. Size: %s' % server.stat())
    # list()返回所有邮件的编号:
    resp, mails, octets = server.list()
    # 可以查看返回的列表类似[b'1 82923', b'2 2184', ...]
    # print(mails)
    # 获取最新一封邮件, 注意索引号从1开始:
    max_len = len(mails)
    # print('index %s'%index)
    #获取上次抓取的邮件索引
    last_id = get_last_email()
    print('上次抓取的邮件索引:%s' % last_id)
    ##抓取最近的5封
    ##也可以直接删掉抓取过的
    #server.dele(index)
    if last_id == None:
        index = max_len-5
    else:
        index = last_id+1
    while (index <= max_len):
        resp, lines, octets = server.retr(index)
        # lines存储了邮件的原始文本的每一行,
        # 可以获得整个邮件的原始文本:
        msg_content = b'\r\n'.join(lines).decode('utf-8')
        # 稍后解析出邮件:
        msg = Parser().parsestr(msg_content)
        files = ''        
        title = ''
        mail = ''
        desc = ''
        user_name = ''
        get_content(msg)
        tid = save_ticket(title, mail, desc, user_name)
        if tid != None:
            try:
                save_files(files, tid)
                email_log(index)
            except BaseException as e:
                print('保存附件失败')
                print(e)
        index += 1
        
    # 可以根据邮件索引号直接从服务器删除邮件:
    # server.dele(index)
    # 关闭连接:
    server.quit()

猜你喜欢

转载自blog.csdn.net/zhanqixuan22/article/details/88964300