簡體   English   中英

在 Django 中使用 Sendgrid 發送電子郵件

[英]Send email with Sendgrid in Django

我正在嘗試使用 Sendgrid 從 Django 應用程序發送電子郵件。 我嘗試了很多配置,但我的Gmail帳戶仍然沒有收到任何電子郵件。 您可以在下面看到我的配置:

設置.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

視圖.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 設置 Django 的 sendgrid 時,我有點掙扎。 對我有用的是以下內容:

設置.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
          ]
)

嘗試將這些添加到您在設置中的聲明的右側

SENDGRID_SANDBOX_MODE_IN_DEBUG=False

SENDGRID_ECHO_TO_STDOUT=False

在你的 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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM