Skip to content

Latest commit

 

History

History

sending-emails

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

sending-emails

A command-line application built with hapi pal that sends templated emails using nodemailer, vision, and schmervice.

Usage

Before sending any emails, you need some test account credentials with ethereal.email. This app exposes a CLI command to generate these credentials for you and another command to send a templated "welcome" email (see lib/expose/commands.js). Simply follow the directions below to get started!

$ npm install
$ cp server/.env-keep server/.env
$ npx hpal run sending-emails:create-test-account
# Place test account credentials in server/.env
$ npx hpal run sending-emails:welcome Paldo paldo@hapipal.com
# Go to https://ethereal.email, login with your test account credentials,
# and see that your templated welcome email was sent to Paldo.

How it works

This project was scaffolded using the pal boilerplate and the hpal CLI, so everything in lib/ constitutes a hapi plugin and server/ contains the code to configure and deploy that plugin as a hapi server.

One curiosity of this application is that it actually does not have any routes/endpoints! Instead, we opted to expose a command-line interface that can be invoked using hpal, as you may have noticed at the top of Usage section. Because all the code for sending emails is placed inside an email service, it is straightforward to share code for sending emails across route handlers, the REPL (hpal run debug:repl), or a CLI command as we do in this project. That is made possible by the schmervice plugin, which endows your hapi application with a "service layer."

We have a single EmailService in this project, which is a class that configures nodemailer and integrates it with hapi's plugin for template rendering, vision. In this way, we can send handlebars-templated HTML emails (with a plaintext fallback) in a consistent way as long as you have access to your hapi server or a request object. The service is configured via plugin options originating in server/manifest.js, and can work without any implementation changes to support Amazon's SES, Mandrill, sendmail, and other email transports. By default, we send mail over SMTP using a free email mocking service called Ethereal, which does not deliver mail, but instead captures it into a faux inbox for testing purposes. The project comes with a command hpal run sending-emails:create-test-account to generate Ethereal test credentials for you.

Finally, all of this is wired together using pal's file-based plugin composer, haute-couture, and the CLI commands exposed in lib/expose/commands.js for generating credentials and sending emails can be invoked using hpal run. We hope you find it to all be very snazzy, and are open to ideas for improvement.