Closed elimence closed 10 years ago
There's no scheduling functionality built into the library. You could build your own scheduler that used this to send emails quite easily. You could use CRON to run a sending script at a specific time, or go further and introduce a queueing system into your architecture that picks up messages at particular times.
It would be useful to have a scheduling feature included in the library, like Mandrill does:
The difficulty is, we don't support sending scheduled email through our API, so the library cannot support it. However, we've created a SendGrid/Iron.io library that does support scheduling.
It should be relatively simple to use this library in combination with the sendgrid-scheduler library:
// Do your sendgrid thing
var sendgrid = require('sendgrid')(api_user, api_key);
var email = new sendgrid.Email({
to: 'foo@bar.com',
from: 'you@yourself.com',
subject: 'Subject goes here',
text: 'Hello world'
});
// Require the iron_worker Package
// To install, `npm install iron_worker`
var iron_worker = require('iron_worker');
// Initialize the Client
var worker = new iron_worker.Client({token: "IRON_IO_TOKEN", project_id: "IRON_IO_PROJECT_ID"});
// Take the email object, we'll use it as our payload
var payload = email;
// Add your SendGrid username and password to the email
payload.api_user = "YOUR_SENDGRID_USERNAME";
payload.api_key = "YOUR_SENDGRID_PASSWORD";
// schedule the Iron Worker
worker.schedulesCreate("SendGridScheduler", payload, { "start_at" : Math.floor((new Date()).getTime()/1000) + 3600 }, function (err, body) {});
Thanks @nquinlan! This sounds like an alternative solution, although I might end up scheduling emails directly inside our codebase, through some chron job.
It'd be nice if in the future SendGrid would consider to add support sending scheduled email dierctly through their API. Cheers :)
Sounds good @pensierinmusica. Please let us know if you have any issues or if there's anything we can do to help.
I'll definitely bring your feedback to the product team.
I see this API endpoint now, though I'm confused by the documentation - there's "Cancel scheduled send" but not "Schedule send" in the left sidebar:
https://sendgrid.com/docs/API_Reference/Web_API_v3/cancel_schedule_send.html
There's also the sendAt
option, though apparently with some potential dealbreaker limitations.
Hey @dandv! You can now schedule emails using SendGrid alone, using their API v3 or SMTPAPI. You can see an example of how to do this using this library in the use cases provided.
Simply, you'll add a sendAt
parameter to the message object you're trying to send. The sentAt
parameter should be a UNIX timestamp, since we're using Node here it's really easy to accidentally use JS timestamp in milliseconds versus seconds, so make sure you do something like Math.round(myTime/1000)
if you're passing in a JS timevalue.
As a complete example you'd do something like this:
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'recipient@example.org',
from: 'sender@example.org',
subject: 'Hello delayed email',
html: '<p>Some email content</p>',
sendAt: 1500077141,
};
//ES6
sgMail
.send(msg)
.then(() => {}, error => {
console.error(error);
if (error.response) {
console.error(error.response.body)
}
});
At time of writing, you are correct in your linked issue that there are a number of requirements to using sendAt
that currently stand.
A couple disclaimers: I haven't tested the above code nor have I run the SendGrid Node library in years, since I no longer work at Twilio SendGrid or maintain the library, but 99% it should work. For other folks coming to this answer in the future, my example may be out of date, but the links provided in the answer should be kept up-to-date.
Thanks @nquinlan, that's pretty much what I linked to :) The problem with sendAt
is that it seems to have some serious limitations.
I am wondering if it's possible to send scheduled emails using this library. I have looked through the docs, but haven't seen anything like that.