0

I want to send a calendar invitation in nodejs. I'm using Nodemailer and ical-generator to generate ical files. Everything working fine but the email doesn't add invite to the calendar automatically instead I get an option to Add to the calendar. But I want email to directly add events to the calendar.

This is the code snippet for the same.

const nodemailer = require('nodemailer');
const ical = require('ical-generator');

const sendInvite = (
    club,
    eventName,
    eventDescription,
    participants,
    startTime,
    endTime,
    organizer,
    url,
    location = 'Online'
) => {
    const iclObject = createIcalObject(
        eventName,
        startTime,
        endTime,
        eventDescription,
        location,
        url,
        organizer
    );

    sendEmail(
        participants,
        eventName,
        iclObject,
        club,
        eventName,
        startTime,
        url
    );
};

module.exports = sendInvite;

function createIcalObject(
    title,
    startTime,
    endTime,
    description,
    location,
    url,
    organizer
) {
    const cal = ical({
        domain: process.env.HOSTNAME,
        name: title,
    });
    cal.createEvent({
        start: startTime,
        end: endTime,
        summary: description,
        description,
        location,
        url,
        organizer,
        method: 'request',
    });
    return cal;
}

function sendEmail(
    participants,
    subject,
    iclObject,
    club,
    eventName,
    startTime,
    url
) {
    mailOptions = {
        to: participants,
        subject: subject,
        html: `<h3>Hi, </h3><p>${club} club inviing you to participate on the event ${eventName} on ${startTime}. Add the invite to yur calendar and join on the event by clickig <a href=${url}><button>Join</button></a> without fail.<br/>Thank you</p> `,
        alternatives: {
            contentType: 'text/calendar',
            method: 'REQUEST',
            content: new Buffer.from(iclObject.toString()),
            component: 'VEVENT',
            'Content-Class': 'urn:content-classes:calendarmessage',
        },
    };

    const smtpTransport = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: process.env.EMAIL_NODEMAILER,
            pass: process.env.PASS_NODEMAILER,
        },
    });

    smtpTransport.sendMail(mailOptions, function (error, response) {
        if (error) {
            console.log(error);
        } else {
            console.log('Message sent: ', response);
        }
    });
}

As same I need to cancel the event, cancel mail is sent But from the calendar it was not removed.

Athulya Ratheesh
  • 291
  • 4
  • 22

0 Answers0