Mailgun Python Django command

Interesting service is recommended for bulk mailing and yet effective with different possibilities to track statistics or feedback loops from different providers in 1 place and possibility to create pools of different IPs also a standard and useful possibility for domain DKIM, SPF authentication. But I don't know why there is no python library. We will now use an example and compose a command python to be able to send emails via python requests..

We'll start with a code sample already provided by Mailgun.

def send_simple_message():



auth=("api", "YOUR_API_KEY"),

data={"from": "Excited User <mailgun@YOUR_DOMAIN_NAME>",

"to": ["", "YOU@YOUR_DOMAIN_NAME"],

"subject": "Hello",

"text": "Testing some Mailgun awesomness!"})

Minimum code example works with configuration but with a free account email flies directly into the SPAM box. So we will first level up reputation and modify the script to work with our test data from the database. Test mailboxes must be from different providers such as hotmail, gmail, etc.. That depends on which country you have to send to and which provider has the most mailboxes there..

​​In test mode, a man can only send mails to his own email. In the next step to be able to use 6,000 mails, you have to add your own domain. The procedure looks like this:

Then you need to add or modify DNS records so that mailgun configuration is in there.

Test DNS settings you'll see it appear when it's done..

On the domains page.

Gebruik mail tester om configuratie te testen en alle instellingen..

Sent emails via domain, but do not appear in the dashboard as sent and we have not received any emails. While it works without errors, the script previously tested with default domain to own email.. Now I will wait and see how long it takes. 11:04 May 2. Did an upgrade at 12:40 and nothing solved. Emails will not be sent forbidden 401 response from the server.. IP cannot be selected. Nothing works.. I'll see if it evolves further. Tomorrow I'll have to check, maybe a manual check of the account should be done by admins and it hasn't been done yet or anything like that. Tomorrow if not resolved I will open a ticket. Ticket created so we'll see.. Account not working. No error messages or anything...


Issue with sending messages through my account. Taking a look at my account, I do see that domain is on the EU Mailgun server, while the sandbox domain assigned on the account is on the US Mailgun Server. The servers have different endpoints based on their location. Below is the endpoint you need to call for each server, depending on where your domain is located.

US Region

EU Region

I would recommend changing the endpoint to the EU region on your API calls. If this doesn't resolve the issue, feel free to reach back out with the full call details so we can take another look.

With regards to your dedicated IP, you aren't automatically assigned a dedicated IP when choosing a plan that includes a dedicated IP in the pricing. If you would like a dedicated IP, you'll need to request one through your account.

Django Python Code example for Mailgun command

for item in cyclus:

    def send_message(self, item):
        message = self.create_message(item)
        response =, auth=("api", API_KEY), data=message)
        item.sent = True
        item.status = response.text
        return item

    def create_message(self, item):
        #unsub_headers['List-Unsubscribe'] = '<%s>' % (unsub_url)
        text = render_to_string('newsletter/template.txt',
                                    {'sub':item, })
        html = render_to_string('newsletter/template.html',
                                    {'sub':item, })
        subject = "Subject"
            subject = str( + " - " + subject
        data =  {"from":self.from_email,
                "subject": subject,
                "text": text,
                "html": html }
        return data