G:\PleskVhosts\stockarbiter.com\httpdocs\karimkameka/wp-content/themes/naya-lite/lib/css/custom-css.css is not writeable.
Copy the generated css from the text area below and paste it in the file above.



Tag: SendGrid

Sending a File Attachment using SendGrid

I was looking to automate the process of downloading an Excel Spreadsheet from a web site and then sending that spreadsheet to one of my many inboxes. This process needed to run hourly. I have also been learning all I can about Windows Azure Mobile Services (WAMS) provided by Azure which come “free”! (I like free, especially when I am learning and dabbling, but especially when I have some real code to get out). Anyway, given my new found knowledge for WAMS and the SendGrid service which is an add on to Azure I thought maybe I could automate this process quickly and freely. So begins my journey. As a part of the AMS service there is a scheduler service, given I need this hourly this is the natural choice.

In developing the script and my newbie status to node.js, I was at a loss for how to get the file. In my case the file was hosted on a third-party web site with a VERY accessible URL Smile. I just wanted to download the file and attach it to my email. Enter SendGrid, SendGrid is a powerful email service which provides a RESTful email API allowing me to send emails from the cloud without having to worry about having an SMTP server at the ready! Awesome since I don’t have an SMTP server hanging around! So begins the adventure.

I look up the sample from the Windows Azure documentation and for a basic send it works great however, in looking up the SendGrid API it turns out there is an “Email” object which will provide me more control over what is sent and the ability to add attachments. One of which methods allows me to set the URL for which the attachment resides and SendGrid takes care of all the leg work regarding downloading the file and attaching it to the email! (AWESOME!!)

Below is what I ended up with after fighting through some syntax errors! All in all this now works to a T and I get my hourly emails for processing like clockwork!

I even used (yet another “free” service) Send to DropBox to send the file to my dropbox account for easy filing!

FINAL WORKING SAMPLE

var sg = require('sendgrid'); 

function myjob_hourly() { 
var theUrl = ""; 
var theemail = ""; 
var currentDate = new Date(); 
var subject = ""; 

var sendgrid = new sg.SendGrid('username', 'password'); 
var email = new sg.Email ({ 
to: theemail, 
from: theemail, 
subject: subject, 
text: '' 
}); 
//JUST GIVE THE URL AND THE FILE NAME FOR THE ATTACHEMENT HERE!!! 
email.addFile({filename: "myfile.xlsx", 
url:theUrl}); 
sendgrid.send(email, function(success, message) { 
if (!success) { 
console.error(message); 
}  
}); 
}