{"id":19935,"date":"2015-06-12T15:05:57","date_gmt":"2015-06-12T09:35:57","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=19935"},"modified":"2015-12-10T18:39:20","modified_gmt":"2015-12-10T13:09:20","slug":"sending-ical-invite-using-node-js","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/sending-ical-invite-using-node-js\/","title":{"rendered":"Sending iCal invite using Node Js"},"content":{"rendered":"<p>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.<\/p>\n<p>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 <a title=\"my-ical-generator\" href=\"https:\/\/github.com\/sakshityagi\/my-ical-generator\" target=\"_blank\">here<\/a>\u00a0.<\/p>\n<p>Let&#8217;s say we have an object called &#8220;eventObj&#8221; which contains all the required information related to an event like start time, end time, title, organiser details etc. We will use this\u00a0information to create our &#8220;.ics&#8221; file and send it to the receiver as an email attachment.<\/p>\n<p><strong>Step1:<\/strong> In your js file, require the module.<\/p>\n<p>[js]<br \/>\nvar ical = require(&#8216;my-ical-generator&#8217;);<br \/>\n[\/js]<\/p>\n<p><strong>Step2:<\/strong> Now we need that event object which looks like:<\/p>\n<pre>var eventObj = {\r\n\t'start' :\u00a0new Date(),\r\n\t'end' :\u00a0new Date(\"13 June,2015\"),\r\n\t'title' : 'Annual trip',\r\n\t'description' : 'Lets enjoy and relax'\r\n\t'id' : 'wdcwe76234e127eugb', \/\/Some unique identifier\r\n\t'organiser' : {'name' : 'Sakshi Tyagi', 'email':'sakshi.tyagi@tothenew.com'},\r\n\t'location' : 'Goa'\r\n}\r\n<\/pre>\n<p>var cal = ical();<br \/>\nNow set the domain which is your application&#8217;s weburl and give a name to the event.<br \/>\ncal.setDomain(&#8216;http:\/\/www.tothenew.com\/&#8217;).setName(&#8216;My ical invite&#8217;);<\/p>\n<p>Now, we actually add the event details to the iCal object using method <strong>addEvent()<\/strong><\/p>\n<pre>cal.addEvent({\r\n\tstart: eventObj.start,\r\n\tend: eventObj.end,\r\n\tsummary: eventObj.title,\r\n\tuid: eventObj.id, \/\/ Some unique identifier\r\n\tsequence: 0,\r\n\tdescription: eventObj.description,\r\n\tlocation: eventObj.location,\r\n\torganizer: {\r\n                name: eventObj.owner.name,\r\n                email: eventObj.owner.email\r\n      \t\t},\r\n\tmethod: 'request'\r\n});\r\n<\/pre>\n<p>Some of the fields are optional.<\/p>\n<p><strong>Step 3:<\/strong> Now that our event is ready, we need to save the &#8216;.ics&#8217; file created. Here in this example, we save it on our server, in the &#8216;uploads&#8217; directory.<\/p>\n<pre> var path = __dirname + '\/uploads\/'+ eventObj.id + '.ics';\r\n cal.saveSync(path);\r\n<\/pre>\n<p><strong>Step 4:<\/strong> Last step is to send the mail with the .ics file. We can use classic &#8220;nodemailer&#8221; module for this.<\/p>\n<p>Just require the module and use its &#8220;sendMail&#8221; method to send the attachment in the email.<\/p>\n<pre>var nodemailer = require('nodemailer');\r\nvar transporter = nodemailer.createTransport({\r\n    service: \"yourEmailProvider\",\r\n    auth: {\r\n        user: \"yourEmail\" ,\r\n        pass: \"yourPassword\"\r\n    }\r\n});\r\n<\/pre>\n<pre>var mailObj = {\r\n\tfrom: \"Sender email Id\",\r\n    to: \"Receiver email Id\",\r\n    subject: \"Your Subject\",\r\n    text: \"Some text in the body\",\r\n\tattachments : [path]\r\n};\r\n\r\ntransporter.sendMail(mailObj, function(err, info){\r\n       console.log(err,info);\r\n});\r\n\r\n<\/pre>\n<p>There you go! We successfully sent an email with iCal invite.<\/p>\n<p>Hope this helps. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":48},"categories":[1185],"tags":[1864,55,1124,1863],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19935"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=19935"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19935\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=19935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=19935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=19935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}