在Django中使用Sendgrid发送电子邮件 [英] Send email with Sendgrid in Django

查看:116
本文介绍了在Django中使用Sendgrid发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Sendgrid从Django应用发送电子邮件。我尝试了许多配置,但是 Gmail 帐户仍然没有收到任何电子邮件。您可以在下面看到我的配置:

I'm trying to send emails from a Django app using Sendgrid. I've tried many configurations, but I still doesn't get any email on my Gmail account. You can see my configurations bellow:

settings.py:

SEND_GRID_API_KEY = 'APIGENERATEDONSENDGRID'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'mySENDGRIDusername'
EMAIL_HOST_PASSWORD = 'myEMAILpassword' #sendgrid email
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'MYEMAIL' #sendgrig email

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .forms import ContactForm
from django.core.mail import send_mail
from django.conf import settings
from django.template.loader import get_template


def index(request):
    return render(request, 'index.html')

def contact(request):

    success = False
    form = ContactForm(request.POST)
    if request.method == 'POST':
        name = request.POST.get("name")
        email = request.POST.get("email")
        message = request.POST.get("message")

        subject = 'Contact from MYSITE'

        from_email = settings.DEFAULT_FROM_EMAIL
        to_email = [settings.DEFAULT_FROM_EMAIL]

        message = 'Name: {0}\nEmail:{1}\n{2}'.format(name, email, message)

        send_mail(subject, message, from_email, to_email, fail_silently=True)

        success = True 
    else:
        form = ContactForm()
    context = {
        'form': form,
        'success': success
    }
    return render(request, 'contact.html',context)

你们知道会发生什么吗?我可以在本地获取电子邮件,也可以在终端中看到它,但是根本无法发送电子邮件。

Do you guys know what could be happening? I can get the email locally and I can see it in the terminal, but no email can be sent at all.

推荐答案

I尝试使用SMTP与Django设置sendgrid时遇到了一些麻烦。对我有用的是以下内容:

I struggled a bit when trying to set up the sendgrid with Django using SMTP. What worked for me is the following:

settings.py

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = 'sendgrid-api-key' # this is your API key
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'your-email@example.com' # this is the sendgrid email

我使用了此配置,并且运行正常,我强烈建议使用环境变量填充 EMAIL_HOST_PASSWORD DEFAULT_FROM_EMAIL ,因此代码如下:

I used this configuration, and it worked properly, and I strongly suggest using environment variables to fill EMAIL_HOST_PASSWORD and DEFAULT_FROM_EMAIL, so the code will be as follows:

import os # this should be at the top of the file

# ...

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD", "")
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = os.getenv("DEFAULT_FROM_EMAIL", "")

然后,在发送电子邮件时,我使用以下代码:

Then, when sending an email I used the following code:

from django.conf import settings
from django.core.mail import send_mail

send_mail('This is the title of the email',
          'This is the message you want to send',
          settings.DEFAULT_FROM_EMAIL,
          [
              settings.EMAIL_HOST_USER, # add more emails to this list of you want to
          ]
)

这篇关于在Django中使用Sendgrid发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆