简体   繁体   English

验证电子邮件地址的域

[英]Verify the domain of an e-mail address

I am writing a Python script that checks the inbox of an IMAP account, reads in those e-mails, and replies to specific e-mails. 我正在编写一个Python脚本,用于检查IMAP帐户的收件箱,读取这些电子邮件,并回复特定的电子邮件。 However, for security reasons I need to make sure that the original sender of the e-mail came from a particular domain. 但是,出于安全原因,我需要确保电子邮件的原始发件人来自特定域。

I am reading the e-mails in Python using the email library and its message_from_string function: 我正在使用email库及其message_from_string函数阅读Python中的email

msg = email.message_from_string(data[0][1])

This gives me easy access to the sender via msg['from'] . 这使我可以通过msg['from']轻松访问发件人。 After some testing, I've found that this is generally in the format John Doe <JohnDoe@example.com> but I would think it could also be in the format JohnDoe@example.com . 经过一些测试,我发现这通常采用John Doe <JohnDoe@example.com>的格式,但我认为它也可以采用JohnDoe@example.com格式。

As I said, I want to make sure the domain of the sender's e-mail is, say, foobar.net . 正如我所说的,我想确保发件人电子邮件的域名是foobar.net What would be the best way to verify that? 什么是最好的验证方法? Should I use a regex so I can pull out the e-mail regardless what format msg['from'] is in? 我应该使用正则表达式,以便我可以提取电子邮件,无论msg['from']格式是什么? Or should I just do a string split on @ and then check the next 10 characters are foobar.net ? 或者我应该在@分割字符串然后检查接下来的10个字符是foobar.net吗? Something else entirely? 还有别的吗?

I'm really not a fan of the regex option, especially as so many people commented on answers here, it is very hard to catch all cases. 我真的不喜欢正则表达式选项,特别是因为有很多人在这里评论答案,所以很难捕捉所有案例。

Instead, I decided to use the Python email.utils.parseaddr function which will split the message "From" header to a tuple of (name, addr) . 相反,我决定使用Python email.utils.parseaddr函数,它将消息“From”标题拆分为(name, addr)元组。 From there, using addr.split('@') will split the e-mail address into its local part and domain part, and I can match the latter against the domain I'm verifying for. 从那里,使用addr.split('@')将电子邮件地址拆分为其本地部分和域部分,我可以将后者与我正在验证的域进行匹配。

So, the answer to my question is: 所以,我的问题的答案是:

msg = email.message_from_string(data[0][1])
addr = email.utils.parseaddr(msg['From'])[1]
domain = addr.split('@')[1]
if domain == "example.com":
    print "Verified requester"

You could try this pattern for simple domains: 您可以为简单域尝试此模式:

PATTERN 图案

(?<=@)\w+(\.\w+?)+?(?=>)

This pattern basically will check everything after @ and before > 这个模式基本上会检查@和之前>之后的所有内容

INPUT INPUT

1. John Doe <JohnDoe@example.com.no>
2. John Doe <JohnDoe@foobar.net>

OUTPUT OUTPUT

1. example.com.no
2. foobar.net

As mentioned in the comment you could use some sort of contains method, then I googled a bit for it and if you know the domain you could use something like this: 正如评论中提到的,您可以使用某种contains方法,然后我用Google搜索了一下,如果您知道域名,则可以使用以下内容:

PYTHON CODE PYTHON代码

if "@foobar.net" in msg['from']: continue

This code is basing on: 此代码基于:

stackoverflow.com stackoverflow.com

I'd definitely recommend to use a regex. 我绝对建议使用正则表达式。 However, you should also be aware that the 'from' field in emails is set by the email client and can be spoofed without much effort. 但是,您还应该知道电子邮件中的“发件人”字段是由电子邮件客户端设置的,可以不费吹灰之力地进行欺骗。 So you should think if maybe also checking for the sending mail server might be an option. 因此,您应该考虑是否也可以选择检查发送邮件服务器。

Cheers Hendrik 干杯亨德里克

import re
email = 'JohnDoe@example.com'
re.search(r'@((\w+?\.)+\w+)', email).group(1)

Note: 注意:

As given at here , correct regex to match email address is: 截至给出这里 ,正确的正则表达式匹配的电子邮件地址是:

(?:(?:\\r\\n)?[ \\t])*(?:(?:(?:[^()<>@,;:\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t] )+|\\Z|(?=[\\["()<>@,;:\\\\".\\[\\]]))|"(?:[^\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*"(?:(?: \\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\".\\[\\] \\000-\\031]+(?:(?:( ?:\\r\\n)?[ \\t])+|\\Z|(?=[\\["()<>@,;:\\\\".\\[\\]]))|"(?:[^\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*"(?:(?:\\r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\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[ "()<>@,;:\\\\".\\[\\]]))|"(?:[^\\"\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*"(?:(?:\\r\\n)?[ \\t]) *))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t]) +|\\Z|(?=[\\["()<>@,;:\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\ .(?:(?:\\r\\n)?[ \\t])*(?:[^()<>@,;:\\\\".\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z |(?=[\\["()<>@,;:\\\\".\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*\\>(?:( ?:\\r\\n)?[ \\t])*))*)?;\\s*)

Wow!! 哇!!

Doesn't the email library have functions to extract a sender and their domain? 电子邮件库是否具有提取发件人及其域的功能?

But if it doesn't, and if it's just one, known domain you want to check for, just check if the msg['from'] contains @foobar.net and be done with it. 但如果它没有,并且如果它只是一个你要检查的已知域名,只需检查msg['from']包含@foobar.net并完成它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM