I am using React with Nodemailer to send emails from a user input form, the user should be able to attach a file (a PDF for example) through the form and the content of the form will be sent as an email using Nodemailer. My issue comes with not knowing how to attach the file to the email. Here is a list and examples of properties that can be used using Nodemailer. What properties can I extract from the object inputted through the file input to the event.target.files
to use to attach to the email, can I get the path of the inputted file for example?
Code:
const [file, setFile] = useState(null);
const handleSubmit = async(e) => {
e.preventDefault();
try {
await axios.post("http://localhost:4000/send_form", { file });
}
catch (error) {
console.log(error);
}
}
return (
<form onSubmit={handleSubmit}>
<input
type="file"
onChange={(e) => setFile(e.target.files[0])}
required/>
<button type="submit">Send</button>
</form>
);
Server:
app.post("/send_form", cors(), async (req, res) => {
let { file } = req.body;
await transport.sendMail({
from: "from@sender.com",
to: "to@receiver.com",
subject: "Subject",
html: `<h1>Hello</h1>`,
attachments: [{
filename: "",
path: ""
}]
})
});