Sendgrid Web API 基本安装不使用 Meteor 方法发送电子邮件
Sendgrid Web API Basic install not sending email using Meteor Method
我正在为 Node.js 使用 SendGrid WebAPI。
我遵循了这些说明:
我从客户端触发的方法中有以下代码。
// using SendGrid's v3 Node.js Library
// https://github.com/sendgrid/sendgrid-nodejs
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'test@example.com',
from: 'test@example.com',
subject: 'Sending with 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>',
};
sgMail.send(msg);
当我触发发送方法时,我收到一个奇怪的错误(如下)- 不确定为什么会这样。希望有一些想法。
谢谢!!
错误:
(node:21240) UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded
W20190301-07:12:22.267(-8)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:594:27)
W20190301-07:12:22.267(-8)? (STDERR) at Array.forEach (<anonymous>)
W20190301-07:12:22.268(-8)? (STDERR) at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20190301-07:12:22.268(-8)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20190301-07:12:22.268(-8)? (STDERR) at Array.forEach (<anonymous>)
W20190301-07:12:22.268(-8)? (STDERR) at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20190301-07:12:22.268(-8)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20190301-07:12:22.268(-8)? (STDERR) at Array.forEach (<anonymous>)
W20190301-07:12:22.268(-8)? (STDERR) at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20190301-07:12:22.268(-8)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20190301-07:12:22.268(-8)? (STDERR) at Array.forEach (<anonymous>)
W20190301-07:12:22.268(-8)? (STDERR) at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20190301-07:12:22.269(-8)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20190301-07:12:22.269(-8)? (STDERR) at Array.forEach (<anonymous>)
W20190301-07:12:22.269(-8)? (STDERR) at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20190301-07:12:22.269(-8)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20190301-07:12:22.269(-8)? (STDERR) (node:21240) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 9)
首先,将您的 API_KEY 放在客户端,就是在展示它,它应该保密。
创建一个新文件server/smtp.js
。通过将它放在 server/
目录中,Meteor 将只把它放在服务器端,这段代码在客户端是不可见的:
Meteor.startup(function () {
process.env.MAIL_URL = 'smtp://username:password@smtp.sendgrid.net:587';
});
将电子邮件包添加到流星。在命令行上:
meteor add email
创建文件server/methods.js
以添加一些服务器端方法:
Meteor.methods({
sendmail(to) {
// for security reasons, you should validate the 'to' argument
// but let's forget that for now.
Email.send({
from: "me@paulpedrazzi.com",
to: to
subject: "Awesome",
text: "It worked"
});
}
});
每当您想发送电子邮件时,在客户端调用此方法并向其传递必要的参数:
Meteor.call('sendmail', ‘elon.musk@tesla.com’, (err, res) => {
if (err) {
alert(err);
} else {
// success!
}
});
希望对您有所帮助。
我正在为 Node.js 使用 SendGrid WebAPI。
我遵循了这些说明:
我从客户端触发的方法中有以下代码。
// using SendGrid's v3 Node.js Library
// https://github.com/sendgrid/sendgrid-nodejs
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'test@example.com',
from: 'test@example.com',
subject: 'Sending with 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>',
};
sgMail.send(msg);
当我触发发送方法时,我收到一个奇怪的错误(如下)- 不确定为什么会这样。希望有一些想法。
谢谢!!
错误:
(node:21240) UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded
W20190301-07:12:22.267(-8)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:594:27)
W20190301-07:12:22.267(-8)? (STDERR) at Array.forEach (<anonymous>)
W20190301-07:12:22.268(-8)? (STDERR) at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20190301-07:12:22.268(-8)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20190301-07:12:22.268(-8)? (STDERR) at Array.forEach (<anonymous>)
W20190301-07:12:22.268(-8)? (STDERR) at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20190301-07:12:22.268(-8)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20190301-07:12:22.268(-8)? (STDERR) at Array.forEach (<anonymous>)
W20190301-07:12:22.268(-8)? (STDERR) at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20190301-07:12:22.268(-8)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20190301-07:12:22.268(-8)? (STDERR) at Array.forEach (<anonymous>)
W20190301-07:12:22.268(-8)? (STDERR) at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20190301-07:12:22.269(-8)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20190301-07:12:22.269(-8)? (STDERR) at Array.forEach (<anonymous>)
W20190301-07:12:22.269(-8)? (STDERR) at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
W20190301-07:12:22.269(-8)? (STDERR) at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
W20190301-07:12:22.269(-8)? (STDERR) (node:21240) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 9)
首先,将您的 API_KEY 放在客户端,就是在展示它,它应该保密。
创建一个新文件server/smtp.js
。通过将它放在 server/
目录中,Meteor 将只把它放在服务器端,这段代码在客户端是不可见的:
Meteor.startup(function () {
process.env.MAIL_URL = 'smtp://username:password@smtp.sendgrid.net:587';
});
将电子邮件包添加到流星。在命令行上:
meteor add email
创建文件server/methods.js
以添加一些服务器端方法:
Meteor.methods({
sendmail(to) {
// for security reasons, you should validate the 'to' argument
// but let's forget that for now.
Email.send({
from: "me@paulpedrazzi.com",
to: to
subject: "Awesome",
text: "It worked"
});
}
});
每当您想发送电子邮件时,在客户端调用此方法并向其传递必要的参数:
Meteor.call('sendmail', ‘elon.musk@tesla.com’, (err, res) => {
if (err) {
alert(err);
} else {
// success!
}
});
希望对您有所帮助。