forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.64k stars 339 forks source link

[fix] CC emails not working if we specify mutiple emails instead of single email #452

Closed that-spy-kid closed 10 months ago

that-spy-kid commented 11 months ago

Describe the bug

Node.js version: v16.14.1

OS version: windows 11

Description:

Actual behavior

Not working for adding mutilple emails, -> 'cc_recipient1@example.com, cc_recipient2@example.com' -> ['cc_recipient1@example.com', 'cc_recipient2@example.com']

Expected behavior

Working for only sinlge email wrapped in CC, -> 'cc_recipient1@example.com' ->['cc_recipient1@example.com']

Code to reproduce

import Email from 'email-templates'; import fs from 'fs'; import nodemailer from 'nodemailer'; import nodemailerMailjet from 'nodemailer-mailjet-transport';

const transport = nodemailer.createTransport( nodemailerMailjet({ auth: { apiKey: 'my-key', apiSecret: 'my-value', }, }), );

export const sendMail = async ( companyId: string, emailId: string, body: string, subject: string, CC: string[], attachements: Record<string, any>[], template?: string, otherParams?: Record<string, any>, ) => { if (emailId && template) { const email = new Email({ views: { root: __dirname, options: { extension: 'pug' } }, message: { from: 'sample@gmail.com', }, preview: false, send: true, transport: transport, }); console.log(CC, subject, body, emailId, 'emaiiillll'); email .send({ template: template, message: { cc: CC ? CC : '', to: emailId, attachments: attachements, }, locals: { name: companyId, subject: ${subject}, body: body, url: 'my-url', ...otherParams, }, }) .then(() => { attachements.length && attachements.forEach((eachFile: any) => { fs.unlink(${eachFile.path}, (err) => { if (err) { logger.error(err); } }); }); console.log(Email successfully Sent to ${emailId}); }) .catch((e: any) => { console.log(e); }); return { error: false, message: email successfully Sent to ${emailId} }; } else { console.log(emailId, 'email'); console.log(emailId); return { error: true, message: email not sent ${emailId} }; } };

So in message: { cc: }, here I tried all types like the one I mentioned in above! And attachements accessing from sendMail() parameters!

Can you help me with this sorry for the inconvenience caused by me! Thank you so much in advance for your help!

Checklist

that-spy-kid commented 11 months ago

With just one email in cc like ‘example@you.com' or [‘example@you.com'] working fine but multiple cc email entries not working though! Like this ‘example@you.com,anyother@you.com' or [‘example@you.com', ‘anyother@you.com’, …] Also I’m accessing attachements from sendMail function parameters not from the parameters from email.send() method! So attachements Fine!

titanism commented 10 months ago

Submit a PR that shows a test case failing.