diff options
| author | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2024-04-07 09:59:17 +0100 |
|---|---|---|
| committer | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2024-04-07 09:59:17 +0100 |
| commit | 730c589a34e181b22bf720ca67f002baa0910b17 (patch) | |
| tree | da01a35f246d3010aa6a34f82f522b26fb01c74e /api/_send-email.ts | |
| parent | 7c923ef405a9128b8a0367094fce99833ec99c54 (diff) | |
git add api! WIP git add api! contact-form api endpointcontact-form
Diffstat (limited to 'api/_send-email.ts')
| -rw-r--r-- | api/_send-email.ts | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/api/_send-email.ts b/api/_send-email.ts new file mode 100644 index 0000000..73f0bc3 --- /dev/null +++ b/api/_send-email.ts @@ -0,0 +1,41 @@ +import nodemailer from 'nodemailer'; +import CONTACT_DETAILS from '_contactDetails'; + +const transporter = nodemailer.createTransport({ + host: TODO, + port: 587, + secure: false, // Use `true` for port 465, `false` for all other ports + auth: { + user: TODO, + pass: TODO, + }, +}); + +export async function sendEmail({ recipient, senderName, senderEmail, subject, body }: SendEmailArgs) { + const validRecipients = Object.keys(CONTACT_DETAILS); + if (!validRecipients.includes(recipient)) { + throw new Error( + 'Invalid recipient. Recipient must be one of: ' + + validRecipients.join(', ') + + '.' + ); + } + const contactDetails = CONTACT_DETAILS[recipient]; + + const info = await transporter.sendMail({ + from: `"${senderName}" <${senderEmail}>`, + to: contactDetails.email, + subject, + text: body, + }); + + console.log("Message sent: %s", info.messageId); +} + +type SendEmailArgs = { + recipient: string, + senderName: string, + senderEmail: string, + subject: string, + body: string, +}; |
