简体   繁体   中英

Verify if an email exists using mailgun and python

Mailgun has an email validator that they have a sample response for but I don't know how to use it. I want to verify that "is_valid" is true or false and clean my email list of bad emails. I get Response [401] when I print the function call. How do I ask it if is_valid is 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

The sample response is:

{
    "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"
    }
}

401 is an authentication failure, and this is because "key" is an invalid API key. When you signed up at mailgun your account was given an API key, and this needs to be used in place of "key" . See mailgun documentation here

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)

This is the final solution.

One easy way is that you upload your CSV into a Mailgun list and then use MailboxValidator to import the list and perform the verification.

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

This is the most practical method if you are planning to use Mailgun to send out your emails later on.

But if you are just looking for an email verification API, try the MailboxValidator free API.

Free API key: https://www.mailboxvalidator.com/pay/9

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

You get 300 free verification every 30 days.

Sample Python code for calling the API

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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