You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constexpress=require('express');constnodemailer=require('nodemailer');constapp=express();app.use(express.json());// Configure Nodemailer to use the local sendmail serviceconsttransporter=nodemailer.createTransport({sendmail: true,newline: 'unix',path: '/usr/sbin/sendmail'});app.post('/send-email',(req,res)=>{const{ to, subject, text }=req.body;// Set up email dataconstmailOptions={from: '"No Name" <[email protected]>',// sender address
to,// list of receivers
subject,// Subject line
text,// plain text body};// Send email with the defined transport objecttransporter.sendMail(mailOptions,(error,info)=>{if(error){returnres.status(500).json({error: error.message});}res.status(200).json({message: 'Email sent successfully', info });});});// Start the serverconstPORT=process.env.PORT||3000;app.listen(PORT,()=>{console.log(`Server is running on port ${PORT}`);});
Testing with cURL
curl -X POST http://localhost:3000/send-email -H "Content-Type: application/json" -d '{"to": "[email protected]", "subject": "Hello from Express", "text": "This is a test email sent from the Express.js API using sendmail."}'