Skip to content

Instantly share code, notes, and snippets.

@dorancemc
Last active November 22, 2024 21:26
Show Gist options
  • Save dorancemc/57e79394fef16c6ef5a04cc639c9188a to your computer and use it in GitHub Desktop.
Save dorancemc/57e79394fef16c6ef5a04cc639c9188a to your computer and use it in GitHub Desktop.
Envio de correos de prueba
MAIL_FROM=
MAIL_TO=
MAIL_SERVER=
MAIL_PORT=25
MAIL_SUBJECT=Correo de prueba
MAIL_TEXT=Este es un correo de prueba
#!/bin/bash
mkdir sendTestEmail
cd sendTestEmail
wget https://gist.githubusercontent.com/dorancemc/57e79394fef16c6ef5a04cc639c9188a/raw/81e568980dda4a64116b016f49189fcee97331c9/.env
wget https://gist.githubusercontent.com/dorancemc/57e79394fef16c6ef5a04cc639c9188a/raw/81e568980dda4a64116b016f49189fcee97331c9/senMail.js
npm install nodemailer dotenv
require('dotenv').config();
const nodemailer = require('nodemailer');
async function sendTestEmail() {
const {
MAIL_FROM, MAIL_TO, MAIL_SERVER, MAIL_PORT,
MAIL_SUBJECT, MAIL_TEXT
} = process.env;
if (!MAIL_FROM || !MAIL_TO || !MAIL_SERVER || !MAIL_PORT) {
console.error('Faltan variables de configuración en el archivo .env');
process.exit(1);
}
const currentDate = new Date();
const formattedDate = currentDate.toISOString().replace('T', ' ').slice(0, 19);
const subjectWithDate = `${MAIL_SUBJECT} ${formattedDate}`;
const transporter = nodemailer.createTransport({
host: MAIL_SERVER,
port: parseInt(MAIL_PORT, 10),
secure: false,
tls: {
rejectUnauthorized: false,
},
});
try {
const info = await transporter.sendMail({
from: MAIL_FROM,
to: MAIL_TO,
subject: subjectWithDate,
text: MAIL_TEXT,
});
console.log('Correo enviado con éxito:', info.messageId);
} catch (error) {
console.error('Error al enviar el correo:', error);
}
}
sendTestEmail();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment