Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Send email from server code

As part of my app, I need to send emails out to users based on a schedule that they set ... For reminders, maintenance to data, etc.

Do you have any examples of other people here that have worked a similar challenge and posted a solution. I'd like to use send grid to do so if possible but don't know how to make the plugin available on the server side, etc.

Thanks in advance,
Bruce

Jon Haider
Posts: 0
Joined: Thu Oct 16, 2014 2:53 pm

Send email from server code

According to the documentation here, you can just use this code:

pre
var sendgrid = require('sendgrid')(api_user, api_key);

var payload = {
to: 'to@example.com',
from: 'from@other.com',
subject: 'Saying Hi',
text:'This is my first email through SendGrid'
}

sendgrid.send(payload, function(err, json) {
if (err) { console.error(err); }
console.log(json);
});
/pre

Seems pretty straightforward to use from the Server code. The other question is how would you set it up to send these emails on a schedule. There's a schedule building on the script side. Maybe this would be a good place to start.

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Send email from server code

Jon,

Agreed - after your response (thanks much!) - I remembered back to one of the tutorials on sending password reminders using server code - and knew that i could find the code in the tutorial as well...

Here's the code from the tutorial....:

Code: Select all

var XHRResponse = XHR.send("POST", "[url=https://api.sendgrid.com/api/mail.send.json]https://api.sendgrid.com/api/mail.sen...[/url]", { 
     "parameters": { 
       "api_user": "Sendgrid username", 
       "api_key": "Sendgrid password", 
       "to": email, 
       "toname": "", 
       "subject": "Password Recovery", 
       "text": "Your recovery code is: " + secretCode + ". Copy and this code into the field provided.", 
       "from": "a href="mailto:admin@company.com" rel="nofollow"admin@company.com/a" 
     } 
   }); 

Thanks again - happy Monday, I'm off implementing the code ...

Bruce

Return to “Issues”