Forums

Use mailgun API to send mail with a free account

Hi,

I would like to use django-anymail to send mail so I created a free account on mailgun and then I set the API key on my django application, which I host on a free PythonAnywhere account.

I thought that it was possible to make it work without verified the domain, but I'm having the following error in the logs when I tried the send_mail function :

anymail.exceptions.AnymailRequestsAPIError: Sending a message to test@mail.com from Ben & Mae <noreply@benetmae.pythonanywhere.com>
Mailgun API response 400 (BAD REQUEST):
{
  "message": "The domain is unverified and requires DNS configuration. Log in to your control panel to view required DNS records."
}

So I tried to add the domain benetemae.pythonanywhere.com on mailgun, but now I have to verified it by adding DNS records, which doesn't seem to be possible ?

If you have any recommendations for me on how to do it, I would be glad to hear it !

Benjamin

benetmae.pythonanywhere.com is not a valid domain to send email from. Check with mailgun on the requirements for sending emails. You may need to get a domain that you can control to verify it.

So you confirm there is now way to send mail using just a free pythonanwyhere account ?

I should try to see how to get my own domain and then use it in mailgun after following the procedure to verify it ?

Thanks for the quick answer by the way !

You may send emails from free pythonanywhere account. You can't send them from the email address like some_name@myaccountname.pythonanywhere.com.

Hi,

While Sending Out Emails I am getting the Below Error -

server = smtplib.SMTP("smtp.mailgun.org", 587)

File "/usr/lib/python3.7/smtplib.py", line 251, in init (code, msg) = self.connect(host, port) File "/usr/lib/python3.7/smtplib.py", line 336, in connect self.sock = self._get_socket(host, port, self.timeout) File "/usr/lib/python3.7/smtplib.py", line 307, in _get_socket self.source_address) File "/usr/lib/python3.7/socket.py", line 727, in create_connection raise err File "/usr/lib/python3.7/socket.py", line 716, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 111] Connection refused

It seems mailgun.org is not passing through.

Please help

just to clarify, you can use the mailgun or any other http api to send mail, but for smtp, we only allow gmail for that.

Thanks. Gmail does not allow unsecure connections from this server. I had to set allow lower secure connections in gmail settings, which i didn't. Instead I developed an web api for send messages and mails are now flowing Thanks again. INDIA

oh okay, thanks for letting us know. For future reference, I think gmail tends to work if you create an app specific password.

I think gmail tends to work if you create an app specific password.

Can confirm as I use that. You either need to lower your security settings (which I would advise against!) or generate an app specific password and use that to login via SMTP instead of your regular password. For added security you should store your password as an environment variable rather than storing it as just text within your code.

Excellent, thanks for confirming that it works for you!

Hi Conrad, i'm using gmail app specific password and some emails don't get sent.

just to confirm- some emails get sent and some don't?

I had a similar problem. I was never able to find the root of the issue but it seemed like there was a problem with the email being passed to Gmail from my code. Again, it didn't happen all the time so I just put it down to network issues being the reason (I've since moved my project to a local server instead of PythonAnywhere so this isn't a PA network issue!).

To 'solve' it what I ended up doing was that if an email failed to send I cached it, just using a new table in my database. Then I set up a scheduled task, using AppScheduler so that every hour or so (you can increase the frequency if your emails are more urgent) it tries to send all unsent emails.

Hi Conrad, yes some emails were sent after delay and some don't.

that sounds like perhaps gmail blocking some smtp calls, or the server receiving the email finding it spammy?

do you get any error messages when the emails don't send?

There's no error message. However, when i click on the send mail button on our backend several times, the emails get sent out sometimes in full, or at times, 6 out of 10.

that sounds like perhaps gmail blocking some smtp calls, or the server receiving the email finding it spammy?