85 lines
2.1 KiB
JavaScript
85 lines
2.1 KiB
JavaScript
/* require('../config/config'); // mantiene tu carga de variables
|
||
const nodemailer = require('nodemailer');
|
||
|
||
const transporter = nodemailer.createTransport({
|
||
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
|
||
});
|
||
|
||
const gmail = (subject, to, text) =>
|
||
transporter.sendMail({
|
||
from: process.env.USER_GMAIL,
|
||
to,
|
||
subject,
|
||
text,
|
||
});
|
||
|
||
module.exports = gmail;
|
||
*/
|
||
|
||
|
||
|
||
|
||
|
||
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;
|
||
|
||
|
||
|