gpt4 book ai didi

python - 如何从 InboundMailHandler 中提取仅电子邮件地址?

转载 作者:太空宇宙 更新时间:2023-11-04 10:51:52 25 4
gpt4 key购买 nike

关于 InboundMailHandler 的问题。

class MailHandle(InboundMailHandler):
def receive(self, mail_message):

mail_message.sender may contained "John Bolton <john78@mail.com>". How can i extract "john78@mail.com" from it?

最佳答案

我一直在使用这个正则表达式,效果很好。从理论上讲,设计一个无法通过此正则表达式的发件人是可能的,这就是我捕获异常并警告地址的原因。不过这还没有发生。

try:
sender = re.search('<?([^<>]+)>?$',mail.sender).group(1)
except AttributeError:
...

另一个答案与根据 RFC 2822 定义的官方电子邮件地址格式不兼容。

Normally, a mailbox is comprised of two parts: (1) an optional displayname that indicates the name of the recipient (which could be a personor a system) that could be displayed to the user of a mailapplication, and (2) an addr-spec address enclosed in angle brackets("<" and ">"). There is also an alternate simple form of a mailboxwhere the addr-spec address appears alone, without the recipient'sname or the angle brackets.

请注意,此片段提到了收件人,但如果您阅读规范,它也适用于发件人。

https://www.rfc-editor.org/rfc/rfc2822#section-3.4

关于python - 如何从 InboundMailHandler 中提取仅电子邮件地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13319275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com