server.route({
path: '/enviar',
method: 'post',
config: {
payload: {
output: 'stream',
parse: true,
allow: 'multipart/form-data'
},
cors: {
origin: ['*'],
additionalHeaders: ['cache-control', 'x-requested-with', 'Accept', 'Authorization', 'Content-Type', 'f-None-Match', 'Accept-language']
}
},
handler: function (request, reply) {
let usuario = '[email protected]';
let senha = 'senha';
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: usuario,
pass: senha
}
});
let data = request.payload;
if (data.file) {
let nome = uuidv4();
//let nome = data.file.hapi.filename;
let path = __dirname + '/uploads/' + nome;
let file = fs.createWriteStream(path);
file.on('error', function (err) {
console.log(err)
});
data.file.pipe(file);
}
let receiver = request.query.destinatario;
let ass = request.query.assunto;
let email = request.query.texto;
let mailOptios = {
from: usuario,
to: receiver,
subject: ass,
text: email,
attachments: [
{
//filename: 'Arquivo de Teste.txt',
path: __dirname + '/uploads/' + data.file.filename
},
]
};
console.log(mailOptios);
transporter.sendMail(mailOptios, function (err, info) {
if (err){
console.log(err)
} else {
console.log('Enviado! ' + info.response);
return reply.response('Enviou')
}
})
}
});
});