code-8
code-8

Reputation: 58632

Sent Email via Python using Mailgun API

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 <[email protected]>",
              "to": ["[email protected]", "[email protected]"],
              "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 <[email protected]>",
              "to": ["[email protected]", "[email protected]"],
              "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 ?

Upvotes: 3

Views: 12448

Answers (3)

Luigi Palumbo
Luigi Palumbo

Reputation: 93

I believe you should change the email in "from" field to match the domain you registered on Mailgun. At least It seems so from Mailgun documentation, where the placeholder text for the "from" email is YOU@YOUR_DOMAIN_NAME

The code should be:

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

Also, make sure that in your script you call the function you are defining :-)

Upvotes: 1

0xInfection
0xInfection

Reputation: 2919

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.

Upvotes: 4

Punchlinern
Punchlinern

Reputation: 754

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

Upvotes: 1

Related Questions