簡體   English   中英

NestJS:如何覆蓋郵件正文?

[英]NestJS: How to overwrite the body of a mail?

我正在使用nestjs 的nodemailer 中的郵件。

目前我有一個不知道如何解決的問題,我需要能夠“攔截”我從前端收到的郵件正文。 這意味着什么?

如果附件的大小超過 MB 的限制,我會發送帶有附加 pdf 文件的電子郵件,我所做的就是划分附件數組並將它們發送到多封電子郵件中。 也就是說,我有 20 個 pdf 文件,但是在這 20 個文件中,它的重量超過了 25 MB,所以我將電子郵件分開並兩兩發送,所有這些都使用相同的電子郵件正文

也就是說,在前端,用戶將正文提供給郵件,如下所示:

附上請求的結果:

• G-22-6-04136 - NamePatient1 • G-22-6-04137 - NamePatient2 • G-22-6-04138 - NamePatient3

所以前面的每個NamePatient(是角度的)都是一個pdf文件,所以api只負責管理附件的大小並檢查它是否超過了設置的限制,當它已經有它時,它會划分文件並始終以同一個身體發送它們。

但我需要這樣,例如,如果矩陣將 3 封電子郵件分成兩封和一封,因為它超過了大小,我希望電子郵件的正文介入並用黑色下划線與附件一起使用,如下所示:

郵件 1/2

• G-22-6-04136 - NamePatient1

• G-22-6-04137 - NamePatient2

• G-22-6-04138 - NamePatient3

附件1.姓名Patient1

附件2.姓名Patient2

郵件 2/2

• G-22-6-04136 - NamePatient1

• G-22-6-04137 - NamePatient2

• G-22-6-04138 - NamePatient3

附件3.姓名Patient3

目前,我所做的是分配一個根據附件分配的新主體,但我仍然無法攔截來自前面的主體

你認為我的問題有解決方案嗎? 有什么辦法嗎?

編碼::

const totalSize: number = this.getSizeFromAttachments(attachments);
const chunkSplit = Math.floor(isNaN(totalSize) ? 1 : totalSize / this.LIMIT_ATTACHMENTS) + 1; 
const attachmentsChunk: any[][] = _.chunk(attachments, chunkSplit);

if ((totalSize > this.LIMIT_ATTACHMENTS) && attachmentsChunk?.length >= 1) {
   
    const result = attachment.map(element => this.getCantidad.find(y => element.content === y.content))
            const aux = this.namePatient
            const ans = [] 
            result.forEach(ele => {
              const expected_key = ele["correlativo_solicitud"];
              if (aux[expected_key]) {
                const newItem = { ...ele };
                newItem["name_Patient"] = aux[expected_key]
                newItem["fileName"] = `${expected_key}${aux[expected_key] ? ' - ' + aux[expected_key] : null}\n`.replace(/\n/g, '<br />')
                ans.push(newItem)
              }
            });
            
            let newBody: any;
            const resultFilter = attachment.map(element => element.content);

            const newArr = [];
            ans.filter(element => {
              if (resultFilter.includes(element.content)) {
                newArr.push({
                  fileNameC: element.fileName
                })
              }
            })

            newBody = `• ${newArr.map(element => element.fileNameC)}`;

            const date = this.limpiarFechaSolicitud;
            const cantidad = attachmentsChunk?.length;
            const getCurrent = `(${index}/${attachmentsChunk?.length})`
            const header = `Estimado Usuario, la solicitud realizada el ${this.limpiarFechaSolicitud} será enviada en ${attachmentsChunk?.length} correos. Este es el correo (${index}/${attachmentsChunk?.length})`;

            console.log('cuerpo', context.body);
   
           // context.body is the body I receive of frontend like a string

            const newContext = {
              newDate: date,
              cantidad: cantidad,
              getCurrent: getCurrent,
              newBody: newBody,
              ...context
            }
 
            return this.prepareEmail({
              to: to,
              subject: ` ${subject} (Correo ${index}/${attachmentsChunk?.length})`,
              template: template,
              context: newContext,
            }, attachment);
 
}

我拆分的數組是 newBody = • ${newArr.map(element => element.fileNameC)}

newBody = [ fileNameC: 'G-22-6-04136 - PatientName1']
context.body = 'G-22-6-04136 - PatientName1<br> '

這個結果可以通過對原始字符串使用.replace來實現,將原始文本替換為突出顯示的文本。

 let originalBody = `G-22-6-04136 - PatientName1 G-22-6-04136 - PatientName3 G-22-6-04136 - PatientName2`; const newBody = ['G-22-6-04136 - PatientName1', 'G-22-6-04136 - PatientName2']; newBody.forEach((item) => { originalBody = originalBody.replace(item, `<b>${item}</b>`); });

結果如下: G-22-6-04136 - PatientName1 G-22-6-04136 - PatientName3 G-22-6-04136 - PatientName2

僅將匹配的文本加粗

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM