在Django中使用Sendgrid发送电子邮件

编程入门 行业动态 更新时间:2024-10-21 11:28:00
本文介绍了在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' 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' 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' # 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' 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发送电子邮件

本文发布于:2023-11-06 06:38:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562963.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送电子邮件   Django   Sendgrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!