Sending iCal invite using Node Js

12 / Jun / 2015 by Sakshi Tyagi 3 comments

Sending event invites through an email is a common use case nowadays in applications. In this blog, we will briefly cover the steps to accomplish this using Node.js.

Node.js provide various npm modules for sending iCal invite. We have customized one of the modules and used it according to our use case. You can find the module here .

Let’s say we have an object called “eventObj” which contains all the required information related to an event like start time, end time, title, organiser details etc. We will use this information to create our “.ics” file and send it to the receiver as an email attachment.

Step1: In your js file, require the module.

[js]
var ical = require(‘my-ical-generator’);
[/js]

Step2: Now we need that event object which looks like:

var eventObj = {
	'start' : new Date(),
	'end' : new Date("13 June,2015"),
	'title' : 'Annual trip',
	'description' : 'Lets enjoy and relax'
	'id' : 'wdcwe76234e127eugb', //Some unique identifier
	'organiser' : {'name' : 'Sakshi Tyagi', 'email':'sakshi.tyagi@tothenew.com'},
	'location' : 'Goa'
}

var cal = ical();
Now set the domain which is your application’s weburl and give a name to the event.
cal.setDomain(‘http://www.tothenew.com/’).setName(‘My ical invite’);

Now, we actually add the event details to the iCal object using method addEvent()

cal.addEvent({
	start: eventObj.start,
	end: eventObj.end,
	summary: eventObj.title,
	uid: eventObj.id, // Some unique identifier
	sequence: 0,
	description: eventObj.description,
	location: eventObj.location,
	organizer: {
                name: eventObj.owner.name,
                email: eventObj.owner.email
      		},
	method: 'request'
});

Some of the fields are optional.

Step 3: Now that our event is ready, we need to save the ‘.ics’ file created. Here in this example, we save it on our server, in the ‘uploads’ directory.

 var path = __dirname + '/uploads/'+ eventObj.id + '.ics';
 cal.saveSync(path);

Step 4: Last step is to send the mail with the .ics file. We can use classic “nodemailer” module for this.

Just require the module and use its “sendMail” method to send the attachment in the email.

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
    service: "yourEmailProvider",
    auth: {
        user: "yourEmail" ,
        pass: "yourPassword"
    }
});
var mailObj = {
	from: "Sender email Id",
    to: "Receiver email Id",
    subject: "Your Subject",
    text: "Some text in the body",
	attachments : [path]
};

transporter.sendMail(mailObj, function(err, info){
       console.log(err,info);
});

There you go! We successfully sent an email with iCal invite.

Hope this helps. 🙂

FOUND THIS USEFUL? SHARE IT

comments (3)

  1. Mohit Mathur

    Can we send a meeting invitation wit accept , reject and decline buttons without the attachment? Means email itself act as an invite.

    Reply
    1. Muhammad Umar

      I know I am replying too late but it will help others.
      Yes, you can send an invite with Accept and Reject buttons using nodemailer.
      Check the complete code here, https://github.com/nodemailer/nodemailer/blob/v2.2/examples/ical-event.js

      change content with path: “your ics file path”
      var message = {
      from: ‘”Sender Name” ‘,
      to: ‘”Receiver Name” ‘,
      subject: ‘Calendar invite’,
      text: ‘This message contains a calendar event’,
      icalEvent: {
      method: ‘request’,
      // content can be a string, a buffer or a stream
      // alternatively you could use `path` that points to a file or an url
      content: ‘BEGIN:VCALENDAR\r\nPRODID:-//ACME/DesktopCalendar//EN\r\nMETHOD:REQUEST\r\nVERSION:2.0\r\n…’
      }
      };

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *