|
1 | | -# fastify-mailer |
2 | | -Fastify nodemailer connection plugin |
| 1 | +# fastify-mailer-provider |
| 2 | + |
| 3 | +This module provides a way to send email using `nodemailer`. |
| 4 | + |
| 5 | +## Install |
| 6 | +``` |
| 7 | +npm i fastify-mailer-provider |
| 8 | +``` |
| 9 | + |
| 10 | +## Usage |
| 11 | +Add it to you project with `register` and you are done! |
| 12 | + |
| 13 | +```js |
| 14 | +const fastify = require('fastify')() |
| 15 | + |
| 16 | +fastify.register(require('fastify-mailer-provider'), { |
| 17 | + pool: true, |
| 18 | + host: 'smtp.example.com', |
| 19 | + port: 465, |
| 20 | + secure: true, // use TLS |
| 21 | + auth: { |
| 22 | + user: 'username', |
| 23 | + pass: 'password' |
| 24 | + } |
| 25 | +}) |
| 26 | + |
| 27 | + |
| 28 | +fastify.get('/sendmail/:email', async (req, reply) => { |
| 29 | + const {email} = req.params |
| 30 | + |
| 31 | + await fastify.mailer.sendMail({ |
| 32 | + from: 'sender@example.com', |
| 33 | + to: email, |
| 34 | + subject: 'foo', |
| 35 | + text: 'bar' |
| 36 | + }) |
| 37 | + return reply.status(204).send() |
| 38 | +}) |
| 39 | + |
| 40 | +fastify.listen({ port: 3000 }, err => { |
| 41 | + if (err) throw err |
| 42 | +}) |
| 43 | +``` |
| 44 | + |
| 45 | +You can also set an email provider for identification: |
| 46 | +```js |
| 47 | +const fastify = require('fastify')() |
| 48 | + |
| 49 | +fastify.register(require('fastify-mailer-provider'), { |
| 50 | + provider: 'aws', // using aws to send email |
| 51 | + pool: true, |
| 52 | + host: 'smtp.example.com', |
| 53 | + port: 465, |
| 54 | + secure: true, |
| 55 | + auth: { |
| 56 | + user: 'username', |
| 57 | + pass: 'password' |
| 58 | + } |
| 59 | +}) |
| 60 | + |
| 61 | + |
| 62 | +fastify.get('/sendmail/:email', async (req, reply) => { |
| 63 | + const {email} = req.params |
| 64 | + |
| 65 | + await fastify.mailer.aws.sendMail({ |
| 66 | + from: 'sender@example.com', |
| 67 | + to: email, |
| 68 | + subject: 'foo', |
| 69 | + text: 'bar' |
| 70 | + }) |
| 71 | + return reply.send({ |
| 72 | + provider: fastify.mailer.aws.provider // { provider: 'aws' } |
| 73 | + }) |
| 74 | +}) |
| 75 | + |
| 76 | +fastify.listen({ port: 3000 }, err => { |
| 77 | + if (err) throw err |
| 78 | +}) |
| 79 | +``` |
| 80 | + |
| 81 | +You can also set multiply an email providers: |
| 82 | +```js |
| 83 | +const fastify = require('fastify')() |
| 84 | + |
| 85 | +fastify.register(require('fastify-mailer-provider'), { |
| 86 | + provider: 'aws', |
| 87 | + // ... |
| 88 | +}) |
| 89 | + |
| 90 | +fastify.register(require('fastify-mailer-provider'), { |
| 91 | + provider: 'google', |
| 92 | + // ... |
| 93 | +}) |
| 94 | + |
| 95 | +fastify.register(require('fastify-mailer-provider'), { |
| 96 | + provider: 'sendgrid', |
| 97 | + // ... |
| 98 | +}) |
| 99 | + |
| 100 | +fastify.get('/sendmail/:email', async (req, reply) => { |
| 101 | + const {email} = req.params |
| 102 | + |
| 103 | + await fastify.mailer.aws.sendMail({ /*...*/ }) |
| 104 | + await fastify.mailer.google.sendMail({ /*...*/ }) |
| 105 | + await fastify.mailer.sendgrid.sendMail({ /*...*/ }) |
| 106 | + // ... |
| 107 | +}) |
| 108 | + |
| 109 | +fastify.listen({ port: 3000 }, err => { |
| 110 | + if (err) throw err |
| 111 | +}) |
| 112 | +``` |
| 113 | + |
| 114 | +## Documentation |
| 115 | + |
| 116 | +More details about nodemailer documentation, see [nodemailer docs](https://nodemailer.com/about/). |
| 117 | + |
| 118 | +## License |
| 119 | + |
| 120 | +[MIT License](https://github.com/leandroandrade/fastify-mailer-provider/blob/main/LICENSE/) |
0 commit comments