使用 Python IMAP 获取发件人电子邮件地址
我有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需再一步,使用 email.utils :
注意:最初引用的是
rfc822
,现已弃用。Just one more step, using email.utils:
Note: originally referenced
rfc822
, which is now deprecated.这对我有用。
This works for me.
我的外部库 https://github.com/ikvk/imap_tools
让您可以处理邮件而不是阅读 IMAP 规范。
msg.from_、msg.to - 解析的地址,例如:'[电子邮件受保护]'
My external lib https://github.com/ikvk/imap_tools
let you work with mail instead read IMAP specifications.
msg.from_, msg.to - parsed addresses, like: '[email protected]'
我不喜欢现有的解决方案,因此我决定为 我的电子邮件发件人 创建一个姐妹库称为红盒。
以下是如何搜索和处理电子邮件,包括获取
发件人
地址:我还写了扩展其文档。它还具有完全支持嵌套逻辑操作的查询语言。
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:I also wrote extensive documentation for it. It also has query language that fully supports nested logical operations.