使用mailgun和python验证是否存在电子邮件

时间:2018-03-25 23:22:32

标签: python email mailgun

Mailgun有一个电子邮件验证器,他们有一个示例响应,但我不知道如何使用它。我想验证“is_valid”是真还是假,并清理我的错误电子邮件的电子邮件列表。当我打印函数调用时,我得到Response [401]。如果is_valid为false,我怎么问它?

def get_validate(email):
        return requests.get(
            "https://api.mailgun.net/v3/address/validate",
            auth=("api", "key"),
            params={"address": email})


with open('emails.csv', 'r') as file:
    reader = csv.reader(file)
    for i in reader:
        s = ''
        try:
            print(i[0])
            s = s + i[0]
            print(get_validate(s))
        except IndexError:
            pass

样本回复是:

{
    "address": "foo@mailgun.net",
    "did_you_mean": null,
    "is_disposable_address": false,
    "is_role_address": false,
    "is_valid": true,
    "parts": {
        "display_name": null,
        "domain": "mailgun.net",
        "local_part": "foo"
    }
}

3 个答案:

答案 0 :(得分:0)

Serial.available() > 0是身份验证失败,这是因为success: function(response) { console.log(response); } 是无效的API密钥。当您在mailgun注册时,您的帐户会获得一个API密钥,这需要用来代替dataType: 'text' 。请参阅mailgun文档here

答案 1 :(得分:0)

def get_validate(email):
    return requests.get(
        "https://api.mailgun.net/v3/address/validate",
        auth=("api", "pubkey"),
        params={"address": email})


emails = list()

with open('emails.csv', 'r') as file:
    reader = csv.reader(file)
    for i in reader:
        s = ''
        try:
            #print(i[0])
            s = s + i[0]
            response = get_validate(s)
            status = response.status_code
            content = response.headers['content-type']
            valid = response.json()['is_valid']
            emails.append((s, valid))
        except IndexError:
            pass
    with open('clean_emails.csv', 'w') as outfile:
        writer = csv.writer(outfile)
        writer.writerows(emails)

这是最终的解决方案。

答案 2 :(得分:0)

一种简单的方法是将CSV上传到Mailgun列表,然后使用MailboxValidator导入列表并执行验证。

https://www.mailboxvalidator.com/resources/articles/how-to-import-email-list-from-mailgun/

如果您计划稍后使用Mailgun发送电子邮件,这是最实用的方法。

但如果您只是在寻找电子邮件验证API,请尝试使用MailboxValidator免费API。

免费API密钥:https://www.mailboxvalidator.com/pay/9

API文档:https://www.mailboxvalidator.com/api-single-validation

您每30天可获得300次免费验证。

用于调用API的示例Python代码

import httplib
import urllib
import hashlib

p = { 'key': 'Enter_License_Key', 'format': 'json', 'email': 'Enter_Email' }

conn = httplib.HTTPConnection("api.mailboxvalidator.com")
conn.request("GET", "/v1/validation/single?" + urllib.urlencode(p))
res = conn.getresponse()
print res.read()