Реагировать не удалось отправить письмо с SendGrid - PullRequest
0 голосов
/ 21 февраля 2019

Я использую реагирование для отправки писем:

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.REACT_APP_SENDGRID);
const msg = {
  to: 'test@example.com',
  from: 'test@example.com',
  subject: 'Sending with SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);

Но я получил следующую ошибку:

Access to fetch at 'https://api.sendgrid.com/v3/mail/send' 
from origin 'http://localhost:3000' has been blocked by CORS policy: 
Response to preflight request doesn't pass access control check: 
The 'Access-Control-Allow-Origin' header has a value 'https://sendgrid.api-docs.io' that is not equal to the supplied origin. 
Have the server send the header with a valid value, or, if an opaque response serves your needs, 
set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Как это исправить?Большое спасибо!

1 Ответ

0 голосов
/ 21 февраля 2019

Sendgrid не позволит вам отправлять электронную почту напрямую, используя Javascript в браузере.

Вам понадобится настроить сервер и использовать сервер для отправки электронной почты (используя ваш любимый серверный фреймворк / язык, Node.js, php, Java и т. Д.).

Шаги для отправки почты будут аналогичны следующим:

  1. Запишите данные электронной почты в приложении React
  2. Отправьте запрос POST на конечную точку вашего сервера (например,/ sendemail) с данными электронной почты (получатель, заголовок, содержимое и т. д.)
  3. Получите данные электронной почты на сервере и отправьте их на Sendgrid api

Вот официальная Sendgrid документация относительно их политики CORS: https://sendgrid.com/docs/for-developers/sending-email/cors/

...