Sent Email via Python using Mailgun API

Viewed 5965

I try to send email via python using Mailgun

I have

def send_simple_message():
    return requests.post(
        "https://api.mailgun.net/v3/sandbox6247218655a94010b9840c23c2688fc7.mailgun.org",
        auth=("api", "key-********"),
        data={"from": "Excited User <bb@gmail.com>",
              "to": ["bb@outlook.com", "bb4@gmail.com"],
              "subject": "Hello",
              "text": "Testing some Mailgun awesomness!"})

I realize I forgot /messages

def send_simple_message():
    return requests.post(
        "https://api.mailgun.net/v3/sandbox6247218655a94010b9840c23c2688fc7.mailgun.org/messages",
        auth=("api", "key-********"),
        data={"from": "Excited User <bb@gmail.com>",
              "to": ["bb@outlook.com", "bb4@gmail.com"],
              "subject": "Hello",
              "text": "Testing some Mailgun awesomness!"})

I run it

python mail.py 

I still don't see any email. I also check my MailGun and my spam folder.

Any hints for me ?

2 Answers

According to the MailGun documentation, here you can see that the POST url should be in this format https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages

From your piece of code, I can see that sandbox6247218655a94010b9840c23c2688fc7.mailgun.org is YOUR_DOMAIN_NAME but your missing the format which is the /messages API endpoint.

So all you have to do is to add the /messages endpoint to your post URL. So it changes from https://api.mailgun.net/v3/sandbox6247218655a94010b9840c23c2688fc7.mailgun.org to https://api.mailgun.net/v3/sandbox6247218655a94010b9840c23c2688fc7.mailgun.org/messages.

Also note that if your domain is added to the EU region (instead of US) you have to change the post URL domain from api.mailgun.net to api.eu.mailgun.net (note the .eu), as read in the documentation.

So the entire URL would be https://api.eu.mailgun.net/v3/YOUR_DOMAIN_NAME/messages

Related