Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sendgrid Web API基本安装不使用Meteor方法发送电子邮件_Meteor_Sendgrid - Fatal编程技术网

Sendgrid Web API基本安装不使用Meteor方法发送电子邮件

Sendgrid Web API基本安装不使用Meteor方法发送电子邮件,meteor,sendgrid,Meteor,Sendgrid,我正在为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 = { t

我正在为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_密钥放在客户端,您正在显示它,并且应该对它保密

创建一个新的文件服务器/smtp.js。通过将其放在服务器/目录中,Meteor将仅将其放在服务器端,从客户端看不到此代码:

Meteor.startup(function () {
  process.env.MAIL_URL = 'smtp://username:password@smtp.sendgrid.net:587';
});
将电子邮件包添加到meteor。在命令行上:

流星添加电子邮件

创建文件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!
  }
});

希望有帮助。

谢谢您的回复。我试图不使用旧的meteor邮件包。我使用了主npm sendgrid库,它运行良好,没有任何额外的流星优势。感谢您抽出时间回复!