【发布时间】:2025-12-03 06:05:01
【问题描述】:
我想使用@sendgrid/mail 发送邮件,但是当我导入它时它不起作用。我的代码 sn-p 如下,
import * as sgMail from '@sendgrid/mail';
function sendMail(msg) {
sgMail.setApiKey("API-KEY");
sgMail.send(msg, (err, result) => {
if(err){
console.error(err);
}
console.log(result);
});
}
const obj = {
to: "mail@gmail.com",
from: "no-reply@gmail.com",
subject: "abc",
text: "abc",
html: "<h1>Working</h1>",
}
sendMail(obj);
这是我做的代码,所以现在问题是 sgMail.setApiKey is not a function error pops.
如果我删除 setApiKet 则 sgMail.send is not a function error pops.
所以,如果您有任何解决方案,请告诉我。
【问题讨论】:
-
你做过这个 npm install --save @sendgrid/mail
-
是的,我已经这样做了
-
包为
commonjs格式。使用const sgMail = require('@sendgrid/mail')或尝试默认导出import sgMail from '@sendgrid/mail' -
sendMail(obj);更改为 sendMail(obj, sgMail);和函数 sendMail(msg, sgMail)
标签: javascript node.js sendgrid