如何在节点js中使用多个内容发送邮件?

时间:2016-08-27 09:04:51

标签: node.js

我正在使用sendgrid发送邮件。我可以从内容类型为text/htmltext/Calendar的任何人发送邮件,但我想发送这两种邮件。

以下是我目前的代码:

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 tmp_data = data
                fs.readFile(calendar, {encoding: 'utf-8'}, function(err, cal_data) {
                    if (!err)
                    {
                        var data = tmp_data.replace("$content", bodycontent).replace("$calendar", cal_data);
                        content = new helper.Content('text/html', data)
                        content = new helper.Content("text/Calendar", cal_data)

                        mail = new helper.Mail(from_email, subject, to_email, content);
                        var sg = require('sendgrid')('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');
                        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) {
                            if (!error)
                            {
                                console.log('mail send Successfully to', email);
                            }
                            else
                            {
                                console.log('Error for send mail', error);
                            }
                        });
                    }
                });
            }
            else
            {
                console.log('Can not read calendar', err);
            }
        });
    }
});

1 个答案:

答案 0 :(得分:1)

根据sendgrid文档,这是必须完成的方式,没有帮助类。

content: [
  {
    type: 'text/html',
    value: '<b>Hello, Email!'</b>,
  },
],

所以很明显它是一个类型/数据对的数组

所以你们很多人尝试在没有帮助的情况下发送,或者你可以试试这个,我不确定。

 content1 = new helper.Content('text/html', data)
 content2 = new helper.Content("text/Calendar", cal_data)
 final_content = [content1,content2]
 mail = new helper.Mail(from_email, subject, to_email, final_content)