@@ -711,10 +711,10 @@ export class MailService {
711711 return okAsync ( true )
712712 }
713713
714- const attachments = submissionAttachments
715- if ( pdfAttachment ) {
716- attachments . push ( pdfAttachment )
717- }
714+ const attachmentsToInclude = [
715+ ... submissionAttachments ,
716+ ... ( pdfAttachment ? [ pdfAttachment ] : [ ] ) ,
717+ ]
718718
719719 logger . info ( {
720720 message : 'Sending admin notification mail' ,
@@ -755,13 +755,12 @@ export class MailService {
755755 const adminEmails : string [ ] = getAdminEmails ( form )
756756
757757 return generateSubmissionToAdminHtml ( htmlData ) . andThen ( ( mailHtml ) => {
758-
759758 const mail : MailOptions = {
760759 to : adminEmails ,
761760 from : this . #senderFromString,
762761 subject : `formsg-auto: ${ formTitle } (#${ refNo } )` ,
763762 html : mailHtml ,
764- attachments,
763+ attachments : attachmentsToInclude ,
765764 headers : {
766765 [ EMAIL_HEADERS . formId ] : String ( form . _id ) ,
767766 [ EMAIL_HEADERS . submissionId ] : refNo ,
@@ -836,22 +835,33 @@ export class MailService {
836835 formUrl : `${ this . #appUrl} /${ form . _id } ` ,
837836 }
838837
839- // Create a copy of attachments for attaching of autoreply pdf if needed.
840- const attachmentsWithAutoreplyPdf = [
841- ...submissionAttachments ,
842- ...( pdfAttachment ? [ pdfAttachment ] : [ ] ) ,
843- ]
838+ const getAttachmentsToInclude = ( mailData : AutoReplyMailData ) => {
839+ const shouldPdfAttachmentBeIncluded =
840+ ! isPaymentEnabled && mailData . includeFormSummary
841+
842+ if ( shouldPdfAttachmentBeIncluded && ! pdfAttachment ) {
843+ logger . error ( {
844+ message : 'Could not find PDF attachment required for autoReply email. Continuing to send without PDF attachment.' ,
845+ meta : {
846+ action : 'sendAutoReplyEmails' ,
847+ formId : String ( form . _id ) ,
848+ submissionId : String ( submission . id ) ,
849+ } ,
850+ } )
851+ }
852+
853+ return pdfAttachment && shouldPdfAttachmentBeIncluded
854+ ? [ ...submissionAttachments , pdfAttachment ]
855+ : [ ...submissionAttachments ]
856+ }
844857
845858 // Prepare mail sending for each autoreply mail.
846859 return Promise . allSettled (
847860 autoReplyMailDatas . map ( ( mailData , index ) => {
848861 return this . #sendSingleAutoreplyMail( {
849862 form,
850863 submission,
851- attachments :
852- mailData . includeFormSummary && ! isPaymentEnabled
853- ? attachmentsWithAutoreplyPdf
854- : submissionAttachments ,
864+ attachments : getAttachmentsToInclude ( mailData ) ,
855865 autoReplyMailData : mailData ,
856866 formSummaryRenderData : strippedRenderData ,
857867 index,
0 commit comments