1

There is a scenario where I need to send event meeting invites to end users. I am able to generate the ICS file and send it as attachment. But the ICS files are not readable or added in User Calander.

Code to generate and send email is as below:

  var transport = require('nodemailer-smtp-transport'),
    transporter = nodemailer.createTransport(transport(config.mailer)),
    sendMail = function(mailOptions) {
      transporter.sendMail(mailOptions, function(err, response) {
        if (err) return err;
        return response;
      });
    },
    eventEmailToUser = function(user, events, createdBy, mailOptions) {
    var ical = require('ical-generator');
    var cal = ical();
      var username = user.username ? user.username : ' ';
      var eventName = events.title ? events.title : ' ';
      var eventDate = moment.tz(events.date, 'Asia/Kolkata');
      eventDate = eventDate.format('YYYY-MM-DD h:mm a');
      cal.addEvent({
        start: new Date(),
        end: new Date(new Date().getTime() + 3600000),
        summary: events.title,
        uid: events._id, // Some unique identifier
        sequence: 0,
        description: events.description,
        location: events.location,
        organizer: {
          name: createdBy.username,
          email: createdBy.email
        },
        method: 'request'
      });

      var path = '/files/' + events._id + '.ics';
      cal.save(path, function(err, file) {
        if (err) return err;
      });

      mailOptions.alternatives = [{
        contentType: "text/calendar",
        contents: new Buffer(cal).toString()
      }];

      mailOptions.attachments = [{
        filename: events.title + '.ics',
        filePath: path
      }];
      mailOptions.html = [
        '<div>',
        '<div>Hi <b>' + username + '</b><br/<br/>',
        ' You have just confirmed to attend <b>' + eventName + '</b> on <b>' + eventDate + '</b>',
        ' <br/><br/>',
        'Thanks',
        ' <br/>',
        '</div>',
        '<br/>',
        '</div>'
      ].join('\n\n');
      mailOptions.subject = 'Invitation for' + eventName;
      return mailOptions;
    };
  exports.sendInvite = function(req, res) {
    var userMailOptions = {
      to: 'abc@gmail.com',
      from: 'xyz@gmail.com',
    };
    userMailOptions = eventEmailToUser(user, events, eventCreator, userMailOptions);
    var userEmailresult = sendMail(userMailOptions);
  };
zulekha
  • 313
  • 9
  • 17

1 Answers1

0

The first issue that strikes me is that you are missing an ATTENDEE property which is required in any iMIP REQUEST.

There might be other issues but we would need to see your full MIME message instead of your code to really spot those. As a starter, you might want to doublecheck Multipart email with text and calendar: Outlook doesn't recognize ics

Community
  • 1
  • 1
Arnaud Quillaud
  • 4,420
  • 1
  • 12
  • 8