2025-05-06 14:24:23 -06:00
|
|
|
|
/* require('../config/config'); // mantiene tu carga de variables
|
2025-04-29 17:14:52 -06:00
|
|
|
|
const nodemailer = require('nodemailer');
|
|
|
|
|
|
|
|
|
|
|
|
const transporter = nodemailer.createTransport({
|
2025-05-05 13:38:23 -06:00
|
|
|
|
host: 'smtp.gmail.com', // o smtp‑relay.gmail.com (ver punto 2)
|
|
|
|
|
|
port: 587,
|
|
|
|
|
|
secure: false,
|
|
|
|
|
|
requireTLS: true, // TLS STARTTLS
|
|
|
|
|
|
auth: { user: process.env.USER_GMAIL, pass: process.env.PASS_GMAIL },
|
|
|
|
|
|
pool: true, // <‑‑ activa reuse
|
|
|
|
|
|
maxConnections: 1, // una sola conexión viva
|
|
|
|
|
|
maxMessages: 100, // reabrir después de 100 envíos
|
|
|
|
|
|
rateDelta: 2000, // ventana 1 s
|
|
|
|
|
|
rateLimit: 1 // máx. 5 mensajes/seg
|
2025-04-29 17:14:52 -06:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const gmail = (subject, to, text) =>
|
|
|
|
|
|
transporter.sendMail({
|
|
|
|
|
|
from: process.env.USER_GMAIL,
|
|
|
|
|
|
to,
|
|
|
|
|
|
subject,
|
|
|
|
|
|
text,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = gmail;
|
2025-05-06 14:24:23 -06:00
|
|
|
|
*/
|
2025-05-06 14:03:56 -06:00
|
|
|
|
|
2025-05-06 11:13:08 -06:00
|
|
|
|
|
|
|
|
|
|
|
2025-05-06 14:24:23 -06:00
|
|
|
|
|
2025-05-06 11:13:08 -06:00
|
|
|
|
|
|
|
|
|
|
require('../config/config');
|
|
|
|
|
|
const nodemailer = require('nodemailer');
|
|
|
|
|
|
const { google } = require('googleapis');
|
|
|
|
|
|
|
|
|
|
|
|
const oAuth2Client = new google.auth.OAuth2(
|
|
|
|
|
|
process.env.CLIENT_ID_GMAIL,
|
|
|
|
|
|
process.env.CLIENT_SECRET_GMAIL,
|
|
|
|
|
|
process.env.REDIRECT_URI_GMAIL // puedes usar "https://developers.google.com/oauthplayground"
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
oAuth2Client.setCredentials({
|
|
|
|
|
|
refresh_token: process.env.REFRESH_TOKEN_GMAIL,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
async function sendMail(subject, to, text) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const accessToken = await oAuth2Client.getAccessToken();
|
|
|
|
|
|
|
|
|
|
|
|
const transporter = nodemailer.createTransport({
|
|
|
|
|
|
service: 'gmail',
|
|
|
|
|
|
auth: {
|
|
|
|
|
|
type: 'OAuth2',
|
|
|
|
|
|
user: process.env.USER_GMAIL_GMAIL,
|
|
|
|
|
|
clientId: process.env.CLIENT_ID_GMAIL,
|
|
|
|
|
|
clientSecret: process.env.CLIENT_SECRET_GMAIL,
|
|
|
|
|
|
refreshToken: process.env.REFRESH_TOKEN_GMAIL,
|
|
|
|
|
|
accessToken: accessToken.token,
|
|
|
|
|
|
},
|
|
|
|
|
|
pool: true,
|
|
|
|
|
|
maxConnections: 1,
|
|
|
|
|
|
maxMessages: 100,
|
|
|
|
|
|
rateDelta: 2000,
|
|
|
|
|
|
rateLimit: 1,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const result = await transporter.sendMail({
|
|
|
|
|
|
from: process.env.USER_GMAIL_GMAIL,
|
|
|
|
|
|
to,
|
|
|
|
|
|
subject,
|
|
|
|
|
|
text,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('Error al enviar correo:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = sendMail;
|
|
|
|
|
|
|
2025-05-06 14:03:56 -06:00
|
|
|
|
|
|
|
|
|
|
|