【问题标题】:Search Emails in Mailbox with Imap_tools & Mailparser使用 Imap_tools 和 Mailparser 在邮箱中搜索电子邮件
【发布时间】:2022-11-21 11:32:13
【问题描述】:

在整个网站上搜索后的第一个帖子。

我正在尝试在 outlook 邮箱中的电子邮件正文中搜索电子邮件地址。我正在使用 Imap_tools、MailParser 和 Beautiful Soup。我需要获取电子邮件正文中存在的任何电子邮件地址,以便在我正在编写的脚本的另一部分中使用。也许我做的太多了,但需要这个才能工作。

这是我到目前为止所拥有的。

with MailBox('outlook.office365.com').xoauth2('MAILBOX@domain.com', result['access_token'], 'INBOX') as mailbox:
        for msg in mailbox.fetch(A(seen= True, subject='SUBJECT', from_= 'EMAIL')):
            #to validate it's fetching the correct emails
            print(msg.date_str, msg.subject) 
                email_message = mailparser.parse_from_file_obj(msg.obj)
                soup = BeautifulSoup(email_message.body, "html.parser")
                print(soup)
                text = soup.get_text()
                # Find all email addresses in the body of the email
                email = re.findall(r'[\w\.-]+@[\w\.-]+', text)
                print(email)
                email = email[0]

这是我得到的错误

Traceback (most recent call last):
  File ".\testServPrinc.py", line 55, in <module>
    email_message = mailparser.parse_from_file_obj(msg.obj)
  File AppData\Roaming\Python\Python38\s
r.py", line 66, in parse_from_file_obj
    return MailParser.from_file_obj(fp)
  File \AppData\Roaming\Python\Python38\s
r.py", line 166, in from_file_obj
    s = fp.read()
AttributeError: 'Message' object has no attribute 'read'

所有帮助表示赞赏。

【问题讨论】:

  • msg.obj 是什么?它实际上是一个文件对象吗?您确定 parse_from_file_obj() 是正确的使用方法吗?
  • 不是。我实际上已经弄清楚了。结束使用 msg.html 并完全删除 mailparser。我想我只需要额外的 3 个小时就可以用 3 天和 3 个小时来解决这个问题。感谢您尝试提供帮助!

标签: search office365 imap imap-tools mailbox


【解决方案1】:
body = msg.text or msg.html 

一封信中可能有多个邮件表示形式

【讨论】:

    猜你喜欢
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    相关资源
    最近更新 更多