@@ -138,6 +138,32 @@ export async function sendEmail(options: EmailOptions): Promise<SendEmailResult>
138138 }
139139}
140140
141+ interface UnsubscribeData {
142+ headers : Record < string , string >
143+ html ?: string
144+ text ?: string
145+ }
146+
147+ function addUnsubscribeData (
148+ recipientEmail : string ,
149+ emailType : string ,
150+ html ?: string ,
151+ text ?: string
152+ ) : UnsubscribeData {
153+ const unsubscribeToken = generateUnsubscribeToken ( recipientEmail , emailType )
154+ const baseUrl = getBaseUrl ( )
155+ const unsubscribeUrl = `${ baseUrl } /unsubscribe?token=${ unsubscribeToken } &email=${ encodeURIComponent ( recipientEmail ) } `
156+
157+ return {
158+ headers : {
159+ 'List-Unsubscribe' : `<${ unsubscribeUrl } >` ,
160+ 'List-Unsubscribe-Post' : 'List-Unsubscribe=One-Click' ,
161+ } ,
162+ html : html ?. replace ( / \{ \{ U N S U B S C R I B E _ T O K E N \} \} / g, unsubscribeToken ) ,
163+ text : text ?. replace ( / \{ \{ U N S U B S C R I B E _ T O K E N \} \} / g, unsubscribeToken ) ,
164+ }
165+ }
166+
141167async function processEmailData ( options : EmailOptions ) : Promise < ProcessedEmailData > {
142168 const {
143169 to,
@@ -155,23 +181,14 @@ async function processEmailData(options: EmailOptions): Promise<ProcessedEmailDa
155181
156182 let finalHtml = html
157183 let finalText = text
158- const headers : Record < string , string > = { }
184+ let headers : Record < string , string > = { }
159185
160186 if ( includeUnsubscribe && emailType !== 'transactional' ) {
161187 const primaryEmail = Array . isArray ( to ) ? to [ 0 ] : to
162- const unsubscribeToken = generateUnsubscribeToken ( primaryEmail , emailType )
163- const baseUrl = getBaseUrl ( )
164- const unsubscribeUrl = `${ baseUrl } /unsubscribe?token=${ unsubscribeToken } &email=${ encodeURIComponent ( primaryEmail ) } `
165-
166- headers [ 'List-Unsubscribe' ] = `<${ unsubscribeUrl } >`
167- headers [ 'List-Unsubscribe-Post' ] = 'List-Unsubscribe=One-Click'
168-
169- if ( html ) {
170- finalHtml = html . replace ( / \{ \{ U N S U B S C R I B E _ T O K E N \} \} / g, unsubscribeToken )
171- }
172- if ( text ) {
173- finalText = text . replace ( / \{ \{ U N S U B S C R I B E _ T O K E N \} \} / g, unsubscribeToken )
174- }
188+ const unsubData = addUnsubscribeData ( primaryEmail , emailType , html , text )
189+ headers = unsubData . headers
190+ finalHtml = unsubData . html
191+ finalText = unsubData . text
175192 }
176193
177194 return {
@@ -349,21 +366,10 @@ async function sendBatchWithResend(emails: EmailOptions[]): Promise<BatchSendEma
349366
350367 if ( includeUnsubscribe && emailType !== 'transactional' ) {
351368 const primaryEmail = Array . isArray ( email . to ) ? email . to [ 0 ] : email . to
352- const unsubscribeToken = generateUnsubscribeToken ( primaryEmail , emailType )
353- const baseUrl = getBaseUrl ( )
354- const unsubscribeUrl = `${ baseUrl } /unsubscribe?token=${ unsubscribeToken } &email=${ encodeURIComponent ( primaryEmail ) } `
355-
356- emailData . headers = {
357- 'List-Unsubscribe' : `<${ unsubscribeUrl } >` ,
358- 'List-Unsubscribe-Post' : 'List-Unsubscribe=One-Click' ,
359- }
360-
361- if ( email . html ) {
362- emailData . html = email . html . replace ( / \{ \{ U N S U B S C R I B E _ T O K E N \} \} / g, unsubscribeToken )
363- }
364- if ( email . text ) {
365- emailData . text = email . text . replace ( / \{ \{ U N S U B S C R I B E _ T O K E N \} \} / g, unsubscribeToken )
366- }
369+ const unsubData = addUnsubscribeData ( primaryEmail , emailType , email . html , email . text )
370+ emailData . headers = unsubData . headers
371+ if ( unsubData . html ) emailData . html = unsubData . html
372+ if ( unsubData . text ) emailData . text = unsubData . text
367373 }
368374
369375 batchEmails . push ( emailData )
0 commit comments