diff options
| author | Ovv <contact@ovv.dev> | 2024-11-12 16:48:42 +0100 | 
|---|---|---|
| committer | Ovv <contact@ovv.dev> | 2024-11-13 11:07:36 +0100 | 
| commit | 4ddacf84e5dbb55e69e74a086a02ebb5d0e0c7ce (patch) | |
| tree | 12a5e5ef8c9561fa36463b2405fbb8f0b763bcd6 | |
| parent | d7c83d30c36a16dfaed169349d4008bddf9a05fd (diff) | |
nodemailer: Add support for no-auth and fix from address for attendee emails
| -rw-r--r-- | src/lib/email.ts | 28 | ||||
| -rwxr-xr-x | src/routes.js | 48 | 
2 files changed, 32 insertions, 44 deletions
diff --git a/src/lib/email.ts b/src/lib/email.ts index f19e5dc..40b5200 100644 --- a/src/lib/email.ts +++ b/src/lib/email.ts @@ -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.", @@ -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(); @@ -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({ 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,                                              };  | 
