Update email code to send an email per EditUserCell

using multiple recipients is break of privacy since user will be able
to see each other's emails.
This commit is contained in:
Kurt Hutten
2021-07-01 20:12:17 +10:00
parent 43376b8214
commit 084c4afdc3
7 changed files with 31 additions and 14 deletions

View File

@@ -7,8 +7,6 @@ export const schema = gql`
type EmailResponse {
accepted: [String!]!
rejected: [String!]!
messageId: String!
envelope: Envelope
}
input Email {

View File

@@ -1,7 +1,7 @@
import nodemailer, { SendMailOptions } from 'nodemailer'
interface Args {
to: SendMailOptions['to']
export interface SendMailArgs {
to: string
from: SendMailOptions['from']
subject: string
text: string
@@ -26,7 +26,7 @@ export function sendMail({
from,
subject,
text,
}: Args): Promise<SuccessResult> {
}: SendMailArgs): Promise<SuccessResult> {
const transporter = nodemailer.createTransport({
host: 'smtp.mailgun.org',
port: 587,

View File

@@ -1,26 +1,45 @@
import { requireAuth } from 'src/lib/auth'
import { sendMail } from 'src/lib/sendmail'
import type { SendMailArgs } from 'src/lib/sendmail'
import { users } from 'src/services/users/users'
export const sendAllUsersEmail = async ({ input: { body, subject } }) => {
requireAuth({ role: 'admin' })
const recipients = (await users()).map(({ email }) => email)
const from = {
address: 'news@mail.cadhub.xyz',
name: 'CadHub',
}
const result = await sendMail({
to: recipients,
const emails: SendMailArgs[] = (await users()).map(({ email }) => ({
to: email,
from,
subject,
text: body,
}))
const emailPromises = emails.map((email) => sendMail(email))
const accepted = []
const rejected = []
const result = await Promise.allSettled(emailPromises)
result.forEach((result) => {
if (result.status === 'fulfilled') {
accepted.push(result.value.accepted[0])
} else {
rejected.push(result.reason)
}
})
await sendMail({
to: 'k.hutten@protonmail.ch',
from,
subject: `All users email report`,
text: JSON.stringify(result, null, 2),
text: JSON.stringify(
{
accepted,
rejected,
originalEmailList: emails,
},
null,
2
),
})
return result
return { accepted, rejected }
}