Alex Alex - 3 months ago 25
Node.js Question

how to attach .ics file with email in node js?

I am using ics for calender for get event it done successfully but i want to send some text with

.ics
attachment and for that i try this code just see and tell me where i have to change.
I got event.ics file but want to send that file with mail text/html content.

var ics = require('ics');
var options = {
eventName: 'Fingerpainting lessons',
filename: 'event1.ics',
dtstart: date,
location: 'Here and there',
email: {
name: 'alex bob',
email: email
}
};
ics.createEvent(options, null, function(err, calendar) {
if (!err)
{
console.log('Event file',calendar);
var filePath = path.join('vabo_email', 'email.html');
fs.readFile(filePath, {encoding: 'utf-8'},
function(err, data) {
if ( ! err )
{

var helper = require('sendgrid').mail;

from_email = new helper.Email('booking@callvabo.com')
to_email = new helper.Email(email)
subject = subject
//Dynamic content
var data = data.replace("$content", bodycontent)

content = new helper.Content('text/html', data)
//content = new helper.Content("text/Calendar",calendar)
mail = new helper.Mail(from_email, subject, to_email,content);

var sg = require('sendgrid')('secretekey');

var requestBody = mail.toJSON();
var request = sg.emptyRequest();
request.method = 'POST';
request.path = '/v3/mail/send';
request.body = requestBody;
data = new helper.Content("text/Calendar",calendar)
request.attachments = [{'filename': 'calendar.ics', 'content': data}]

sg.API(request, function (error, response) {
if ( ! error )
{
console.log('mail send Successfully to',email);
}
else
{
console.log('Error for send mail',error);
}
});
}
else
{
console.log('Error for calendar',err);
}
});
}
else
{
console.log('Error for creating Event',err);
}
});

Answer

You need to Base64 encode the file content.

var base64Content = new Buffer(calendar).toString('base64');
request.attachments = [{'filename': 'calendar.ics', 'content': base64Content, 'type': 'text/Calendar'}]
Comments