首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SendGrid邮件发送在我的django rest-api视图中不工作

SendGrid邮件发送在我的django rest-api视图中不工作
EN

Stack Overflow用户
提问于 2020-10-20 03:16:34
回答 1查看 339关注 0票数 0

我尝试在我的django & react项目中使用sendgrid添加发送邮件,但它不起作用。我在后端api视图中的代码如下:

代码语言:javascript
运行
AI代码解释
复制
import os
import json

from rest_framework.response import Response
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

def send_invitation(request):

  if request.method == 'POST':
    TO_EMAILS = [('My Email Address', 'My fullname'), ]
    FROM_EMAIL = 'my another email address'
    TEMPLATE_ID = 'send-grid template id'

    message = Mail(
      from_email=FROM_EMAIL,
      to_emails=TO_EMAILS)

    message.dynamic_template_data = {
        'subject': 'SendGrid Development',
        'place': 'New York City',
        'event': 'Twilio Signal'
    }
    # message.add_filter('templates', 'enable', '1')
    message.template_id = TEMPLATE_ID
    
    try:
      sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
      response = sg.send(message)
      code, body, headers = response.status_code, response.body, response.headers
      print(f"Response code: {code}")
      print(f"Response headers: {headers}")
      print(f"Response body: {body}")
      print("Dynamic Messages Sent!")
      return Response({'message': 'Your Invitation is sent successfully!'})
    except Exception as e:
      print("Error {0}".format(e))
      return Response({'error': 'Your Invitation is failed to send!'})

我已经在我的django后端的.env文件中添加了SENDGRID_API_KEY,并使用pipenv安装了sendgrid。错误如下所示;Error HTTP Error 403: Forbidden

如果描述不够充分,请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-29 16:19:28

这是因为Sendgrid Web API设置中的发件人电子邮件身份验证。我已经通过将发送者电子邮件地址添加到sendgrid web api单一身份验证列表中解决了这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64437952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档