A Campos
A Campos

Reputation: 803

Django: send_mail not working [only for production]

I'm having issues with my send_mail function in my production environment in Heroku.

My settings.py are identical for local and for production and Gmail is sending the emails correctly when I test in the localhost, but for some reason I'm getting a 500 SERVER ERROR and I'm not even getting the error logs from django in my Admin email (probably for the same reason).

I already did this before and it is really strange that this is happening. And the same gmail had already done this for the local development, so I don't think the problem is there.

I'm using Python3.6, Django1.11 and Heroku.

Here's my code:

settings.py

EMAIL_HOST = 'smtp.gmail.com'
from .passwords import EMAIL_HOST_USER
from .passwords import EMAIL_HOST_PASSWORD
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
SERVER_EMAIL = EMAIL_HOST_USER

from .passwords import ADMINS
MANAGERS = ADMINS

views.py

from django.conf import settings
from django.core.mail import send_mail
from django.http import HttpResponseRedirect
from django.shortcuts import render

from .forms import LandingPageMapasForm
def mapas(request):
    form = LandingPageMapasForm(request.POST or None)
    context = {
        "form": form,
    }
    if form.is_valid():
        obj = form.save(commit=False)
        # obj.user = self.request.user
        obj.save()

        form_empresa = form.cleaned_data.get('empresa')


        subject = '%s - Solicitação de Orçamento' %(form_empresa)
        contact_message = 'message'
        context = {}
        from_email = '[email protected]'
        to_email = ['[email protected]']

        send_mail(
            subject,
            contact_message,
            from_email,
            to_email,
            fail_silently = False,
        )

        return HttpResponseRedirect('http://geoeng.com.br/muito-obrigado/')

    return render(request, 'mapas.html', context)

Upvotes: 1

Views: 2123

Answers (1)

A Campos
A Campos

Reputation: 803

Okay, turns out it had nothing to do with my code. Gmail was not happy with my account being accessed from Ashburn, VA, EUA (where Heroku supposedly does it's thing). So I had to play a little bit with my configurations and in the end I changed the password for a new (more secure) one and everything started to work just fine.

In short:

THE ISSUE WAS WITH GMAIL

CHANGED THE PASSWORD AND IT STARTED TO WORK

Upvotes: 2

Related Questions