简体   繁体   English

Django:send_mail无效[仅用于生产]

[英]Django: send_mail not working [only for production]

I'm having issues with my send_mail function in my production environment in Heroku. 我在Heroku的生产环境中的send_mail函数出现问题。

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). 我的settings.py在本地和生产环境中都是相同的,当我在localhost进行测试时,Gmail可以正确发送电子邮件,但是由于某种原因,我收到了500 SERVER ERROR消息,甚至没有从Django中获取错误日志我的管理员电子邮件(可能出于相同的原因)。

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. 相同的gmail已经为本地开发完成了此操作,因此我认为问题不存在。

I'm using Python3.6, Django1.11 and Heroku. 我正在使用Python3.6,Django1.11和Heroku。

Here's my code: 这是我的代码:

settings.py 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 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@gmail.com'
        to_email = ['mail@geoeng.com.br']

        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)

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). Gmail对我从弗吉尼亚州的Ashburn,EUA(据说是Heroku负责的地方)访问我的帐户感到不满意。 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 更改密码并开始工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM