Attach file in email application?

Asked

Viewed 215 times

1

I am 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 managed to send simple emails using Nodemailer and I’m listing them in my application’s inbox through Node-imap.

But now I want to take the next step and I want to add attachments in the email(PDF, .txt, images) and I’m using the q-uploader Quasar 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 email attachment.

Follow codes and images...

q-uploader of the quasar, where the :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>

Imagery:

Tela Principal

Tela de resposta

Tela de anexo

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 together with the email??

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.