如何使用nodemailer正确发送会议请求?

时间:2014-06-20 08:53:44

标签: node.js email icalendar nodemailer

我正在尝试使用以下代码使用nodemailer发送会议请求。我面临的问题是,会议邀请将作为附件ics文件而不是可以直接添加的请求。我在多个邮件客户端上尝试过它。任何指针都将受到高度赞赏。

transport.sendMail({
                    from: 'BakBak.io <biplav.saraf@gmail.com>',
                    to: 'biplav.saraf@gmail.com',
                    subject: 'Meeting',
                    //html: "Hi",
                    text: "Hola!!",
                    alternative: {
                      contentType: "text/calendar; method=REQUEST; name='meeting.ics';component=VEVENT",
                      contents: new Buffer(cal.toString()),
                      contentEncoding:"7bit",
                      "Content-Class":"urn:content-classes:calendarmessage"
                    },
                    headers: {
                              "Content-Type": "text/calendar", 
                              //"charset":"utf-8",
                              "method":"REQUEST",
                              "component":"VEVENT",
                              "Content-Class":"urn:content-classes:calendarmessage"
                            }//,
                    //attachments : [{filename:'invite.ics',contents: cal.toString()}]
                    }, function(err, responseStatus) {
                    if (err) {
                        console.log(err);
                        res.render('schedule',{errors: err.message});
                    } else {
                        console.log(responseStatus.message);
                        res.render('schedule',{success_msg: "Successfully Created!"});
                    }
                });

3 个答案:

答案 0 :(得分:2)

如果发件人和收件人相同,则Gmail不会显示会议请求并提供添加到日历的选项。

这对我有用:

transport.sendMail({
                    from: 'BakBak.io <biplav.saraf@gmail.com>',
                    to: 'donateoldspectacles@gmail.com',
                    subject: 'Meeting',
                    html: "Hiya!!",
                    text: "Hola!!",
                    alternatives: [{
                      contentType: "text/calendar",
                      content: new Buffer(ical)
                    }]
                    }, function(err, responseStatus) {
                    if (err) {
                        console.log(err);
                        res.render('schedule',{errors: err.message});
                    } else {
                        console.log(responseStatus.message);
                        res.render('schedule',{success_msg: "Successfully Created!"});
                    }
                });

答案 1 :(得分:1)

这对我有用,而无需使用ical-generator

const express    = require('express');
const http       = require('http');
const bodyParser = require('body-parser');
const nodeMailer = require('nodemailer');
const cors  = require('cors');

const app = express();

app.use(cors());

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post("/v1/sendemail", (req, res) => {

  let transporter = nodeMailer.createTransport({
    host: "smtp.xxx.de",
    secureConnection: true,
    port: 465,
    tls: {
      chipers: "SSLv3"
    },
    auth: {
      user: "xxx@xxx.xxx",
      pass: "xxx"
    }
  });
  let content = 'BEGIN:VCALENDAR\n' +
        'VERSION:2.0\n' +
        'BEGIN:VEVENT\n' +
        'SUMMARY:Summary123\n' +
        'DTSTART;VALUE=DATE:20201030T093000Z\n' +
        'DTEND;VALUE=DATE:20201030T113000Z\n' +
        'LOCATION:Webex \n' +
        'DESCRIPTION:Description123\n' +
        'STATUS:CONFIRMED\n' +
        'SEQUENCE:3\n' +
        'BEGIN:VALARM\n' +
        'TRIGGER:-PT10M\n' +
        'DESCRIPTION:Description123\n' +
        'ACTION:DISPLAY\n' +
        'END:VALARM\n' +
        'END:VEVENT\n' +
        'END:VCALENDAR';

  let mailOptions = {
    from: "from@xxx.xx",
    to: "to@xxx.xx",
    subject: "Subject",
    text: "Test",
    icalEvent: {
        filename: "invitation.ics",
        method: 'request',
        content: content
        }
      };

  transporter.sendMail(mailOptions, function(error, info) {
    if (error) {
      res.status(500).send({
        message: {
          Error: "Could not sent email"
        }
      })
    }
  });
});

const port = process.env.PORT || '3001';
app.set('port', port);

const server = http.createServer(app);
server.listen(port, () => {
  console.log(`API running on localhost:${port}`);
});

答案 2 :(得分:0)

我认为问题在于:"Content-Type": "text/calendar" 如果您只想将内容添加为纯文本或HTML,用户可以单击电子邮件中的链接进行添加,那么您可能需要使用textHTML标题。您只需将链接发送到Google日历,例如,用户只需点击该按钮即可。加入活动。

"Content-Type": "text/calendar"使邮件发件人/客户端相信附有实际文件或与电子邮件关联。

Update:

附加的ICS文件必须附加,否则邮件客户端无法告诉它是一个事件,您只需要使用正确的标头。例如:

Content-Type: text/calendar; method=REQUEST
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename=iCal-20140610-083450.ics

附加.ics文件。邮件客户端将足够聪明,可以解决问题,并提供一个选项,以便在点击时添加到本地日历。

有些邮件客户端(如AirMail)会自动检测文本中的任何日期,不需要附件,并允许用户直接将事件集成到本地日历,但此功能无法广泛使用。 / p>