Home > OS >  NestJS: How to overwrite the body of a mail?
NestJS: How to overwrite the body of a mail?

Time:07-14

I am working with mails in nodemailer in nestjs.

Currently I have a problem that I don't know how to solve, I need to be able to "intercept" the body of the mail I receive from the frontend. What does that mean?

I send emails with attached pdf files if the size of the attachment exceeds a limit of MB what I do is that I divide the array of attachments and send them in multiple emails. That is to say, I have 20 pdf files, but among those 20 files it weighs more than 25 MB, so I divide the emails and send them two by two, all this with the same body of the email

That is to say that in the frontend the user gives the body to the mail something like this:

The result of the requests is attached:

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

So each NamePatient in the front (which is in angular) is a pdf file, so the api is only in charge of managing the size of the attached files and checking if it exceeds a set limit when it already has it, it divides the files and sends them always with the same body.

But I need that, for example, if the matrix divided 3 emails into two and one because it exceeded the size, I want the body of the email to intervene and underline in black the one that goes with the attached file, something like this:

Mail 1/2

• G-22-6-04136 - NamePatient1

• G-22-6-04137 - NamePatient2

• G-22-6-04138 - NamePatient3

Attachment1.NamePatient1

Attachment2.NamePatient2

Mail 2/2

• G-22-6-04136 - NamePatient1

• G-22-6-04137 - NamePatient2

• G-22-6-04138 - NamePatient3

Attachment3.NamePatient3

Currently, what I did was assign a new body that is assigned depending on the attached file, but I still can't intercept the body that comes from the front

Do you think my problem has a solution? Is there any way to do it?

The code::

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);
 
}

The array I split is by newBody = • ${newArr.map(element => element.fileNameC)}

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

CodePudding user response:

This result can be achieved by using the .replace to the original string, to replace the original text with the highlighted text.

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>`);
});

The result would be as follows: G-22-6-04136 - PatientName1 G-22-6-04136 - PatientName3 G-22-6-04136 - PatientName2

Bolding only the matching text

  • Related