11import { Transporter , createTransport } from 'nodemailer' ;
2+ import aws , { SES } from '@aws-sdk/client-ses' ;
23import hbs from 'nodemailer-express-handlebars' ;
34
5+
6+ // verify env integrity
7+ if ( process . env . NODE_ENV === 'production' ) {
8+ if ( ! process . env . SES_ACCESS_KEY_ID || ! process . env . SES_SECRET_ACCESS_KEY || ! process . env . EMAIL_USER ) {
9+ console . error ( 'Missing SES_ACCESS_KEY_ID or SES_SECRET_ACCESS_KEY' ) ;
10+ process . exit ( 1 ) ;
11+ }
12+ } else {
13+ if ( ! process . env . EMAIL_USER || ! process . env . EMAIL_PASSWORD ) {
14+ console . error ( 'Missing EMAIL_USER or EMAIL_PASSWORD' ) ;
15+ process . exit ( 1 ) ;
16+ }
17+ }
18+
19+ const ses =
20+ process . env . NODE_ENV === 'production'
21+ ? new SES ( {
22+ apiVersion : '2010-12-01' ,
23+ region : 'ap-southeast-1' ,
24+ credentials : {
25+ accessKeyId : process . env . SES_ACCESS_KEY_ID as string ,
26+ secretAccessKey : process . env . SES_SECRET_ACCESS_KEY as string ,
27+ } ,
28+ } )
29+ : null ;
30+
31+ const transporterOptions =
32+ process . env . NODE_ENV === 'production'
33+ ? {
34+ SES : { ses, aws } ,
35+ }
36+ : {
37+ service : 'gmail' ,
38+ host : 'smtp.gmail.com' ,
39+ port : 587 ,
40+ secure : false ,
41+ auth : {
42+ user : process . env . EMAIL_USER as string ,
43+ pass : process . env . EMAIL_PASSWORD as string ,
44+ } ,
45+ } ;
46+
447let transporter : Transporter ;
548try {
6- transporter = createTransport ( {
7- service : 'gmail' ,
8- host : 'smtp.gmail.com' ,
9- port : 587 ,
10- secure : false ,
11- auth : {
12- user : process . env . EMAIL_USER as string ,
13- pass : process . env . EMAIL_PASSWORD as string ,
14- } ,
15- } ) ;
49+ transporter = createTransport ( transporterOptions ) ;
1650} catch ( err ) {
1751 console . error ( err ) ;
1852 process . exit ( 1 ) ;
@@ -31,15 +65,13 @@ transporter.use('compile', hbs(handlebarsOptions));
3165
3266export default transporter ;
3367
34- /*
3568await transporter . sendMail ( {
3669 from : {
3770 name : 'hi' ,
3871 address : process . env . EMAIL_USER as string ,
3972 } , // sender address
4073 to : [ 'sebastian.ong@hotmail.com' ] , // list of receivers
41- subject: " Hello ✔" , // Subject line
42- text: " Hello world?" , // plain text body
43- html: " <b>Hello world?</b>" , // html body
74+ subject : ' Hello ✔' , // Subject line
75+ text : ' Hello world?' , // plain text body
76+ html : ' <b>Hello world?</b>' , // html body
4477} ) ;
45- */
0 commit comments