Question

I'm trying to debug a view which gets an incoming mail from Mailgun to a Django installation on Heroku.

The view itself is directly taken from the Mailgun docs and if I manually call the URL then I get an OK response.

@csrf_exempt
def askfriend_emailresponse(request):
    from_email = "..."
    to_email = "..."
    if request.method == 'POST':
        sender    = request.POST.get('sender')
        recipient = request.POST.get('recipient')
        answer = request.POST.get('stripped-text', '')

        try:
            send_mail("inside post"+str(recipient[recipient.find("+")+1:recipient.find("@")]), answer, from_email, to_email)
        except Exception, e:
            raise e

    return HttpResponse('OK')

Also, if I just place a post to that URL through an external tool (I'm using Poster for this purpose) I can get the 200 (I had to add the @csrf_exempt for that but that's fine for now).

However, from the Mailgun logs I see that there is a 500 Error when forwarding the message and from Heroku I see there is an incoming request causing a 500:

2013-03-13T09:59:04+00:00 heroku[router]: at=info method=POST path=[url] host=[hostname].herokuapp.com fwd="198.61.253.112" dyno=web.1 queue=0 wait=0ms connect=1ms service=86ms status=500 bytes=102194

My questions after spending more than a day researching:

1) Any idea what could be wrong (would be nice) - and at least as important

2) How can I properly test this? Because Mailguns logs are very brief (500 - that's it!) and Heroku also doesn't really help there... Also, having to always deploy to Heroku first obviously is quite slow, hence, I'm wondering if that really is the "smart" way to develop... (all other things I obviously test locally but with an external e-mail coming through a POST through Mailgun I don't know how to test that locally)

Thanks!

Was it helpful?

Solution

I finally figured out what the problem was:

1) in the Mailgun tutorial, the route you create doesn't have a '/' at the end (example is: http://myhost.com/messages) - however, APPEND_SLASH caused problems for the POST redirect then. The solution is to simply append the ''/' at the end for the redirect

2) (potential further issue later in production) - do note that in Django 1.5 the allowed_host value was introduced which restricts the domains which are allowed to make POST requests. I believe this is only used in production environments (debug = false) but could cause problems later so make sure that you set these accordingly as well (I believe to allow mailgun.org as host).

Thanks to everybody who tried to help out!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top