Node.js 如何使用SendGrid在ExpressJS中发送电子邮件
我正在尝试在上跟踪文档。作为测试,我尝试在用户创建帖子时发送电子邮件 这是我的密码: 在my package.json中:Node.js 如何使用SendGrid在ExpressJS中发送电子邮件,node.js,express,sendgrid-api-v3,Node.js,Express,Sendgrid Api V3,我正在尝试在上跟踪文档。作为测试,我尝试在用户创建帖子时发送电子邮件 这是我的密码: 在my package.json中: ... "dependencies": { "@sendgrid/client": "^6.4.0", ... 在posts.js中: const express = require('express'); const auth = require('../middlewares/authenticate'); const User = require('../m
...
"dependencies": {
"@sendgrid/client": "^6.4.0",
...
在posts.js中:
const express = require('express');
const auth = require('../middlewares/authenticate');
const User = require('../models/User');
const Post = require('../models/Post');
const sgMail = require('@sendgrid/client');
let router = express.Router();
//POST new post route
router.post('/', async (req, res, next) => {
await Post.query()
.insert({
body: req.body.post.body,
users_id: req.body.post.userId,
groups_id: req.body.post.groupId
})
res.json({
success: true, message: 'ok'
}); // respond back to request
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
console.log('this is the sendgrid api key: ', process.env.SENDGRID_API_KEY)
const msg = {
to: 'dariusgoore@gmail.com',
from: 'team@writerboards.com',
subject: 'User just posted a message',
text: req.body.post.body,
html: '<strong>Can we see the message?</strong>',
};
console.log('this is the msg 2b sent: ', msg)
sgMail.send(msg);
});
const express=require('express');
const auth=require('../middleware/authenticate');
const User=require('../models/User');
const Post=require('../models/Post');
const sgMail=require('@sendgrid/client');
让router=express.router();
//邮递新路线
router.post('/',异步(req,res,next)=>{
等待Post.query()
.插入({
正文:req.body.post.body,
用户id:req.body.post.userId,
组id:req.body.post.groupId
})
res.json({
成功:正确,消息:“ok”
});//回复请求
sgMail.setApiKey(process.env.SENDGRID\u API\u KEY);
log('这是sendgrid api键:',process.env.sendgrid_api_键)
常数msg={
致:'dariusgoore@gmail.com',
发件人:'team@writerboards.com',
主题:“用户刚刚发布了一条消息”,
文本:req.body.post.body,
html:“我们能看到消息吗?”,
};
log('这是已发送的消息:',消息)
sgMail.send(msg);
});
这会引发一个错误:
未处理的PromisejectionWarning:TypeError:sgMail.send不是一个
作用
邮局
使用
@sendgrid/mail
而不是@sendgrid/client
。@sendgrid/client
包没有.send
功能
const sgMail=require('@sendgrid/mail')
而不是const sgMail=require('@sendgrid/client')
我认为您使用了错误的模块-您使用了@sendgrid/client
,它提供的不仅仅是发送邮件,您应该使用@sendgrid/mail