{"id":16400673,"url":"https://github.com/coopflow/fastify-mailer","last_synced_at":"2025-03-21T02:32:59.487Z","repository":{"id":34932008,"uuid":"191574579","full_name":"coopflow/fastify-mailer","owner":"coopflow","description":"Nodemailer instance initialization and encapsulation in fastify framework.","archived":false,"fork":false,"pushed_at":"2023-03-21T06:56:55.000Z","size":94,"stargazers_count":17,"open_issues_count":6,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-12T05:28:21.240Z","etag":null,"topics":["e-mail","email","fastify","fastify-plugin","mailer","nodemailer"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coopflow.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-12T13:17:40.000Z","updated_at":"2024-04-26T06:18:26.000Z","dependencies_parsed_at":"2024-06-18T21:37:04.778Z","dependency_job_id":"8b36b3b6-e68f-4f1d-92cf-91e0792567c7","html_url":"https://github.com/coopflow/fastify-mailer","commit_stats":{"total_commits":92,"total_committers":3,"mean_commits":"30.666666666666668","dds":0.07608695652173914,"last_synced_commit":"171c5ba74e935a257e9259825244ffb2ca6f0ed7"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coopflow%2Ffastify-mailer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coopflow%2Ffastify-mailer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coopflow%2Ffastify-mailer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coopflow%2Ffastify-mailer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coopflow","download_url":"https://codeload.github.com/coopflow/fastify-mailer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221811380,"owners_count":16884305,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["e-mail","email","fastify","fastify-plugin","mailer","nodemailer"],"created_at":"2024-10-11T05:28:22.417Z","updated_at":"2024-10-28T09:12:08.405Z","avatar_url":"https://github.com/coopflow.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# fastify-mailer\n\n[![NPM version](https://img.shields.io/npm/v/fastify-mailer.svg?style=flat)](https://www.npmjs.com/package/fastify-mailer)\n[![GitHub CI](https://github.com/coopflow/fastify-mailer/workflows/GitHub%20CI/badge.svg)](https://github.com/coopflow/fastify-mailer/actions?workflow=GitHub+CI)\n[![Coverage Status](https://coveralls.io/repos/github/coopflow/fastify-mailer/badge.svg?branch=master)](https://coveralls.io/github/coopflow/fastify-mailer?branch=master)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)\n\n[Nodemailer](https://www.nodemailer.com) instance initialization and encapsulation in [fastify](https://www.github.com/fastify/fastify) framework.\n\n## Install\n\nInstall the package with:\n\n```sh\nnpm i fastify-mailer nodemailer --save\n```\n\n## Usage\n\nThe package needs to be added to your project with `register` and you must at least configure your transporter options following [Nodemailer documentation](https://nodemailer.com/usage/) and you are done.\n\n```js\n'use strict'\n\nconst fastify = require('fastify')({ logger: true })\n\nfastify.register(require('fastify-mailer'), {\n  defaults: { from: 'John Doe \u003cjohn.doe@example.tld\u003e' },\n  transport: {\n    host: 'smtp.example.tld',\n    port: 465,\n    secure: true, // use TLS\n    auth: {\n      user: 'john.doe',\n      pass: 'super strong password'\n    }\n  }\n})\n\nfastify.get('/send', (request, reply) =\u003e {\n  const { mailer } = fastify\n\n  mailer.sendMail({\n    to: 'someone@example.tld',\n    subject: 'example',\n    text: 'hello world !'\n  }, (errors, info) =\u003e {\n    if (errors) {\n      fastify.log.error(errors)\n\n      reply.status(500)\n      return {\n        status: 'error',\n        message: 'Something went wrong'\n      }\n    }\n\n    reply.status(200)\n    return {\n      status: 'ok',\n      message: 'Email successfully sent',\n      info: {\n        from: info.from, // John Doe \u003cjohn.doe@example.tld\u003e\n        to: info.to, // ['someone@example.tld']\n      }\n    }\n  })\n})\n\nfastify.listen(3000, (errors) =\u003e {\n  if (errors) {\n    fastify.log.error(errors)\n    process.exit(1)\n  }\n})\n```\n\n## Options\n\n- `defaults`: is an *optional* object that defines default values for mail options.\n\n#### example:\n\n```js\n'use strict'\n\nconst fastify = require('fastify')({ logger: true })\n\nfastify.register(require('fastify-mailer'), {\n  defaults: {\n    // set the default sender email address to jane.doe@example.tld\n    from: 'Jane Doe \u003cjane.doe@example.tld\u003e',\n    // set the default email subject to 'default example'\n    subject: 'default example',\n  },\n  transport: {\n    host: 'smtp.example.tld',\n    port: 465,\n    secure: true, // use TLS\n    auth: {\n      user: 'jane.doe',\n      pass: 'super strong password'\n    }\n  }\n})\n\nfastify.get('/send', (request, reply) =\u003e {\n  const { mailer } = fastify\n\n  mailer.sendMail({\n    to: 'someone@example.tld',\n    text: 'hello world !'\n  }, (errors, info) =\u003e {\n    if (errors) {\n      fastify.log.error(errors)\n\n      reply.status(500)\n      return {\n        status: 'error',\n        message: 'Something went wrong'\n      }\n    }\n\n    reply.status(200)\n    return {\n      status: 'ok',\n      message: 'Email successfully sent',\n      info: {\n        from: info.from, // Jane Doe \u003cjane.doe@example.tld\u003e\n        to: info.to, // ['someone@example.tld']\n      }\n    }\n  })\n})\n\nfastify.listen(3000, (errors) =\u003e {\n  if (errors) {\n    fastify.log.error(errors)\n    process.exit(1)\n  }\n})\n```\n\n- `namespace`: is an *optional* string that lets you define multiple namespaced transporter instances (with different options parameters if you wish) that you can later use in your application.\n\n#### example:\n\n```js\n'use strict'\n\nconst fastify = require('fastify')({ logger: true })\n\nfastify\n  .register(require('fastify-mailer'), {\n    defaults: {\n      // set the default sender email address to jane.doe@example.tld\n      from: 'Jane Doe \u003cjane.doe@example.tld\u003e',\n      // set the default email subject to 'default example'\n      subject: 'default example',\n    },\n    namespace: 'jane',\n    transport: {\n      host: 'smtp.example.tld',\n      port: 465,\n      secure: true, // use TLS\n      auth: {\n        user: 'jane.doe',\n        pass: 'super strong password for jane'\n      }\n    }\n  })\n  .register(require('fastify-mailer'), {\n    defaults: { from: 'John Doe \u003cjohn.doe@example.tld\u003e' },\n    namespace: 'john',\n    transport: {\n      pool: true,\n      host: 'smtp.example.tld',\n      port: 587,\n      secure: false,\n      auth: {\n        user: 'john.doe',\n        pass: 'super strong password for john'\n      }\n    }\n  })\n\nfastify.get('/sendwithjane', (request, reply) =\u003e {\n  const { mailer } = fastify\n\n  mailer.jane.sendMail({\n    to: 'someone@example.tld',\n    text: 'hello world !'\n  }, (errors, info) =\u003e {\n    if (errors) {\n      fastify.log.error(errors)\n\n      reply.status(500)\n      return {\n        status: 'error',\n        message: 'Something went wrong'\n      }\n    }\n\n    reply.status(200)\n    return {\n      status: 'ok',\n      message: 'Email successfully sent',\n      info: {\n        from: info.from, // Jane Doe \u003cjane.doe@example.tld\u003e\n        to: info.to, // ['someone@example.tld']\n      }\n    }\n  })\n})\n\n\nfastify.get('/sendwithjohn', (request, reply) =\u003e {\n  const { mailer } = fastify\n\n  mailer.john.sendMail({\n    to: 'someone@example.tld',\n    subject: 'example with john',\n    text: 'hello world !'\n  }, (errors, info) =\u003e {\n    if (errors) {\n      fastify.log.error(errors)\n\n      reply.status(500)\n      return {\n        status: 'error',\n        message: 'Something went wrong'\n      }\n    }\n\n    reply.status(200)\n    return {\n      status: 'ok',\n      message: 'Email successfully sent',\n      info: {\n        from: info.from, // John Doe \u003cjohn.doe@example.tld\u003e\n        to: info.to, // ['someone@example.tld']\n      }\n    }\n  })\n})\n\nfastify.listen(3000, (errors) =\u003e {\n  if (errors) {\n    fastify.log.error(errors)\n    process.exit(1)\n  }\n})\n```\n\n- `transport`: is a *required* transport configuration object, connection url or a transport plugin instance.\n\n#### example using SES transport:\n\n```js\n'use strict'\n\nconst fastify = require('fastify')({ logger: true })\nconst aws = require('@aws-sdk/client-ses')\n\n/**\n * configure AWS SDK:\n *\n * Use environment variables or Secrets as a Service solutions\n * to store your secrets.\n *\n * NB: do not hardcode your secrets !\n */\nprocess.env.AWS_ACCESS_KEY_ID = 'aws_access_key_id_here'\nprocess.env.AWS_SECRET_ACCESS_KEY = 'aws_secret_access_key_here'\n\nconst ses = new aws.SES({\n  apiVersion: '2010-12-01',\n  region: 'us-east-1'\n})\n\nfastify.register(require('fastify-mailer'), {\n  defaults: { from: 'John Doe \u003cjohn.doe@example.tld\u003e' },\n  transport: {\n    SES: { ses, aws }\n  }\n})\n\nfastify.get('/send', (request, reply) =\u003e {\n  const { mailer } = fastify\n\n  mailer.sendMail({\n    to: 'someone@example.tld',\n    subject: 'example',\n    text: 'hello world !',\n    ses: {\n      // optional extra arguments for SendRawEmail\n      Tags: [\n        {\n          Name: 'foo',\n          Value: 'bar'\n        }\n      ]\n    }\n  }, (errors, info) =\u003e {\n    if (errors) {\n      fastify.log.error(errors)\n\n      reply.status(500)\n      return {\n        status: 'error',\n        message: 'Something went wrong'\n      }\n    }\n\n    reply.status(200)\n    return {\n      status: 'ok',\n      message: 'Email successfully sent',\n      info: {\n        envelope: info.envelope, // {\"from\":\"John Doe \u003cjohn.doe@example.tld\u003e\",\"to\":['someone@example.tld']}\n      }\n    }\n  })\n})\n\nfastify.listen(3000, (errors) =\u003e {\n  if (errors) {\n    fastify.log.error(errors)\n    process.exit(1)\n  }\n})\n```\n\nFor more information on transports you can take a look at [Nodemailer dedicated documentation](https://nodemailer.com/transports/).\n\n## Typescript users\n\nTypes for nodemailer are not officially supported by its author Andris Reinman.\n\nIf you want to use the DefinitelyTyped community maintained types:\n- first you need to install the package with :\n```shell\nnpm install -D @types/nodemailer\n```\n- then you must re-declare the `mailer` interface in the `fastify` module within your own code to add the properties you expect.\n\n#### example :\n\n```ts\nimport { Transporter } from \"nodemailer\";\n\nexport interface FastifyMailerNamedInstance {\n  [namespace: string]: Transporter;\n}\nexport type FastifyMailer = FastifyMailerNamedInstance \u0026 Transporter;\n\ndeclare module \"fastify\" {\n  interface FastifyInstance {\n    mailer: FastifyMailer;\n  }\n}\n```\n\n## Documentation\n\nSee [Nodemailer documentation](https://nodemailer.com/about/).\n\n## Acknowledgements\n\nThis project is kindly sponsored by [coopflow](https://www.coopflow.com).\n\n## License\n\nLicensed under [MIT](https://github.com/coopflow/fastify-mailer/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoopflow%2Ffastify-mailer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoopflow%2Ffastify-mailer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoopflow%2Ffastify-mailer/lists"}