Skip to content

Commit

Permalink
Give meaningful message when no SMTP server is configured
Browse files Browse the repository at this point in the history
  • Loading branch information
floscher committed Jul 1, 2024
1 parent 23183cb commit 33d2341
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions server/src/service/email-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import { UserEntity } from "../entity/User.entity.js";
import logger from "../logger.js";
import { ClientSettings, EmailSettings } from "../settings.js";

export async function sendNotificationEmailAboutNewRegistration(newUsername: string) {
const admin_emails = await AppDataSource.manager
export function sendNotificationEmailAboutNewRegistration(newUsername: string) {
if (!canSendEmail()) {
console.info("Could not send email notification about new user registration to admins. No SMTP server is configured in `.env` file.");
return;
}

AppDataSource.manager
.getRepository(UserEntity)
.createQueryBuilder("find all admins")
.where("roles @> :role", { role: ["ADMIN"] }) // https://www.postgresql.org/docs/16/functions-array.html#ARRAY-OPERATORS-TABLE
Expand All @@ -18,34 +23,35 @@ export async function sendNotificationEmailAboutNewRegistration(newUsername: str
.catch((e) => {
console.error("Failed to get admins", e);
return [];
});

if (admin_emails.length <= 0) {
logger.warn(`There are no admins yet. No notification email is sent about new user '${newUsername}'.`);
} else {
logger.debug(`Sending notification email about registration of new user '${newUsername}' to ${admin_emails.join(", ")}`);
})
.then((adminEmails) => {
if (adminEmails.length <= 0) {
logger.warn(`There are no admins yet. No notification email is sent about new user '${newUsername}'.`);
} else {
logger.debug(`Sending notification email about registration of new user '${newUsername}' to ${adminEmails.join(", ")}`);

await sendEmail(
admin_emails,
"New user registered",
`Hi admins,
sendEmail(
adminEmails,
"New user registered",
`Hi admins,
a new user '${encodeURIComponent(newUsername)}' registered at ${ClientSettings.BASE_URL} .
Visit ${ClientSettings.BASE_URL}/administration in order to give them some permissions.
Kind regards,
Your fuBlog`,
);
}
);
}
});
}

function sendEmail(to: string[], subject: string, text: string) {
if (!(EmailSettings.SMTP_HOST ?? EmailSettings.SMTP_PORT)) {
throw new Error("No SMTP email server set! Cannot send email.");
}
function canSendEmail(): boolean {
return !!(EmailSettings.SMTP_HOST && EmailSettings.SMTP_PORT && EmailSettings.SMTP_FROM);
}

function sendEmail(to: string[], subject: string, text: string) {
createTransport(EmailSettings.SMTP_OPTIONS)
.sendMail({
from: "fublog@fumix.de",
from: EmailSettings.SMTP_FROM,
to,
subject,
text,
Expand Down

0 comments on commit 33d2341

Please sign in to comment.