【问题标题】:Not able to send contact form email with SendGrid on Nodejs无法在 Nodejs 上使用 SendGrid 发送联系表单电子邮件
【发布时间】:2020-04-11 13:38:08
【问题描述】:

我试图在 Nodejs 上实现一个联系表单并使用 SendGrid 表达,但它给了我403 Forbidden 错误,但我发送的发布请求返回200。我不知道我做错了什么,请我帮助解决这个问题。

这是我的全部route

const express = require('express')
const router = express.Router()
const ContacForm = require('../models/contact_form')
const fs = require('fs')
const path = require('path')
const sgMail = require('@sendgrid/mail');
const nodemailer = require("nodemailer");


router.get('/new', (req, res) => {
  res.render("contact_form/new") 
})

router.post('/', (req, res) => {
const output = `
    <p>You have a new Request</p>
    <h3>Contact Details </h3>
    <ul>
        <li>Name: ${req.body.name}</li>
        <li>Email: ${req.body.email}</li> 
    /ul>
<h3>Message</h3>
<li>Request: ${req.body.request}</li>
`;

sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
    to: 'chukwumakingley1@gmail.com',
    from: 'chukwumakingley1@gmail.com',
    subject: 'Sending with Twilio SendGrid is Fun',
    text: 'and easy to do anywhere, even with Node.js',
    html: output,
};

sgMail.send(msg, (error, contact)=> {
    if(error) {
        console.log(error)
        res.render("contact_form/new")
    }
 });
});

这些是我在终端上得到的错误消息响应

ResponseError: Forbidden
at node_modules/@sendgrid/client/src/classes/client.js:105:29
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 403,
message: 'Forbidden',
response: {
headers: {
  server: 'nginx',
  date: 'Sat, 11 Apr 2020 13:15:25 GMT',
  'content-type': 'application/json',
  'content-length': '281',
  connection: 'close',
  'access-control-allow-origin': 'https://sendgrid.api-docs.io',
  'access-control-allow-methods': 'POST',
  'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas- 
 acl',
  'access-control-max-age': '600',
  'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html'
},
 body: { errors: [Array] }
}
}

这是我的表格

<form method="POST" action="/contact_form">
  <label>Name</label>
  <input type="text" name="name" id="name" placeholder="Enter Your Name">
  <label>Email</label>
  <input type="text" name="email" id="email" placeholder="Enter Your Email">
  <label>Request</label>
  <textarea  name="request" id="request" placeholder="Enter Your  Prayer Request" cols="30" 
  rows="10"></textarea>
  <button type="submit"> Submit </button>
</form>

注意:我正在发送我的 SENDGRID_API_KEY 变量,它正在通过

这是我正在使用的那种 Sendgrid API

Integrate using our Web API or SMTP Relay

【问题讨论】:

    标签: node.js express sendgrid


    【解决方案1】:

    我是在花时间研究和阅读文档后才弄明白的。

    碰巧我需要进行额外的身份验证,称为Single Sender Verification 等。

    我更改了代码以便更好地理解错误

    sgMail.setApiKey(process.env.SENDGRID_API_KEY);
    const msg = {
      to: 'chukwumakingsley1@gmail.com',
      from: 'chukwumakingsley1@gmail.com',
      subject: 'Hello world',
      text: output
    };
    
    sgMail
      .send(msg)
      .then(() => {
        //Celebrate
        console.log('Email Sent!');
      })    
      .catch(error => {
    
        //Log friendly error
        console.error(error.toString());
        console.log(output)
    
        //Extract error msg
        const {message, code, response} = error;
    
        //Extract response msg
        const {headers, body} = response;
      });
    });
    

    改代码后,报错信息改为

    Forbidden (403)
    The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for- 
    developers/sending-email/sender-identity/ to see the Sender Identity 
    requirements
    

    从这个错误中,我阅读了 send-grids 上的错误文档。

    我认为上个月添加了额外的身份验证。

    【讨论】:

      【解决方案2】:

      您的 post 请求返回 200,因为您在出现错误时调用了 res.render("contact_form/new")

      if(error) {
          console.log(error)
          res.render("contact_form/new")
      }
      

      查看从 SendGrid 响应中获得的响应正文。它包含一系列错误,可能会提供有关您收到 403 原因的更多信息。

      【讨论】:

        【解决方案3】:

        我也遇到了同样的问题,从 2020 年 4 月 6 日开始,sendgrid 更改了一些免费测试认证的标准 :(。现在需要进行一些配置。 为我解决的问题是从他们那里遵循本教程。 https://sendgrid.com/docs/ui/sending-email/sender-verification/。只授权几封邮件发送请求

        【讨论】:

          猜你喜欢
          • 2021-10-23
          • 1970-01-01
          • 1970-01-01
          • 2021-04-13
          • 1970-01-01
          • 2012-04-30
          • 1970-01-01
          • 2019-06-14
          • 1970-01-01
          相关资源
          最近更新 更多