python 邮件解析_python_imap收取邮件 + 邮件内容解析

本文展示了如何使用Python的imaplib和email库来连接IMAP服务器,收取邮件,并进行邮件内容及附件的解析。通过创建一个Controller类,实现邮件的接收、解析邮件的头部信息(包括主题、发件人、收件人、抄送人、日期)以及邮件正文和附件的处理。解析过程中涉及到日期转换、编码解码以及MIME对象的操作。
摘要由CSDN通过智能技术生成

# -*- coding: utf-8 -*-

import email

import imaplib

import re

from openerp import http

from email import header

class Advertisefor(http.Controller):

@http.route('/advertisefor/getemaillist/', type='http', csrf=False, auth='user')

def get_eamil_list_action(self, **kw):

query = []

recv_config = http.request.env['emailrecvconfig'].search_read(query,order='create_date desc')

# 取最新的配置

recv_config = recv_config[0]

email_address = recv_config['address']

email_password = recv_config['pwd']

imap_server_host = recv_config['imap_server']

imap_server_port = recv_config['server_port']

try:

email_server = imaplib.IMAP4_SSL(host=imap_server_host, port=imap_server_port)

print "imap4----connect server success, now will check username"

except:

print"imap4----sorry the given email server address connect time out"

exit(1)

try:

email_server.login(email_address,email_password)

print "imap4----username exist, now will check password"

except:

print"imap4----sorry the given email address or password seem do not correct"

exit(1)

email_server.select()

email_count = len(email_server.search(None, 'ALL')[1][0].split())

for e in reversed(range(1,email_count+1)):

typ, email_content = email_server.fetch('{}'.format(e).encode(), '

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值