Skip to content

Instantly share code, notes, and snippets.

@hadnazzar
Created March 15, 2022 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadnazzar/e0e4d8377292bc3b32d0ff88c51be66c to your computer and use it in GitHub Desktop.
Save hadnazzar/e0e4d8377292bc3b32d0ff88c51be66c to your computer and use it in GitHub Desktop.
import express, { Application , Request, Response} from 'express';
import nodemailer from "nodemailer";
import handlebars from "handlebars";
const transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
secure: false,
requireTLS: true,
auth: {
user: 'email',
pass: 'password',
},
logger: true
});
const app: Application = express();
const port: number = 8000
app.get('/', async (req: Request, res: Response) => {
const filePath = path.join(__dirname, '../emails/template.html');
const source = fs.readFileSync(filePath, 'utf-8').toString();
const template = handlebars.compile(source);
const replacements = {
username: "Darth Vader"
};
const htmlToSend = template(replacements);
const info = await transporter.sendMail({
from: '"Sender Name" <from@example.net>',
to: "email.address@gmail.com",
subject: "Hello from node",
text: "Hello world?",
html: htmlToSend,
headers: { 'x-myheader': 'test header' }
});
console.log("Message sent: %s", info.response);
res.send('Hello world!')
})
app.listen(port, function () {
console.log(`App is listening on port ${port} !`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment