使用 Python IMAP 获取发件人电子邮件地址

发布于 2024-10-19 18:27:04 字数 944 浏览 3 评论 0原文

我有这个 python IMAP 脚本,但我的问题是,每次我想获取发件人的电子邮件地址(发件人)时,我总是得到发件人的名字,后跟他们的电子邮件地址:

示例:

Souleiman Benhida <[email protected]>

我如何提取电子邮件地址([电子邮件受保护]

我做了之前在 PHP 中:

    $headerinfo = imap_headerinfo($connection, $count)
    or die("Couldn't get header for message " . $count . " : " . imap_last_error());
$from = $headerinfo->fromaddress;

但是,在 python 中我只能获取带地址的全名,如何单独获取地址?我目前使用这个:

    typ, data = M.fetch(num, '(RFC822)')
mail = email.message_from_string(data[0][1])
headers = HeaderParser().parsestr(data[0][1]) 
message = parse_message(mail)  #body
org = headers['From']

谢谢!

I have this python IMAP script, but my problem is that, every time I want to get the sender's email address, (From), I always get the sender's first name followed by their email address:

Example:

Souleiman Benhida <[email protected]>

How can i just extract the email address ([email protected])

I did this before, in PHP:

    $headerinfo = imap_headerinfo($connection, $count)
    or die("Couldn't get header for message " . $count . " : " . imap_last_error());
$from = $headerinfo->fromaddress;

But, in python I can only get the full name w/address, how can I get the address alone? I currently use this:

    typ, data = M.fetch(num, '(RFC822)')
mail = email.message_from_string(data[0][1])
headers = HeaderParser().parsestr(data[0][1]) 
message = parse_message(mail)  #body
org = headers['From']

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扶醉桌前 2024-10-26 18:27:04

只需再一步,使用 email.utils

email.utils.parseaddr(地址)

将地址(应该是某些包含地址的字段(例如“收件人”或“抄送”)的值)解析为其组成的真实姓名和电子邮件地址部分。返回该信息的元组,除非解析失败,在这种情况下,返回 ('', '') 的 2 元组。

注意:最初引用的是 rfc822,现已弃用。

Just one more step, using email.utils:

email.utils.parseaddr(address)

Parse address – which should be the value of some address-containing field such as To or Cc – into its constituent realname and email address parts. Returns a tuple of that information, unless the parse fails, in which case a 2-tuple of ('', '') is returned.

Note: originally referenced rfc822, which is now deprecated.

來不及說愛妳 2024-10-26 18:27:04
to = email.utils.parseaddr(msg['cc'])

这对我有用。

to = email.utils.parseaddr(msg['cc'])

This works for me.

过度放纵 2024-10-26 18:27:04

我的外部库 https://github.com/ikvk/imap_tools
让您可以处理邮件而不是阅读 IMAP 规范。

from imap_tools import MailBox, A

# get all emails from INBOX folder
with MailBox('imap.mail.com').login('[email protected]', 'pwd', 'INBOX') as mailbox:
    for msg in mailbox.fetch(A(all=True)):
        print(msg.date, msg.from_, msg.to, len(msg.text or msg.html))

msg.from_、msg.to - 解析的地址,例如:'[电子邮件受保护]'

My external lib https://github.com/ikvk/imap_tools
let you work with mail instead read IMAP specifications.

from imap_tools import MailBox, A

# get all emails from INBOX folder
with MailBox('imap.mail.com').login('[email protected]', 'pwd', 'INBOX') as mailbox:
    for msg in mailbox.fetch(A(all=True)):
        print(msg.date, msg.from_, msg.to, len(msg.text or msg.html))

msg.from_, msg.to - parsed addresses, like: '[email protected]'

好菇凉咱不稀罕他 2024-10-26 18:27:04

我不喜欢现有的解决方案,因此我决定为 我的电子邮件发件人 创建一个姐妹库称为红盒

以下是如何搜索和处理电子邮件,包括获取发件人地址:

from redbox import EmailBox

# Create email box instance
box = EmailBox(
    host="imap.example.com", 
    port=993,
    username="[email protected]",
    password="<PASSWORD>"
)

# Select an email folder
inbox = box["INBOX"]

# Search and process messages
for msg in inbox.search(unseen=True):

    # Process the message
    print(msg.from_)
    print(msg.to)
    print(msg.subject)
    print(msg.text_body)
    print(msg.html_body)

    # Flag the email as read/seen
    msg.read()

我还写了扩展其文档。它还具有完全支持嵌套逻辑操作的查询语言。

I didn't like the existing solutions so I decided to make a sister library for my email sender called Red Box.

Here is how to search and process emails including getting the from address:

from redbox import EmailBox

# Create email box instance
box = EmailBox(
    host="imap.example.com", 
    port=993,
    username="[email protected]",
    password="<PASSWORD>"
)

# Select an email folder
inbox = box["INBOX"]

# Search and process messages
for msg in inbox.search(unseen=True):

    # Process the message
    print(msg.from_)
    print(msg.to)
    print(msg.subject)
    print(msg.text_body)
    print(msg.html_body)

    # Flag the email as read/seen
    msg.read()

I also wrote extensive documentation for it. It also has query language that fully supports nested logical operations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文