fastify-mailer-provider
v0.2.0
Published
This module provides a way to send email using nodemailer.
Downloads
73
Readme
fastify-mailer-provider
This module provides a way to send email using nodemailer
.
Install
npm i fastify-mailer-provider
Usage
Add it to you project with register
and you are done!
const fastify = require('fastify')()
fastify.register(require('fastify-mailer-provider'), {
pool: true,
host: 'smtp.example.com',
port: 465,
secure: true, // use TLS
auth: {
user: 'username',
pass: 'password'
}
})
fastify.get('/sendmail/:email', async (req, reply) => {
const {email} = req.params
await fastify.mailer.sendMail({
from: '[email protected]',
to: email,
subject: 'foo',
text: 'bar'
})
return reply.status(204).send()
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})
You can also set an email provider for identification:
const fastify = require('fastify')()
fastify.register(require('fastify-mailer-provider'), {
provider: 'aws', // using aws to send email
pool: true,
host: 'smtp.example.com',
port: 465,
secure: true,
auth: {
user: 'username',
pass: 'password'
}
})
fastify.get('/sendmail/:email', async (req, reply) => {
const {email} = req.params
await fastify.mailer.aws.sendMail({
from: '[email protected]',
to: email,
subject: 'foo',
text: 'bar'
})
return reply.send({
provider: fastify.mailer.aws.provider // { provider: 'aws' }
})
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})
You can also set a from with an email provider:
const fastify = require('fastify')()
fastify.register(require('fastify-mailer-provider'), {
provider: 'aws', // using aws to send email
from: '[email protected]',
pool: true,
host: 'smtp.example.com',
port: 465,
secure: true,
auth: {
user: 'username',
pass: 'password'
}
})
fastify.get('/sendmail/:email', async (req, reply) => {
const {email} = req.params
await fastify.mailer.aws.sendMail({
from: fastify.mailer.aws.from,
to: email,
subject: 'foo',
text: 'bar'
})
return reply.send({
provider: fastify.mailer.aws.provider // { provider: 'aws' }
})
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})
You can also set a from with a default provider:
const fastify = require('fastify')()
fastify.register(require('fastify-mailer-provider'), {
from: '[email protected]',
pool: true,
host: 'smtp.example.com',
port: 465,
secure: true,
auth: {
user: 'username',
pass: 'password'
}
})
fastify.get('/sendmail/:email', async (req, reply) => {
const {email} = req.params
await fastify.mailer.aws.sendMail({
from: fastify.mailer.from,
to: email,
subject: 'foo',
text: 'bar'
})
return reply.send({
provider: fastify.mailer.aws.provider // { provider: 'aws' }
})
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})
You can also set multiply an email providers:
const fastify = require('fastify')()
fastify.register(require('fastify-mailer-provider'), {
provider: 'aws',
// ...
})
fastify.register(require('fastify-mailer-provider'), {
provider: 'google',
// ...
})
fastify.register(require('fastify-mailer-provider'), {
provider: 'sendgrid',
// ...
})
fastify.get('/sendmail/:email', async (req, reply) => {
const {email} = req.params
await fastify.mailer.aws.sendMail({ /*...*/ })
await fastify.mailer.google.sendMail({ /*...*/ })
await fastify.mailer.sendgrid.sendMail({ /*...*/ })
// ...
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})
Documentation
More details about nodemailer documentation, see nodemailer docs.