Django,Mail不发送。 Sendgrid

时间:2017-07-31 13:09:32

标签: django sendgrid

我想通过发送网格从网站的联系表单向管理员发送邮件。电子邮件由于某种原因未送达

以下是 view.py

def contact(request):
title = 'Contact'
form = contactForm(request.POST or None)
confirm_message = None

if form.is_valid():
    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
    name = form.cleaned_data['name']
    comment = form.cleaned_data['comment']
    subject = 'Message from **'
    content = '%s %s' %(comment, name)
    from_email = form.cleaned_data['email']
    to_email = Email("***")
    try:
        mail = Mail(from_email, subject, to_email, content)
        response = sg.client.mail.send.post(request_body=mail.get())
    except:
        title="Sorry!"
        confirm_message = "Error sending message, Please try after sometime. Thank you!"
    title="Thanks"
    confirm_message = "Thanks for the message, We will get right back to you."
    form = None

context = {'title': title, 'form':form, 'confirm_message': confirm_message,}
template = "contact.html"
return render(request,template,context)

settings.py

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = '**'
EMAIL_HOST_PASSWORD = '***'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = "sgbackend.SendGridBackend"

电子邮件设置在zoho ..我无法收到任何电子邮件

1 个答案:

答案 0 :(得分:1)

首先确保安装了sendgrid-django。然后在您的设置中,它看起来不像您有sendgrid API密钥。尝试添加以下内容:

SENDGRID_API_KEY =“您的SendGrid API密钥”

看看是否有效。