fix: dryrun option sending email to all queried users (#31571)

Co-authored-by: Hugo Costa <20212776+hugocostadev@users.noreply.github.com>
pull/29800/head^2
Rafael Tapia 2 years ago committed by GitHub
parent 08df231203
commit 2c0260d106
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5
      .changeset/grumpy-eagles-roll.md
  2. 47
      apps/meteor/app/mail-messages/server/functions/sendMail.ts
  3. 2
      apps/meteor/tests/end-to-end/api/livechat/12-mailer.ts

@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---
Fixed Mail dryrun sending email to all users

@ -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) {

@ -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,

Loading…
Cancel
Save