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 #451

Closed that-spy-kid closed 11 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

const transport = nodemailer.createTransport( nodemailerMailjet({}) ); const email = new Email({views: {}, message: {}, preview: false, send: true, transport: transport,}); email.send({});

Checklist

titanism commented 11 months ago

This is related to Nodemailer usage, not our project.

that-spy-kid commented 11 months ago

Hey @titanism Nodemailer Said, “ You should probably check it with the email templates project, Nodemailer can send to multiple cc addresses just fine. “

Can you guys check that send() method in the email-templates….. that’s actually causing error!

This is error Message it’s throwing…. I assignment cc: [ ‘sample1@gmail.com’ , ‘sample2@gmail.com’ ]

Throwing this error for comma-separated string

Error: Unsuccessful: Status Code: "400" Message: "Bad Request"; "sample1@gmail.com, sample2@gmail.com" is an invalid email address. at fn (C:\Temp\micro-v3-main-api-node\node_modules\node-mailjet\mailjet-client.js:326:23) at Request.callback (C:\Temp\micro-v3-main-api-node\node_modules\node-mailjet\node_modules\superagent\src\node\index.js:923:3) at fn (C:\Temp\micro-v3-main-api-node\node_modules\node-mailjet\node_modules\superagent\src\node\index.js:1153:18) at IncomingMessage. (C:\Temp\micro-v3-main-api-node\node_modules\node-mailjet\node_modules\superagent\src\node\parsers\json.js:19:7)

titanism commented 11 months ago

Your "Code to reproduce" isn't even accurate. email.send() is async and require you do to await email.send().

Also you don't even have in your code example a CC option.

Please provide us with a reproducible working test that shows us that this doesn't work the way you want it to and there is a bug. Otherwise we can't help you.

that-spy-kid commented 11 months ago

@titanism

Here is my actually function that I was used to send mails using email-templates!

CODE: 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!

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

titanism commented 11 months ago

@that-spy-kid you have typo attachements should be attachments as far as I know - also you could try a test doing just cc: 'example@you.com', or cc: ['example@you.com','another@you.com'] (replace of course with actual emails)

that-spy-kid commented 11 months ago

@titanism Yes! 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!