@@ -103,119 +103,36 @@ export async function getDomainIdentity(domain: string, region: string) {
103103 return response ;
104104}
105105
106- export async function sendEmailThroughSes ( {
107- to,
108- from,
109- subject,
110- cc,
111- bcc,
112- text,
113- html,
114- replyTo,
115- region,
116- configurationSetName,
117- unsubUrl,
118- isBulk,
119- } : Partial < EmailContent > & {
120- region : string ;
121- configurationSetName : string ;
122- cc ?: string [ ] ;
123- bcc ?: string [ ] ;
124- replyTo ?: string [ ] ;
125- to ?: string [ ] ;
126- isBulk ?: boolean ;
127- } ) {
128- const sesClient = getSesClient ( region ) ;
129- const command = new SendEmailCommand ( {
130- FromEmailAddress : from ,
131- ReplyToAddresses : replyTo ? replyTo : undefined ,
132- Destination : {
133- ToAddresses : to ,
134- CcAddresses : cc ,
135- BccAddresses : bcc ,
136- } ,
137- Content : {
138- // EmailContent
139- Simple : {
140- // Message
141- Subject : {
142- // Content
143- Data : subject , // required
144- Charset : "UTF-8" ,
145- } ,
146- Body : {
147- // Body
148- Text : text
149- ? {
150- Data : text , // required
151- Charset : "UTF-8" ,
152- }
153- : undefined ,
154- Html : html
155- ? {
156- Data : html , // required
157- Charset : "UTF-8" ,
158- }
159- : undefined ,
160- } ,
161- Headers : [
162- // Spread in any unsubscribe headers if unsubUrl is defined
163- ...( unsubUrl
164- ? [
165- { Name : "List-Unsubscribe" , Value : `<${ unsubUrl } >` } ,
166- {
167- Name : "List-Unsubscribe-Post" ,
168- Value : "List-Unsubscribe=One-Click" ,
169- } ,
170- ]
171- : [ ] ) ,
172- // Spread in the precedence header if present
173- ...( isBulk ? [ { Name : "Precedence" , Value : "bulk" } ] : [ ] ) ,
174- {
175- Name : "X-Entity-Ref-ID" ,
176- Value : nanoid ( ) ,
177- } ,
178- ] ,
179- } ,
180- } ,
181- ConfigurationSetName : configurationSetName ,
182- } ) ;
183-
184- try {
185- const response = await sesClient . send ( command ) ;
186- console . log ( "Email sent! Message ID:" , response . MessageId ) ;
187- return response . MessageId ;
188- } catch ( error ) {
189- console . error ( "Failed to send email" , error ) ;
190- throw error ;
191- }
192- }
193-
194- // Need to improve this. Use some kinda library to do this
195- export async function sendEmailWithAttachments ( {
106+ export async function sendRawEmail ( {
196107 to,
197108 from,
198109 subject,
199110 replyTo,
200111 cc,
201112 bcc,
202113 // eslint-disable-next-line no-unused-vars
203- text,
114+ text, // text is not used directly in raw email but kept for interface consistency
204115 html,
205116 attachments,
206117 region,
207118 configurationSetName,
119+ unsubUrl,
120+ isBulk,
121+ inReplyToMessageId,
208122} : Partial < EmailContent > & {
209123 region : string ;
210124 configurationSetName : string ;
211- attachments : { filename : string ; content : string } [ ] ;
125+ attachments ? : { filename : string ; content : string } [ ] ; // Made attachments optional
212126 cc ?: string [ ] ;
213127 bcc ?: string [ ] ;
214128 replyTo ?: string [ ] ;
215129 to ?: string [ ] ;
130+ unsubUrl ?: string ;
131+ isBulk ?: boolean ;
132+ inReplyToMessageId ?: string ;
216133} ) {
217134 const sesClient = getSesClient ( region ) ;
218- const boundary = " NextPart" ;
135+ const boundary = ` NextPart` ;
219136 let rawEmail = `From: ${ from } \n` ;
220137 rawEmail += `To: ${ Array . isArray ( to ) ? to . join ( ", " ) : to } \n` ;
221138 rawEmail += cc && cc . length ? `Cc: ${ cc . join ( ", " ) } \n` : "" ;
@@ -224,19 +141,37 @@ export async function sendEmailWithAttachments({
224141 replyTo && replyTo . length ? `Reply-To: ${ replyTo . join ( ", " ) } \n` : "" ;
225142 rawEmail += `Subject: ${ subject } \n` ;
226143 rawEmail += `MIME-Version: 1.0\n` ;
144+
145+ // Add headers
146+ if ( unsubUrl ) {
147+ rawEmail += `List-Unsubscribe: <${ unsubUrl } >\n` ;
148+ rawEmail += `List-Unsubscribe-Post: List-Unsubscribe=One-Click\n` ;
149+ }
150+ if ( isBulk ) {
151+ rawEmail += `Precedence: bulk\n` ;
152+ }
153+ if ( inReplyToMessageId ) {
154+ rawEmail += `In-Reply-To: <${ inReplyToMessageId } @email.amazonses.com>\n` ;
155+ rawEmail += `References: <${ inReplyToMessageId } @email.amazonses.com>\n` ;
156+ }
157+ rawEmail += `X-Entity-Ref-ID: ${ nanoid ( ) } \n` ;
158+
227159 rawEmail += `Content-Type: multipart/mixed; boundary="${ boundary } "\n\n` ;
228160 rawEmail += `--${ boundary } \n` ;
229161 rawEmail += `Content-Type: text/html; charset="UTF-8"\n\n` ;
230162 rawEmail += `${ html } \n\n` ;
231- for ( const attachment of attachments ) {
232- const content = attachment . content ; // Convert buffer to base64
233- const mimeType =
234- mime . lookup ( attachment . filename ) || "application/octet-stream" ;
235- rawEmail += `--${ boundary } \n` ;
236- rawEmail += `Content-Type: ${ mimeType } ; name="${ attachment . filename } "\n` ;
237- rawEmail += `Content-Disposition: attachment; filename="${ attachment . filename } "\n` ;
238- rawEmail += `Content-Transfer-Encoding: base64\n\n` ;
239- rawEmail += `${ content } \n\n` ;
163+
164+ if ( attachments && attachments . length > 0 ) {
165+ for ( const attachment of attachments ) {
166+ const content = attachment . content ; // Assumes content is base64
167+ const mimeType =
168+ mime . lookup ( attachment . filename ) || "application/octet-stream" ;
169+ rawEmail += `--${ boundary } \n` ;
170+ rawEmail += `Content-Type: ${ mimeType } ; name="${ attachment . filename } "\n` ;
171+ rawEmail += `Content-Disposition: attachment; filename="${ attachment . filename } "\n` ;
172+ rawEmail += `Content-Transfer-Encoding: base64\n\n` ;
173+ rawEmail += `${ content } \n\n` ;
174+ }
240175 }
241176
242177 rawEmail += `--${ boundary } --` ;
@@ -252,11 +187,13 @@ export async function sendEmailWithAttachments({
252187
253188 try {
254189 const response = await sesClient . send ( command ) ;
255- console . log ( "Email with attachments sent! Message ID:" , response . MessageId ) ;
190+ console . log ( "Email sent! Message ID:" , response . MessageId ) ;
256191 return response . MessageId ;
257192 } catch ( error ) {
258- console . error ( "Failed to send email with attachments" , error ) ;
259- throw new Error ( "Failed to send email with attachments" ) ;
193+ console . error ( "Failed to send email" , error ) ;
194+ // It's better to throw the original error or a new error with more context
195+ // throw new Error("Failed to send email");
196+ throw error ;
260197 }
261198}
262199
0 commit comments