【问题标题】:Send email with Sendgrid in Django在 Django 中使用 Sendgrid 发送电子邮件
【发布时间】:2018-10-23 07:24:10
【问题描述】:

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

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)

你们知道会发生什么吗?本地可以收到邮件,在终端可以看到,但是根本发不出去邮件。

【问题讨论】:

  • 您是否将电子邮件控制台后端更改为 smtp 后端?
  • 是的,我做到了!我在文档中读到它实际上是默认设置,但即使我更改它,它也不起作用。
  • 同时签入spam。您是否还尝试为您的 gmail 设置发送电子邮件?
  • 是的。我也查了垃圾邮件,什么都没有。我尝试更改电子邮件客户端,尝试使用outlook,但效果不佳。

标签: python django sendgrid


【解决方案1】:

在尝试使用 SMTP 设置带有 Django 的 sendgrid 时,我有点吃力。对我有用的是:

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_PASSWORDDEFAULT_FROM_EMAIL,所以代码如下:

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", "")

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

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
          ]
)

【讨论】:

  • 那么,上面的配置不再需要 SEND_GRID_API_KEY 了吗?
  • @DaviWesley,你不需要那个环境变量,但你应该在EMAIL_HOST_PASSWORD 中使用这个值。
  • 但是 EMAIL_HOST_PASSWORD 我在 sendgrid 中的密码或 api 密钥的值是多少?
  • EMAIL_HOST_USER 正是 'apikey'EMAIL_HOST_PASSWORD 是您的 sendgrid api 密钥。这清楚吗?
【解决方案2】:

尝试将这些添加到设置中您的声明的右下方

SENDGRID_SANDBOX_MODE_IN_DEBUG=False

SENDGRID_ECHO_TO_STDOUT=False

【讨论】:

    【解决方案3】:

    在你的 settings.py 文件上做这个设置

    EMAIL_BACKEND = 'sendgrid_backend.SendgridBackend'
    SENDGRID_SANDBOX_MODE_IN_DEBUG = False
    FROM_EMAIL = 'youremail@gmail.com' # replace with your address
    SENDGRID_API_KEY = os.environ.get('SENDGRID_API_KEY')
    

    如果你在本地服务器上运行,不要使用 debug true。

    【讨论】:

    • 并打开 sendgrid 网页并编辑您的 api 密钥以允许邮件服务,然后单击受限访问。
    猜你喜欢
    • 2018-11-08
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 2022-06-29
    • 2019-05-27
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多