在Heroku上发送电子邮件时出现服务器错误500

时间:2017-01-15 17:26:32

标签: python django heroku

我有一个Django项目。用户填写表单,然后我收到有关新订单的通知。 Localy,在我的电脑上一切正常。我在Heroku上部署我的应用程序,在网站上我无法发送电子邮件。我收到错误500。

这是我的代码:

settings.py

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'mymail@gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL='sitename'

views.py

from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.template.loader import render_to_string
from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
from .forms import OrderForm


def contact(request):
    company='no company'

    if request.method == 'POST' :
        form=OrderForm(request.POST)

        if form.is_valid():
            company=form.cleaned_data['company']

            try:
                msg_plain = render_to_string('../templates/publicity_agency/email.txt', {'company':company})
                msg_html = render_to_string('../templates/publicity_agency/email.html', {'company':company})
                msg_plain_client = render_to_string('../templates/publicity_agency/email_client.txt')
                msg_html_client = render_to_string('../templates/publicity_agency/email_client.html')
                send_mail('New order',msg_plain,'Notification <myemail@gmail.com>', ['myemail@gmail.com'], html_message=msg_html)
                send_mail('Order',msg_plain_client,'Order <myemail@gmail.com>', [email], html_message=msg_html_client)
            except BadHeaderError:
                return HttpResponse('Invalid header found')
            return redirect('contact')
    else:
        form=OrderForm()

    return render(request, 'publicity_agency/contacts.html', {'company':company})

0 个答案:

没有答案