首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sendgrid基本安装不使用Meteor方法发送电子邮件

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

Stack Overflow用户
提问于 2019-03-01 15:23:08
回答 1查看 172关注 0票数 0

我正在使用SendGrid WebAPI for Node.js。

我按照以下指示行事:

我从客户端触发的方法中有以下代码。

代码语言:javascript
复制
// 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);

当我触发要发送的方法时,我会得到一个奇怪的错误(下面)--不知道为什么会发生这种情况。希望能有一些想法。

谢谢你!!

错误:

代码语言:javascript
复制
(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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-02 09:58:51

首先,通过将您的API_KEY放在客户端,您将显示它,并且它应该被保密。

创建一个新文件server/smtp.js。通过将其放在server/目录中,Meteor将只将其放在服务器端,此代码在客户端将不可见:

代码语言:javascript
复制
Meteor.startup(function () {
  process.env.MAIL_URL = 'smtp://username:password@smtp.sendgrid.net:587';
});

将电子邮件包添加到流星。在命令行上:

meteor add email

创建一个文件server/methods.js以添加一些服务器端方法:

代码语言:javascript
复制
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"
    });
  }
});

每当您想发送电子邮件时,在客户端,调用此方法并传递必要的参数:

代码语言:javascript
复制
Meteor.call('sendmail', ‘elon.musk@tesla.com’, (err, res) => {
  if (err) {
    alert(err);
  } else {
    // success!
  }
});

希望能帮上忙。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54947573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档