diff --git a/src/mail/mail.controller.ts b/src/mail/mail.controller.ts index 54aeee6..f7e7c11 100644 --- a/src/mail/mail.controller.ts +++ b/src/mail/mail.controller.ts @@ -31,7 +31,7 @@ export class MailController { @ApiSendMail() async sendMail( @Headers() headers:{password:string}, - @Body() body: { to: string; subject: string; text: string; fecha_recibido: Date }) { + @Body() body: { to: string; subject: string; text: string; fecha_recibido: Date, html:string, adjuntos:any }) { /* @@ -49,10 +49,15 @@ export class MailController { throw new UnauthorizedException('Password incorrecto'); } */ - - const { to, subject, text, fecha_recibido } = body; + + + console.log("estos son los archivos adjuntos en el controller",body); - const result = await this.mailService.sendMail(to, subject, text, fecha_recibido,1); // sistem + + + const { to, subject, text, html, fecha_recibido, adjuntos } = body; + + const result = await this.mailService.sendMail(to, subject, text,html, fecha_recibido,1,adjuntos); // sistem return result; @@ -84,7 +89,7 @@ export class MailController { for (const email of await emails) { try { - await this.mailService.sendMail(email, "subject", "text", fechaActual,sistem); + await this.mailService.sendMail(email, "subject", "text","html", fechaActual,sistem); console.log(`📧 Enviado a: ${email}`); } catch (error) { console.error(`❌ Error enviando a ${email}:`, error.message); diff --git a/src/mail/mail.service.ts b/src/mail/mail.service.ts index 9f7f6f0..1775f43 100644 --- a/src/mail/mail.service.ts +++ b/src/mail/mail.service.ts @@ -42,14 +42,45 @@ export class MailService { // Función para generar el reporte de correos enviados y fallidos - async sendMail(to: string, subject: string, text: string, fecha_recibido: Date, sistema) { + async sendMail(to: string, subject: string, text: string, html: string, fecha_recibido: Date, sistema, adjuntos?: any[] ) { + + console.log("estos son los archivos adjuntos en el service",adjuntos); + + + const mailOptions = { from: process.env.USER_GMAIL, to, subject, text, + html, + + // OJO: nodemailer usa 'attachments', no 'adjuntos' + attachments: (adjuntos || []).map((adj) => { + // Asegúrate de no incluir 'data:image/png;base64,' en 'content' + let base64Clean = adj.content; + // Si viene con prefijo "data:image...", lo quitamos + if (base64Clean.startsWith('data:image/')) { + base64Clean = base64Clean.split('base64,')[1]; + } + + return { + filename: adj.filename || 'qr.png', + content: Buffer.from(base64Clean, adj.encoding || 'base64'), + cid: adj.cid || 'qrCode', // el mismo que uses en + contentType: 'image/png' // recomendable para que sepa que es PNG + }; + }) + + }; + + console.log(mailOptions); + + + + let resMail = await this.transporter.sendMail(mailOptions); const statusTexto = resMail.accepted.length > 0 ? "Enviado" : "Fallido";