如何在节点js中附上带有电子邮件的.ics文件?

时间:2016-08-27 04:15:55

标签: node.js email sendgrid

我正在使用ics进行日历以获得成功完成的事件,但我想发送一些带有.ics附件的文本,为此我尝试使用此代码只是看到并告诉我在哪里需要更改。 我收到了event.ics文件,但想发送带有邮件文本/ html内容的文件。

var ics = require('ics');
var options = {
    eventName: 'Fingerpainting lessons',
    filename: 'event1.ics',
    dtstart: date,
    location: 'Here and there',
    email: {
        name: 'alex bob',
        email: email
    }
};
ics.createEvent(options, null, function(err, calendar) {
    if (!err)
    {
            console.log('Event file',calendar);
            var filePath = path.join('vabo_email', 'email.html');
                fs.readFile(filePath, {encoding: 'utf-8'},
                function(err, data) {
                        if ( ! err )
                        {

                            var helper = require('sendgrid').mail;

                            from_email = new helper.Email('booking@callvabo.com')
                            to_email = new helper.Email(email)
                            subject = subject
                            //Dynamic content
                            var data = data.replace("$content", bodycontent)

                            content = new helper.Content('text/html', data)
                            //content = new helper.Content("text/Calendar",calendar)
                            mail = new helper.Mail(from_email, subject, to_email,content);

                            var sg = require('sendgrid')('secretekey');

                            var requestBody = mail.toJSON();
                            var request = sg.emptyRequest();
                            request.method = 'POST';
                            request.path = '/v3/mail/send';
                            request.body = requestBody;
                            data = new helper.Content("text/Calendar",calendar)
                            request.attachments = [{'filename': 'calendar.ics', 'content': data}]

                            sg.API(request, function (error, response) {
                                if ( ! error )
                                {
                                        console.log('mail send Successfully to',email);
                                }
                                else
                                {
                                        console.log('Error for send mail',error);
                                }
                            });
                        }
                        else
                        {
                                console.log('Error for calendar',err);
                        }
                        });
    }
    else
    {
            console.log('Error for creating Event',err);
    }
});

1 个答案:

答案 0 :(得分:3)

日历仅包含文件路径和名称。你需要读入文件内容然后你需要Base64编码文件内容最后request.attachments是不正确的。将附件添加到邮件对象或requestBody。

mail = new helper.Mail(from_email, subject, to_email,content);
var base64Content = fs.readFileSync(calendar).toString('base64');
mail.attachments = [{'filename': 'calendar.ics', 'content': base64Content, 'type': 'text/Calendar'}]

var sg = require('sendgrid')('secretekey');

var requestBody = mail.toJSON();
var request = sg.emptyRequest();
request.method = 'POST';
request.path = '/v3/mail/send';
request.body = requestBody;

sg.API(request, function (error, response) {