通过 Firebase 函数发送电子邮件时出现 Firebase SendGrid 错误 - 错误:错误请求
Posted
技术标签:
【中文标题】通过 Firebase 函数发送电子邮件时出现 Firebase SendGrid 错误 - 错误:错误请求【英文标题】:Firebase SendGrid error on sending email via Firebase functions - Error: Bad Request 【发布时间】:2020-01-12 00:33:13 【问题描述】:我正在使用 Firebase 和 SendGrid,我正在使用 Firebase 函数。我正在尝试在创建新用户时发送电子邮件。当我触发该功能时,我收到以下错误:
firestoreEmail: Function execution started
2019-09-10T08:13:49.245Z I firestoreEmail: Error: Bad Request
at Request.http [as _callback] (node_modules/@sendgrid/client/src/classes/client.js:124:25)
at Request.self.callback (node_modules/request/request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (node_modules/request/request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
code: 400,
message: 'Bad Request',
response:
headers:
server: 'nginx',
date: 'Tue, 10 Sep 2019 08:13:49 GMT',
'content-type': 'application/json',
'content-length': '365',
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/Clas-s-room/Basics/API/cors.html' ,
body: errors: [Array]
这是我的 Index.js:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const SENDGRID_API_KEY = 'exact API key here because functions.config().sendgrid.key doesn't work, says sendgrid is undefined';
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);
exports.firestoreEmail = functions.firestore
.document('Users/userId/Followers/followerId')
.onCreate(event =>
const userId = "for now exact id";
const db = admin.firestore()
return db.collection('Users').doc(userId)
.get()
.then(doc =>
const user = doc.data()
const msg =
to: "someEmailAddress@gmail.com",
from: 'hello@someEmailAddress.com',
subject: 'New Follower',
;
return sgMail.send(msg)
)
.then( () => console.log('email sent!') )
.catch( (err) => console.log(err) )
)
附:我正在关注 Fireship 的教程https://www.youtube.com/watch?v=JVy0JpCOuNI&t=333s
编辑: 这些是数据库 SC first image second image
【问题讨论】:
你能提供你的firetsore db的截图吗 @ConstantinBeer 我编辑了帖子,并包含了 SC 您的Index.js
缺少行。你把它们排除在外了吗?另外,您是否尝试使用userId = "for now exact id"
获取文档,还是仅针对您的帖子进行更改?
@ConstantinBeer 我编辑了这些行,对不起,我的错误。它们从一开始就在文件中,忘记粘贴它们。我为帖子更改了“现在确切的 ID”,那里需要 SendGrid api 密钥。
【参考方案1】:
问题是 const msg 缺少一个 templateId 字段,如果使用 SendGrid,这显然是强制性的。 这是工作的 sn-p:
const msg =
to: "majasveljo@gmail.com",
from: 'hello@angularfirebase.com',
subject: 'New Follower',
templateId: 'd-03ff1102c6e647c08207f293fce1701f', <--- this was missing
;
【讨论】:
以上是关于通过 Firebase 函数发送电子邮件时出现 Firebase SendGrid 错误 - 错误:错误请求的主要内容,如果未能解决你的问题,请参考以下文章
尝试通过 Office 365 发送电子邮件时出现 SMTP 5.7.57 错误