L
L
Lev Polshkov2021-01-28 16:55:05
Node.js
Lev Polshkov, 2021-01-28 16:55:05

Sending photo (input type="file) from React to Node.js and from there using nodemailer to mail?

And so, I will not show what is happening at my front, everything is ok there. Post a request to the node.js server, the picture comes in the form of a File object (

image: File
lastModified: 1608335833699
lastModifiedDate: Sat Dec 19 2020 01:57:13 GMT+0200 (Восточная Европа, стандартное время) {}
name: "Page 1, object 4-1.jpg"
size: 532480
type: "image/jpeg"
webkitRelativePath: ""

).
Difficulties arise here
This is my server on the node
const nodemailer = require('nodemailer');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');




const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(cors());



app.post('/api/form', (req,res)=> {

     const {name,lastName,message, email, image} = req.body;
   

    let transporter = nodemailer.createTransport({
        service:'gmail',
        auth:{
            user:'#######@gmail.com',
            pass:'#####'
        }
    });


    let mailOptions = {
        from:`${email}`,
        to:'######@gmail.com',
        subject:'test',
        text:`Name ${name} , Last name ${lastName}, message: ${message}`,
        attachments:[
            {
                filename:'wassup.png',
                contentType:  'image/png',
                content: new Buffer.from(image.split("base64,")[1], "base64"),
                
            }
        ]
    
    }

    transporter.sendMail(mailOptions, function(err,data){
        err?console.log(` Error occurs ${err}`):console.log('email sent');
});
    



});

const PORT = 3001;

app.listen(PORT, ()=> {
    console.log(`server is running at port ${PORT}`)
})

Sending just messages works, as I try to send a picture, everything is covered with a copper basin and email does not come at all

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Petr Gavrilov, 2021-01-28
@petter

image.split("base64,")[1], "base64"
It seems that the picture is expected as a base64 string, not File.
You can try to wrap the callback in try/catch and see what will fail with an error

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question