Forums

Problem sending emails

I'm not sure if this is a problem with my code, or if there is some internal error. The code for sending an email seems to be pretty straightforward and after a couple days of poking around, I can't find where the error in my code is.

When running an email, my browser spins for minutes and then PA sends me PA specific an error page saying that something went wrong and there was an error "Error code: 504-loadbalancer". I've noticed some others have been having this error recently ( https://www.pythonanywhere.com/forums/topic/987/ ), so now I'm wondering if it's not a problem with my code? Any thoughts?

#settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_USE_TLS = True

#views.py
from django.core.mail import send_mail

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            send_mail(
                cd['subject'],
                cd['body'],
                cd.get('email', 'myemail@gmail.com'),
                ['myemail@gmail.com'],
                fail_silently=False)
            return render(request, 'content/generictext.html',
                {'text':'Your contact has been successfully sent, thank you.'})
    else:
        form = ContactForm()
    return render(request, 'content/contact.html', {'form': form})

oh yeah, by the way, can't see anything in the server errorlog, and no error is popping up anywhere that I can find.

That code looks like it should be working fine. Have you tried the email sending in a Django shell? It's possible that there's a really long timeout with some part of the email sending process and that's causing the request to take too long and so we're killing the worker. If you do it from a shell, and wait a long time (5 mins is when we kill workers for taking too long) you may see the actual error eventually.

Thanks Glenn, I did that and didn't get anything good, just saying that the connection was unexpectedly closed.

I found out there was suspicious behavior on my gmail account and google was blocking the connection.

If this happens to other people, here are the details to allow access: http://stackoverflow.com/questions/13465269/suspicious-sign-in-prevented-heroku-amazon-aws-gmail-smtp

Remember that while security is an annoying pain when nothing goes wrong, it is a lifesaver when it does!

Hi guys,

I'm running into my own 504-loadbalancer issue: essentially, my web app needs to generate a series of relatively complex PDF files in response to a user command. The error is entirely timeout related, as the app works fine for smaller datasets and only begins to raise "504-loadbalancer" when the dataset gets larger and the runtime longer. My questions:

1 - Is there any way for a worker to identify itself as a long-runner (in the 5 - 10 minute range) so that it doesn't get killed as rogue? and

2 - Failing that, is there any other well-accepted workaround that will still allow a reasonable degree of user involvement (i.e. that doesn't depending on cron batches to do the work offline)?

For point 1, I would think that the most likely scenario would be that your users would assume that your app was broken and hit reload (causing yet another long-running worker to spin up) and you'd soon run out of workers that way.

For point 2, not at the moment on PythonAnywere. We're considering adding things like task queues that would make that sort of thing fairly simple, but there's a lot to think about and do before that will be available.

Another possibility would be to look at the process of generating the PDF and see whether there are often calculated intermediate results that you can cache and reuse for different users/requests.

now I have Same Problem System Working on local host when deploy to pythonanywhere start Problem sending emails anyone help ?

I am using Flask, not Django but stumbled upon something that helped me debug an email issue. There is a setting for flask_mail: MAIL_DEBUG = 1 that you can stick in your app config. If set, it will print out the conversation between the web server and mail server. You can see if the email server is complaining about something.

When hosted here, you would need to look in the error log file to see the output.

@joeg -- that's useful, thanks for posting it!

@digitalamarmaris -- I see you also posted on a different thread and I've answered you there.