Alex
Alex

Reputation: 838

how to use multiple content for send mail in node js?

I am using sendgrid for sending mail. I can send mail from anyone having content type of either text/html or text/Calendar, but I want to send both.

Here is the code I currently have:

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('[email protected]')
                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);
            }
        });
    }
});

Upvotes: 2

Views: 294

Answers (1)

enRaiser
enRaiser

Reputation: 2646

as per the sendgrid documentation, this is the way it has to be done, without helper class.

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

So it is obvious its an array of type/data pair.

So You many try sending without helper or , you may try this , I am not sure.

 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)

Upvotes: 1

Related Questions