ppedrazzi
ppedrazzi

Reputation: 787

Sendgrid Web API Basic install not sending email using Meteor Method

I'm using the SendGrid WebAPI for Node.js.

I followed these instructions: Environmental Variables Set Up

I have the following code in a method I trigger from the client.

// 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: '[email protected]',
  from: '[email protected]',
  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);

When I trigger the method to send, I get an odd error (below) - Not sure why this is happening. Hoping for some ideas.

Thank you!!


ERROR:

(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)

Upvotes: 0

Views: 178

Answers (1)

bordalix
bordalix

Reputation: 432

First of all, by putting your API_KEY on the client side, you're showing it, and it should be kept secret.

Create a new file server/smtp.js. By putting it in a server/ directory, Meteor will put it only on the server side, this code will not be visible from the client side:

Meteor.startup(function () {
  process.env.MAIL_URL = 'smtp://username:[email protected]:587';
});

Add the email package to meteor. On the command line:

meteor add email

Create a file server/methods.js to add some server side methods:

Meteor.methods({
  sendmail(to) {
    // for security reasons, you should validate the 'to' argument
    // but let's forget that for now.
    Email.send({
      from: "[email protected]",
      to: to
      subject: "Awesome",
      text: "It worked"
    });
  }
});

Whenever you want to send an email, on the client side call this method and pass it the necessary arguments:

Meteor.call('sendmail', ‘[email protected]’, (err, res) => {
  if (err) {
    alert(err);
  } else {
    // success!
  }
});

Hope it helps.

Upvotes: 1

Related Questions