I'm developing an email application and the front end is being done in VueJS / Quasar Framework and the server is being developed with HapiJS.
I've already been able to send simple emails using NodeMailer , and I'm listing them in the inbox of my application via node-imap .
But now I want to take the next step and I want to add attachments to the email (PDF, .txt, images) and I'm using Quasar q-uploader
for this, but I was only able to upload directly to a directory inside the server and I wanted to send the file as an attachment to the email.
Follow codes and images ...
q-uploader
of quasar where :url
is set to send the files to the directory on the server
<q-uploader slot="header" :url="url.url" color="light-blue-10" >Escolher Anexo</q-uploader>
Images:
Server Code:
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')
}
})
}
});
How to upload the attachment file along with the email ??