Created
March 15, 2022 00:10
-
-
Save hadnazzar/e0e4d8377292bc3b32d0ff88c51be66c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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