diff --git a/backend/src/services/emails.ts b/backend/src/services/emails.ts new file mode 100644 index 0000000..bef1fff --- /dev/null +++ b/backend/src/services/emails.ts @@ -0,0 +1,34 @@ +import "dotenv/config"; +import nodemailer from "nodemailer"; +import env from "../util/validateEnv"; + +/** + * Sends a notification email to PAP staff when a VSR is submitted. + * Throws an error if the email could not be sent. + * + * @param name Name of veteran who submitted the VSR + * @param email Email address of veteran who submitted the VSR + */ +const sendVSRNotificationEmailToStaff = async (name: string, email: string) => { + const EMAIL_SUBJECT = "New VSR Submitted"; + const EMAIL_BODY = `A new VSR was submitted by ${name} from ${email}`; + + const transporter = nodemailer.createTransport({ + service: "gmail", + auth: { + user: env.EMAIL_USER, + pass: env.EMAIL_APP_PASSWORD, + }, + }); + + const mailOptions = { + from: env.EMAIL_USER, + to: env.EMAIL_NOTIFICATIONS_RECIPIENT, + subject: EMAIL_SUBJECT, + text: EMAIL_BODY, + }; + + await transporter.sendMail(mailOptions); +}; + +export { sendVSRNotificationEmailToStaff }; diff --git a/backend/src/util/validateEnv.ts b/backend/src/util/validateEnv.ts index 6d04f81..410a855 100644 --- a/backend/src/util/validateEnv.ts +++ b/backend/src/util/validateEnv.ts @@ -12,5 +12,6 @@ export default cleanEnv(process.env, { FRONTEND_ORIGIN: str(), // URL of frontend, to allow CORS from frontend EMAIL_USER: email(), // Email address to use for sending emails EMAIL_APP_PASSWORD: str(), // App password to use for sending emails + EMAIL_NOTIFICATIONS_RECIPIENT: email(), // Recipient of VSR notification emails BACKEND_FIREBASE_SETTINGS: json(), // Firebase settings for backend, stored as a JSON string });