Sending email via Node.js with calendar invite

Manish Kumar
4 min readSep 19, 2020

--

Sending email via node.js with calendar invites

Email is one of the most commonly used means of communication in this digital world. It is one of the most important requirement when you are building a web app or software. In this article, we will go through how to build an email sending service using Node.js. We will also have a feature of sending calendar invites via this email.

Prerequisites

Node.js and npm installed locally with some more npm packages that we will go through below

Let’s start with installing one of the most commonly used mailer services of node — nodemailer

npm install nodemailer

I have built a custom email-service which I simply import it on other files to send email. It also helps in breaking up my code and reusing it in my entire project

1. Configuring SMTP transport on nodemailer

To start this with we need to first set up the SMTP transport of nodemailer

var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "myemailid@gmail.com",
pass: "randompassword"
}
});

I recommend not to use the email password directly in the code Instead, generate an App password from your google account.

Go to your google account, set up 2-step verification, and then generate App password

Enable 2 step verification on google and then generate App passwords

Once you are done with this, we are ready to send an email.

2. Configuring Mail Options

The mail options mainly contain params as “to” , “subject” and “html”

After which we can use the sendMail function of the above-generated SMTP transport

var mailOptions = {
to: "testuseremail@gmail.com",
subject: "This is a test email from a developer",
html: "<h1>Welcome to my website</h1>"
}
smtpTransport.sendMail(mailOptions, function (error, response) {
if (error) {
console.log(error);
} else {
console.log("Message sent: " , response);
}
})

Thus in almost 10 lines of code, you can easily set up your email sending service in your project.

3. Sending Calendar invites with email

There might be a need to send an email with a reminder of events or a calendar invite when he is subscribed to the event or a webinar on your website

The calendar invites are interpreted well by Gmail and other email services to suggest adding the event to the user's calendar and reminding them as well.

Here’s a screenshot of what the email would look like :

Calendar invite email sent via node.js and interpreted by gmail
  • Install ical-generator
npm install ical-generator --save

Now we need to generate a calendar object which we will be adding it mailOptions in the nodemailer service written above.

4. Generating calendar object (.ics file)

Let's begin by generating the calendar invite object.

I have written function in node that takes some required inputs and based on which it returns a calendar object.

const ical = require('ical-generator');function getIcalObjectInstance(starttime, endtime, summary,             description, location, url , name ,email) {const cal = ical({ domain: "mytestwebsite.com", name: 'My test calendar event' });cal.domain("mytestwebsite.com");cal.createEvent({
start: starttime, // eg : moment()
end: endtime, // eg : moment(1,'days')
summary: summary, // 'Summary of your event'
description: description, // 'More description'
location: location, // 'Delhi'
url: url, // 'event url'
organizer: { // 'organizer details'
name: name,
email: email
},
});
return cal;}

The above function will return a .ics object which is a media type that allows users to store and exchange calendaring and scheduling information.

Once we get the above calendar object, we need to simply send it in the mailOptions

Let see how we can integrate the calendar object with the mail object

5. Sending Calendar object in email

I have written the entire generic function of email sending which can be used to send normal text email as well as calendar invite also.

If we need to send calendar invites just create a calendar object from the above-mentioned function and then send its return object to the below sendemail function.

If you want to simply send a html/text email , send calendarObject as null in the below function

var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "tesgmailaccount@gmail.com",
pass: "gmailapppassword"
}
});
async function sendemail(sendto, subject, htmlbody, calendarObj = null) {
mailOptions = {
to: sendto,
subject: subject,
html: htmlbody
}
if (calendarObj) {
let alternatives = {
"Content-Type": "text/calendar",
"method": "REQUEST",
"content": new Buffer(calendarObj.toString()),
"component": "VEVENT",
"Content-Class": "urn:content-classes:calendarmessage"
}
mailOptions['alternatives'] = alternatives;
mailOptions['alternatives']['contentType'] = 'text/calendar'
mailOptions['alternatives']['content']
= new Buffer(calendarObj.toString())
}
smtpTransport.sendMail(mailOptions, function (error, response) {
if (error) {
console.log(error);
} else {
console.log("Message sent: " , response);
}
})
}
module.exports = {
sendemail,
};

We have specified a key in mailOptions object named as ‘alternatives’ which contains various other metainformation that is helpful for gmail to understand the email contains a calendar invite too.

Conclusion

In this blog post, we looked at how to build a simple email service using nodemailer and then send a calendar invite with that email.

Just Incase, If I have missed out on something please let me know, how I can improve and modify the above.

Do give this article a clap 👏 , if you found it useful

--

--

Manish Kumar

Working as a Full Stack developer at Cardekho. Exploring new things in my part-time. A travel Enthusiast.