虽然应用程序运行时出现0错误,但未使用nodejs通过sendgrid接收邮件



我只是nodejs的初学者,正在为开源项目开发电子邮件功能。此外,我在本地机器上进行了这个测试项目,以学习使用nodejs处理sendgrid,但遇到了严重的问题。它甚至没有显示任何错误!!请帮帮我。这就是测试项目目录的样子。测试目录

这是我的app.js

require('dotenv').config();
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'anushkasharma112000@gmail.com',
from: 'xxxx@gmail.com', // My Verified email at https://app.sendgrid.com/settings/sender_auth
subject: 'Sending with Twilio 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>',
};
//ES6
sgMail
.send(msg)
.then(() => { }, error => {
console.error(error);
if (error.response) {
console.error(error.response.body)
}
});

我有一个.env文件,只有1行

SENDGRID_API_KEY='SG.somelettersanddigits.somelettersanddigits'

和一个package.json文件

{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "app.js",
"dependencies": {
"@sendgrid/mail": "^7.4.2",
"dotenv": "^8.2.0"
},
"devDependencies": {},
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

我刚刚复制了你的代码,并添加到我的电子邮件中,效果很好。我建议删除你的api密钥并创建一个新的。同样像boolfalse所说的:console.log(`your-api-key${process.env.SENDGRID_api_key}`(,以确保它顺利通过。

最新更新