Skip to content

Commit

Permalink
fix: dryrun option sending email to all queried users (#31571)
Browse files Browse the repository at this point in the history
Co-authored-by: Hugo Costa <[email protected]>
  • Loading branch information
tapiarafael and hugocostadev authored Jan 29, 2024
1 parent 08df231 commit 2c0260d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-eagles-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixed Mail dryrun sending email to all users
47 changes: 26 additions & 21 deletions apps/meteor/app/mail-messages/server/functions/sendMail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IUser> & Pick<IUser, '_id'> = 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<IUser> & Pick<IUser, '_id'> = u;
if (user?.emails && Array.isArray(user.emails) && user.emails.length) {
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/tests/end-to-end/api/livechat/12-mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Mailer', () => {
.post(api('mailer'))
.set(credentials)
.send({
from: 'test[email protected]',
from: 'rocketchat.internal.admin.test@rocket.chat',
subject: 'Test email subject',
body: 'Test email body [unsubscribe]',
dryrun: true,
Expand Down

0 comments on commit 2c0260d

Please sign in to comment.