diff options
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, +}; |
