如何使用nodejs在日历中添加ics文件事件

时间:2016-05-24 11:06:24

标签: node.js icalendar

有一种情况我需要向最终用户发送活动会议邀请。我能够生成ICS文件并将其作为附件发送。但ICS文件在User Calander中无法读取或添加。

生成和发送电子邮件的代码如下:

  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);
  };

1 个答案:

答案 0 :(得分:0)

第一个令我印象深刻的问题是您缺少任何iMIP REQUEST所需的ATTENDEE属性。

可能还有其他问题,但我们需要查看完整的MIME邮件而不是您的代码才能真正发现这些问题。作为入门者,您可能需要重新检查Multipart email with text and calendar: Outlook doesn't recognize ics