Skip to content

Commit

Permalink
nodemailer: Add support for no-auth and fix from address for attendee…
Browse files Browse the repository at this point in the history
… emails
  • Loading branch information
ovv committed Nov 13, 2024
1 parent d7c83d3 commit 4ddacf8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 44 deletions.
28 changes: 17 additions & 11 deletions src/lib/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ export const initEmailService = async (): Promise<boolean> => {
case "nodemailer":
if (
!config.nodemailer?.smtp_server ||
!config.nodemailer?.smtp_port ||
!config.nodemailer?.smtp_username ||
!config.nodemailer?.smtp_password
!config.nodemailer?.smtp_port
) {
return exitWithError(
"Nodemailer is configured as the email service, but not all required fields are provided. Please provide all required fields in the config file.",
Expand All @@ -50,15 +48,19 @@ export const initEmailService = async (): Promise<boolean> => {
const nodemailerConfig = {
host: config.nodemailer?.smtp_server,
port: Number(config.nodemailer?.smtp_port) || 587,
auth: {
user: config.nodemailer?.smtp_username,
pass: config.nodemailer?.smtp_password,
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false,
},
} as SMTPTransport.Options;

if (config.nodemailer?.smtp_username) {
nodemailerConfig.auth = {
user: config.nodemailer?.smtp_username,
pass: config.nodemailer?.smtp_password
};
}

const nodemailerTransporter =
nodemailer.createTransport(nodemailerConfig);
const nodemailerVerified = await nodemailerTransporter.verify();
Expand Down Expand Up @@ -109,11 +111,15 @@ export const sendEmail = async (
const nodemailerConfig = {
host: config.nodemailer?.smtp_server,
port: Number(config.nodemailer?.smtp_port) || 587,
auth: {
user: config.nodemailer?.smtp_username,
pass: config.nodemailer?.smtp_password,
},
} as SMTPTransport.Options;

if (config.nodemailer?.smtp_username) {
nodemailerConfig.auth = {
user: config.nodemailer?.smtp_username,
pass: config.nodemailer?.smtp_password
};
}

const nodemailerTransporter =
nodemailer.createTransport(nodemailerConfig);
await nodemailerTransporter.sendMail({
Expand Down
48 changes: 15 additions & 33 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ if (config.general.mail_service) {
host: config.nodemailer?.smtp_server,
port: config.nodemailer?.smtp_port,
secure: false, // true for 465, false for other ports
auth: {
user: config.nodemailer?.smtp_username,
pass: config.nodemailer?.smtp_password,
},
});

if (config.nodemailer?.smtp_username) {
nodemailerTransporter.auth = {
user: config.nodemailer?.smtp_username,
pass: config.nodemailer?.smtp_password
};
}

nodemailerTransporter.verify((error, success) => {
if (error) {
console.log(error);
Expand Down Expand Up @@ -372,11 +376,7 @@ router.post("/deleteevent/:eventID/:editToken", (req, res) => {
function (err, html) {
const msg = {
to: attendeeEmails,
from: {
name: siteName,
email: contactEmail,
address: contactEmail,
},
from: contactEmail,
subject: `${siteName}: ${event.name} was deleted`,
html,
};
Expand Down Expand Up @@ -721,10 +721,7 @@ router.post("/attendevent/:eventID", async (req, res) => {
function (err, html) {
const msg = {
to: req.body.attendeeEmail,
from: {
name: siteName,
email: contactEmail,
},
from: contactEmail,
subject: `${siteName}: You're RSVPed to ${event.name}`,
html,
};
Expand Down Expand Up @@ -800,10 +797,7 @@ router.get("/oneclickunattendevent/:eventID/:attendeeID", (req, res) => {
function (err, html) {
const msg = {
to: req.body.attendeeEmail,
from: {
name: siteName,
email: contactEmail,
},
from: contactEmail,
subject: `${siteName}: You have been removed from an event`,
html,
};
Expand Down Expand Up @@ -872,10 +866,7 @@ router.post("/removeattendee/:eventID/:attendeeID", (req, res) => {
function (err, html) {
const msg = {
to: req.body.attendeeEmail,
from: {
name: siteName,
email: contactEmail,
},
from: contactEmail,
subject: `${siteName}: You have been removed from an event`,
html,
};
Expand Down Expand Up @@ -953,10 +944,7 @@ router.post("/subscribe/:eventGroupID", (req, res) => {
function (err, html) {
const msg = {
to: subscriber.email,
from: {
name: siteName,
email: contactEmail,
},
from: contactEmail,
subject: `${siteName}: You have subscribed to an event group`,
html,
};
Expand Down Expand Up @@ -1095,10 +1083,7 @@ router.post("/post/comment/:eventID", (req, res) => {
function (err, html) {
const msg = {
to: attendeeEmails,
from: {
name: siteName,
email: contactEmail,
},
from: contactEmail,
subject: `${siteName}: New comment in ${event.name}`,
html,
};
Expand Down Expand Up @@ -1226,10 +1211,7 @@ router.post("/post/reply/:eventID/:commentID", (req, res) => {
function (err, html) {
const msg = {
to: attendeeEmails,
from: {
name: siteName,
email: contactEmail,
},
from: contactEmail,
subject: `${siteName}: New comment in ${event.name}`,
html,
};
Expand Down

0 comments on commit 4ddacf8

Please sign in to comment.