@@ -15,6 +15,8 @@ interface SendEmailsOptions {
1515 sleepBetween ?: number ;
1616 /** Only send this many emails (i.e. the first X emails) */
1717 onlySend ?: number ;
18+ /** Send the top {@link onlySend} emails to this email as a test */
19+ testSendTo ?: EmailString ;
1820}
1921
2022const DEFAULT_SLEEP_BETWEEN = 0 ;
@@ -53,6 +55,20 @@ export async function sendEmails(
5355 logger . info ( "Loading merge results..." ) ;
5456 const results = storageBackend . loadMergeResults ( ) ;
5557
58+ if ( options . testSendTo ) {
59+ logger . warn ( "" ) ;
60+ logger . warn ( "=======================" ) ;
61+ logger . warn ( "TEST MODE ACTIVATED." ) ;
62+ logger . warn ( "=======================" ) ;
63+ if ( ! options . onlySend ) {
64+ logger . error ( "You must set onlySend to a number to use test mode." ) ;
65+ throw new Error ( "You must set onlySend to a number to use test mode." ) ;
66+ }
67+ logger . warn ( "" ) ;
68+ logger . warn ( `Will send ${ options . onlySend } emails to ${ options . testSendTo } as a test.` ) ;
69+ logger . warn ( "" ) ;
70+ }
71+
5672 // For each sidecar, send the previews
5773 const pendingEmails : {
5874 to : EmailString [ ] ;
@@ -83,21 +99,26 @@ export async function sendEmails(
8399
84100 // Add to pending emails
85101 pendingEmails . push ( {
86- to : email . to ,
87- subject : email . subject ,
102+ to : options . testSendTo ? [ options . testSendTo ] : email . to ,
103+ subject : options . testSendTo ? "(TEST) " + email . subject : email . subject ,
88104 html,
89105 attachments : attachmentPaths ,
90- cc : email . cc ,
91- bcc : email . bcc ,
106+ cc : options . testSendTo ? [ ] : email . cc ,
107+ bcc : options . testSendTo ? [ ] : email . bcc ,
92108 originalResult : result ,
93109 } ) ;
94110 }
95111
96112 // Print the warning
97113
114+ const emailsNumberDisplay = Math . min (
115+ pendingEmails . length ,
116+ options . onlySend ?? Number . MAX_SAFE_INTEGER ,
117+ ) ;
118+
98119 console . log (
99120 chalk . yellow ( `⚠️ --- WARNING --- ⚠️
100- You are about to send ${ pendingEmails . length } emails.
121+ You are about to send ${ emailsNumberDisplay } emails.
101122This action is IRREVERSIBLE.
102123
103124If the system crashes, restarting will NOT necessarily send already-sent emails again.
@@ -108,8 +129,8 @@ Check that:
1081293. You have tested the system beforehand
1091304. All indications this is a test have been removed
110131
111- You are about to send ${ pendingEmails . length } emails. The esitmated time for this is ${
112- ( ( 3 + ( options . sleepBetween ?? DEFAULT_SLEEP_BETWEEN ) ) * pendingEmails . length ) / 60 / 60
132+ You are about to send ${ emailsNumberDisplay } emails. The esitmated time for this is ${
133+ ( ( 3 + ( options . sleepBetween ?? DEFAULT_SLEEP_BETWEEN ) ) * emailsNumberDisplay ) / 60 / 60
113134 } hours.
114135
115136 If you are happy to proceed, please type "Yes, send emails" below.` ) ,
@@ -123,10 +144,15 @@ You are about to send ${pendingEmails.length} emails. The esitmated time for thi
123144
124145 // Send the emails
125146 logger . info ( "Sending emails..." ) ;
126- const total = pendingEmails . length ;
147+ const total = emailsNumberDisplay ;
127148 let sent = 0 ;
128149 for ( const { to, subject, html, attachments, cc, bcc, originalResult } of pendingEmails ) {
129150 logger . info ( `(${ ++ sent } / ${ total } ) Sending email to ${ to } with subject ${ subject } ...` ) ;
151+ if ( options . testSendTo && to [ 0 ] !== options . testSendTo ) {
152+ throw new Error (
153+ "Test mode is on, but the email is not the test email! This is a bug in the code, crashing to prevent sending emails to the wrong person." ,
154+ ) ;
155+ }
130156 await mailer . sendMail (
131157 fromAddress ,
132158 to ,
0 commit comments