@@ -3,15 +3,15 @@ import { join } from 'node:path';
33
44import type { NextApiRequest , NextApiResponse } from 'next' ;
55
6- import { SendSmtpEmail , TransactionalEmailsApi } from '@getbrevo/brevo' ;
6+ import { BrevoClient , BrevoError } from '@getbrevo/brevo' ;
7+ import { TooManyRequestsError , UnauthorizedError } from '@getbrevo/brevo/dist/cjs/api' ;
78import mjml2html from 'mjml' ;
89
910import type { FormData } from '@scripts/types' ;
1011
11- const sendSMTPEmail = new SendSmtpEmail ( ) ;
12- const transactionalEmailsAPI = new TransactionalEmailsApi ( ) ;
13-
14- transactionalEmailsAPI [ 'authentications' ] [ 'apiKey' ] . apiKey = process . env . SENDINBLUE_API_KEY ?? '' ;
12+ const brevo = new BrevoClient ( {
13+ apiKey : process . env . SENDINBLUE_API_KEY ?? ''
14+ } ) ;
1515
1616const emailTemplate = readFileSync ( join ( process . cwd ( ) , 'email' , 'contact.mjml' ) , 'utf8' ) ;
1717
@@ -38,18 +38,28 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
3838
3939 const htmlContent = interpolateTemplate ( emailTemplate , data ) ;
4040
41- sendSMTPEmail . to = [ { email : 'hi@atanas.info' } ] ;
42- sendSMTPEmail . sender = { email : data . email } ;
43- sendSMTPEmail . subject = 'New contact form submission from https://atanas.info' ;
44- sendSMTPEmail . htmlContent = htmlContent ;
45-
4641 try {
47- result = await transactionalEmailsAPI . sendTransacEmail ( sendSMTPEmail ) ;
42+ result = await brevo . transactionalEmails . sendTransacEmail ( {
43+ to : [ { email : 'hi@atanas.info' } ] ,
44+ sender : { email : data . email } ,
45+ subject : 'New contact form submission from https://atanas.info' ,
46+ htmlContent : htmlContent
47+ } ) ;
4848
4949 return res . status ( 200 ) . json ( result ) ;
5050 } catch ( error ) {
5151 result = { error } ;
5252
53+ if ( error instanceof UnauthorizedError ) {
54+ console . error ( 'Invalid API key' ) ;
55+ } else if ( error instanceof TooManyRequestsError ) {
56+ const retryAfter = ( error . rawResponse ?. headers as any ) [ 'retry-after' ] ;
57+
58+ console . error ( `Rate limited. Retry after ${ retryAfter } seconds` ) ;
59+ } else if ( error instanceof BrevoError ) {
60+ console . error ( `API Error ${ error . statusCode } :` , error . message ) ;
61+ }
62+
5363 return res . status ( 400 ) . json ( result ) ;
5464 }
5565}
0 commit comments