email_server/s2.js
2025-12-07 16:57:53 +01:00

51 lines
1.4 KiB
JavaScript

require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const app = express();
app.use(bodyParser.json());
// Configura il trasporto SMTP con variabili d'ambiente
const transporter = nodemailer.createTransport({
host: process.env.IONOS_HOST,
port: process.env.IONOS_PORT,
secure: false, // true solo se usi 465
auth: {
user: process.env.IONOS_USER,
pass: process.env.IONOS_PASS
},
tls: {
rejectUnauthorized: false
}
});
// Endpoint per inviare email con allegati
app.post('/send-email', async (req, res) => {
const { to, subject, text, html, attachments } = req.body;
if (!to || !subject || (!text && !html)) {
return res.status(400).json({ error: 'Parametri mancanti: to, subject, text/html' });
}
try {
const info = await transporter.sendMail({
from: process.env.IONOS_USER,
to,
subject,
text,
html,
attachments // array di oggetti { filename, path } o { filename, content }
});
res.json({ message: 'Email inviata con successo!', info });
} catch (error) {
console.error('Errore invio email:', error);
res.status(500).json({ error: 'Errore durante l\'invio dell\'email', details: error.message });
}
});
app.listen(process.env.LOCAL_PORT, () => {
console.log(`Server attivo su http://${process.env.LOCAL_HOST}:${process.env.LOCAL_PORT}`);
});