Skip to content

Commit 090c1da

Browse files
committed
Refactor email mailer out for use elsewhere
1 parent bb4a175 commit 090c1da

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

api/src/email.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import nodemailer from 'nodemailer';
2+
3+
const {
4+
SMTP_HOST,
5+
SMTP_PORT,
6+
SMTP_USERNAME,
7+
SMTP_PASSWORD
8+
} = process.env;
9+
10+
if (!SMTP_HOST) throw new Error('No SMTP host configured');
11+
if (!SMTP_PORT) throw new Error('No SMTP port configured');
12+
if (!SMTP_USERNAME) throw new Error('No SMTP user configured');
13+
if (!SMTP_PASSWORD) throw new Error('No SMTP password configured');
14+
15+
export const mailer = nodemailer.createTransport({
16+
host: SMTP_HOST,
17+
port: Number(SMTP_PORT),
18+
secure: true,
19+
auth: {
20+
user: SMTP_USERNAME,
21+
pass: SMTP_PASSWORD
22+
}
23+
});

api/src/functions/contact-form.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,14 @@
11
import * as _ from 'lodash';
2-
import nodemailer from 'nodemailer';
32
import * as log from 'loglevel';
43
import { delay } from '@httptoolkit/util';
54

65
import { catchErrors, reportError } from '../errors';
76
import { getCorsResponseHeaders } from '../cors';
7+
import { mailer } from '../email';
88

9-
const {
10-
CONTACT_FORM_DESTINATION,
11-
SMTP_HOST,
12-
SMTP_PORT,
13-
SMTP_USERNAME,
14-
SMTP_PASSWORD
15-
} = process.env;
16-
9+
const { CONTACT_FORM_DESTINATION } = process.env;
1710
if (!CONTACT_FORM_DESTINATION) throw new Error('No contact form destination configured');
1811

19-
if (!SMTP_HOST) throw new Error('No SMTP host configured');
20-
if (!SMTP_PORT) throw new Error('No SMTP port configured');
21-
if (!SMTP_USERNAME) throw new Error('No SMTP user configured');
22-
if (!SMTP_PASSWORD) throw new Error('No SMTP password configured');
23-
24-
const mailer = nodemailer.createTransport({
25-
host: SMTP_HOST,
26-
port: Number(SMTP_PORT),
27-
secure: true,
28-
auth: {
29-
user: SMTP_USERNAME,
30-
pass: SMTP_PASSWORD
31-
}
32-
});
33-
3412
const THANK_YOU_PAGE = 'https://httptoolkit.com/contact-thank-you/'
3513

3614
export const handler = catchErrors(async (event) => {

0 commit comments

Comments
 (0)