反应无法使用SendGrid发送电子邮件 [英] React unable to send email with SendGrid
本文介绍了反应无法使用SendGrid发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Reaction发送电子邮件:
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.
我如何修复它?非常感谢!
SendGrid
推荐答案不允许您在浏览器中使用Java脚本直接发送电子邮件。
您需要设置服务器并使用服务器发送电子邮件(使用您最喜欢的后端框架/语言、Node.js、php、Java等)。
发送邮件的步骤如下:
- 在Reaction应用程序中写入电子邮件详细信息
- 使用电子邮件数据(收件人、标题、内容等)向您的服务器终结点发送POST请求(例如/sendmail)
- 接收服务器中的电子邮件数据并将其发送到SendGrid API
以下是有关其CORS策略的官方SendGrid文档:https://sendgrid.com/docs/for-developers/sending-email/cors/
这篇关于反应无法使用SendGrid发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文