@essential-projects/email

Service to send mails

Usage no npm install needed!

<script type="module">
  import essentialProjectsEmail from 'https://cdn.skypack.dev/@essential-projects/email';
</script>

README

Email module

Configuration

  1. Create the Files config/ENVIRONMENT/email/email_service.json and config/ENVIRONMENT/email/email_repository.json in your project
  2. Set the content of these config-files to {}

Setup

  1. In your http-extension-module in your application run npm install @5minds/email --save
  2. In your http-extensions ioc_module.js
  3. Require the module with const email = require('@5minds/email/ioc_module');
  4. Register the module at the IoC-Container with: email.registerInContainer(container);

Usage

  • Let the IoC-Container inject the EmailService into the services that need to send emailService

Methods

  • sendSMTPMail(smtpSetup, mail)
  • sendSMTPMails(smtpSetup, mails)
  • sendSendgridMail(sendgridAPIKay, mail)
  • sendSendgridMails(sendgridAPIKay, mails)

Parameter-descriptions:

smtpSetup

{ 
  host: 'smtp.gmail.com', 
  port: 465, 
  username: 'heiko.mathes@5minds.de', 
  password: '*******************', 
}

SendgridAPIKey

'*******************'

mail

text, html and template are all optional, but at least one of them has to exist

{
  senderName: 'Heiko Mathes',
  senderAddress: 'heiko.mathes@gmail.com', 
  to: ['heiko.mathes@gmail.com'], 
  subject: 'testMail',
  text: 'PlainText',
  html: '<b>styledtext</b>'
  template: template-object
  attachments: attachments-object
}, 

template-object

the template-object can have one ot the following forms

{ 
  path: '/path/to/a/handlebars/file.hbs', 
  data: { 
    testText: 'HAI!',
  }
}

or

{ 
  source: '<html><body>testText: <b>{{testText}}</b></body></html>', 
  data: { 
    testText: 'HAI!',
  }
}

or

{ 
  handlebarsTemplate: a_compiled_handlebars_template_object, 
  data: { 
    testText: 'HAI!',
  }
}

attachments-object

See Nodemailer - Using attachments

Example

this.emailService.sendSMTPMail({ 
  host: 'smtp.gmail.com', 
  port: 465, 
  username: 'heiko.mathes@5minds.de', 
  password: '************************', 
}, { 
  senderName: 'Heiko Mathes', 
  to: ['heiko.mathes@gmail.com'], 
  subject: 'testMail', 
  template: { 
    path: './testTemplate.hbs', 
    data: { 
      testText: 'HAI!', 
    }, 
  }, 
}); 

Config

Service-Config

{
}

Repository-Config

{
  "overwriteReceiver": "some@mail.address", // optional, overwrites the direct receiver of all mails
  "smtp": { // optional, default smtp-settings
    "host": "smtp.gmail.com",
    "port": 465,
    "username": "heiko.mathes@5minds.de",
    "password": "*******************"
  },
  "sendgrid": { // optional, default sendgrid-settings
    "APIKey": "*******************"
  }
}