summaryrefslogtreecommitdiff
path: root/api/_send-email.ts
blob: 73f0bc3b31ae524a52f69f3ae1c9917c9602c2dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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,
};