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 12, 2024
1 parent d7c83d3 commit a5ab2bc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 41 deletions.
24 changes: 16 additions & 8 deletions src/lib/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,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 +113,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 a5ab2bc

Please sign in to comment.