From 2c0260d1069fc525635f69b1020043a09d5228e4 Mon Sep 17 00:00:00 2001 From: Rafael Tapia Date: Mon, 29 Jan 2024 16:01:10 -0300 Subject: [PATCH] fix: dryrun option sending email to all queried users (#31571) Co-authored-by: Hugo Costa <20212776+hugocostadev@users.noreply.github.com> --- .changeset/grumpy-eagles-roll.md | 5 ++ .../server/functions/sendMail.ts | 47 ++++++++++--------- .../end-to-end/api/livechat/12-mailer.ts | 2 +- 3 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 .changeset/grumpy-eagles-roll.md diff --git a/.changeset/grumpy-eagles-roll.md b/.changeset/grumpy-eagles-roll.md new file mode 100644 index 000000000000..37e0c7af1688 --- /dev/null +++ b/.changeset/grumpy-eagles-roll.md @@ -0,0 +1,5 @@ +--- +"@rocket.chat/meteor": patch +--- + +Fixed Mail dryrun sending email to all users diff --git a/apps/meteor/app/mail-messages/server/functions/sendMail.ts b/apps/meteor/app/mail-messages/server/functions/sendMail.ts index ce57fc06b1c8..50435bc24812 100644 --- a/apps/meteor/app/mail-messages/server/functions/sendMail.ts +++ b/apps/meteor/app/mail-messages/server/functions/sendMail.ts @@ -36,33 +36,38 @@ export const sendMail = async function ({ userQuery = { $and: [userQuery, EJSON.parse(query)] }; } - const users = await Users.find(userQuery).toArray(); - if (dryrun) { - for await (const u of users) { - const user: Partial & Pick = u; - const email = `${user.name} <${user.emails?.[0].address}>`; - const html = placeholders.replace(body, { - unsubscribe: Meteor.absoluteUrl( - generatePath('mailer/unsubscribe/:_id/:createdAt', { - _id: user._id, - createdAt: user.createdAt?.getTime().toString() || '', - }), - ), - name: user.name, - email, - }); + const user = await Users.findOneByEmailAddress(from); - SystemLogger.debug(`Sending email to ${email}`); - await Mailer.send({ - to: email, - from, - subject, - html, + if (!user) { + throw new Meteor.Error('error-invalid-user', 'Invalid user', { + function: 'Mailer.sendMail', }); } + + const email = `${user.name} <${user.emails?.[0].address}>`; + const html = placeholders.replace(body, { + unsubscribe: Meteor.absoluteUrl( + generatePath('mailer/unsubscribe/:_id/:createdAt', { + _id: user._id, + createdAt: user.createdAt?.getTime().toString() || '', + }), + ), + name: user.name, + email, + }); + + SystemLogger.debug(`Sending email to ${email}`); + return Mailer.send({ + to: email, + from, + subject, + html, + }); } + const users = await Users.find(userQuery).toArray(); + for await (const u of users) { const user: Partial & Pick = u; if (user?.emails && Array.isArray(user.emails) && user.emails.length) { diff --git a/apps/meteor/tests/end-to-end/api/livechat/12-mailer.ts b/apps/meteor/tests/end-to-end/api/livechat/12-mailer.ts index e49628ce718e..01a47594620d 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/12-mailer.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/12-mailer.ts @@ -13,7 +13,7 @@ describe('Mailer', () => { .post(api('mailer')) .set(credentials) .send({ - from: 'test-email@example.com', + from: 'rocketchat.internal.admin.test@rocket.chat', subject: 'Test email subject', body: 'Test email body [unsubscribe]', dryrun: true,