From a5ab2bcaee67ab9c2e21653e5aa07a549da4ea21 Mon Sep 17 00:00:00 2001 From: Ovv Date: Tue, 12 Nov 2024 16:48:42 +0100 Subject: [PATCH] nodemailer: Add support for no-auth and fix from address for attendee emails --- src/lib/email.ts | 24 ++++++++++++++++-------- src/routes.js | 48 +++++++++++++++--------------------------------- 2 files changed, 31 insertions(+), 41 deletions(-) diff --git a/src/lib/email.ts b/src/lib/email.ts index f19e5dc..a8399cf 100644 --- a/src/lib/email.ts +++ b/src/lib/email.ts @@ -50,15 +50,19 @@ export const initEmailService = async (): Promise => { 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(); @@ -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({ diff --git a/src/routes.js b/src/routes.js index 2cbec4f..49718ff 100755 --- a/src/routes.js +++ b/src/routes.js @@ -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); @@ -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, }; @@ -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, }; @@ -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, }; @@ -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, }; @@ -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, }; @@ -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, }; @@ -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, };