Skip to content

Instantly share code, notes, and snippets.

@caeb92
Created April 1, 2020 19:56
Show Gist options
  • Save caeb92/95d6398c1352d5b9313e4c982611da04 to your computer and use it in GitHub Desktop.
Save caeb92/95d6398c1352d5b9313e4c982611da04 to your computer and use it in GitHub Desktop.
Example nodejs typescript : Send emails with Nodemailer - Handlebars
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.card {
border-width: 1px;
border-style: solid;
border-color: #E0E0E0;
border-radius: 3px;
padding: 16px;
max-width: 512px;
margin-left: auto;
margin-right: auto;
}
.title {
color: #202124;
width: 100%;
}
ul {
color: #202124;
}
</style>
</head>
<body>
<div class="card">
<div class="title">
<h2> El usuario {{incident.createdBy}} ha registrado un nuevo evento del tipo {{incident.tag}}</h2>
</div>
<strong>Detalles del evento</strong>
<ul>
<li> <strong>Empresa: </strong> {{incident.company}}</li>
<li> <strong>Motivo del evento: </strong> {{incident.title}}</li>
<li> <strong>Descripción: </strong> {{incident.message}}</li>
<li> <strong>Asignado/Resolutor: </strong> {{incident.assignedTo}}</li>
<li> <strong>Fecha en la que ocurrió el evento: </strong> {{incident.incidentDate}}</li>
<li> <strong>Fecha de creación del evento: </strong> {{incident.date}}</li>
</ul>
<br> <br>
<div style="color: #757575; margin-left: auto; margin-right: auto; text-align: center;">
<small>⚡ Email generado automáticamente, favor no responder ⚡</small> <br>
<small>Hola - 2020</small>
</div>
</div>
</body>
</html>
import nodemailer from 'nodemailer';
import path from 'path';
import { Incident } from '../models/incident.model';
import * as utils from '../utils/utils';
// tslint:disable-next-line: no-var-requires
const hbs = require('nodemailer-express-handlebars');
// Configuración de handlebars
const hbsConfig = {
viewEngine: {
extName: '.hbs',
partialsDir: path.join(__dirname, '../views/'),
layoutsDir: path.join(__dirname, '../views/'),
defaultLayout: ''
},
viewPath: path.join(__dirname, '../views/'),
extName: '.hbs'
};
// Configuración transportador NodeMailer
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: { user: process.env.USER, pass: process.env.PASSWORD }
});
/**
* Envia un correo al administrador y copia a los involucrados en el evento
* @param incident : Incident
*/
async function notifyNewIncident(incident: Incident) {
transporter.use('compile', hbs(hbsConfig));
const ccValues = [];
if (incident.assignedTo !== incident.createdBy) {
ccValues.push(incident.createdBy);
ccValues.push(incident.assignedTo);
} else {
ccValues.push(incident.createdBy);
}
const email = {
from: 'Soporte Neox',
to: process.env.USER,
cc: ccValues,
subject: '👀 Hola ' + incident.company,
template: 'newIncident',
context: { incident }
};
await transporter.sendMail(email).catch(error => {
utils.logger.error(error);
});
}

NPM PACKAGES

eMails

  • nodemailer-express-handlebars
  • nodemailer
  • handlebars
  • @types/nodemailer

Logs

  • winston
  • express-winston
  • @types/express-winston
@zakcodez
Copy link

What's the ../utils/utils file?

@yandreah
Copy link

What's the ../utils/utils file?

It is a logger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment