您好,我正在尝试使用SendGrid从角度云函数发送电子邮件。
以下为示例:https://angularfirebase.com/lessons/sendgrid-v3-nodejs-transactional-email-cloud-function/
我写了以下函数
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const cors = require('cors')({ origin: true });
const SENDGRID_API_KEY="*******************************";
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);
exports.httpEmail = functions.https.onRequest((req, res) => {
cors( req, res, () => {
const toName = req.body.toName;
const toEmail = req.body.toEmail;
const msg = {
to: toEmail,
from: 'glkjrzklegklre@gmail.com',
subject: 'New Message',
text: `You have a new message`,
html: `You have a new message`,
};
return sgMail.send(msg)
.then(() => res.status(200).send('email sent!') )
.catch(err => res.status(400).send(err) )
});});
我只是从组件中调用它:
sendEmail() {
const data = {
toEmail: 'cbanzet@gmail.com',
toName: 'Jeff Delaney'
}
this.http.post(this.endpoint, data).subscribe()}
即使已写为“已发送电子邮件!”在控制台中,我必须遵循以下错误:
错误:SyntaxError:JSON中的意外令牌e在JSON.parse的位置0
我不知道该怎么办,我真的很想让它工作。