Skip to content

Commit e270554

Browse files
committed
Funcion para enviar correo
1 parent b9f2419 commit e270554

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/mail.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const mailer = require('nodemailer')
2+
const file = require('./file')
3+
4+
const subject = 'Listado de usuarios hasta la fecha ' + new Date().toLocaleDateString()
5+
6+
const html = 'Mensaje...';
7+
8+
const confTransp = {
9+
host: 'smtp.gmail.com',
10+
port: 465,
11+
service: 'gmail',
12+
secure: true,
13+
auth: {
14+
user: process.env.MAIL_FROM,
15+
pass: process.env.MAIL_PASS
16+
}
17+
}
18+
19+
let options = {
20+
from: process.env.MAIL_FROM,
21+
cc: process.env.MAIL_CC,
22+
to: process.env.MAIL_TOSENT,
23+
subject,
24+
html,
25+
attachments: [
26+
{
27+
path: '',
28+
filename: '',
29+
}
30+
]
31+
}
32+
33+
// Envia email
34+
const enviaEmail = async (c,data) => {
35+
36+
const transport = mailer.createTransport(confTransp)
37+
38+
c('Validando datos...')
39+
40+
if(!data.length){
41+
options.attachments = null
42+
options.cc = null
43+
options.to = options.from
44+
options.subject = 'Error en consulta de usuarios'
45+
options.html = `Es probable que la lista esté vacía o error al ejecutar la consulta`
46+
c(`${options.subject}. ${options.html}`)
47+
}else{
48+
// Crea archivo
49+
const { pathFile, nameFile } = await file(c,data)
50+
options.attachments[0].path = pathFile
51+
options.attachments[0].filename = nameFile
52+
}
53+
54+
c('Enviando correo electronico...')
55+
56+
const resp = await transport.sendMail(options,(err,info)=>{
57+
if(err){
58+
c('Error al enviar correo:')
59+
return console.error(err.message)
60+
}
61+
c('Archivo enviado existosamente')
62+
c(info.messageId)
63+
})
64+
65+
return resp
66+
}
67+
68+
module.exports = enviaEmail

0 commit comments

Comments
 (0)