Skip to main content

Mail

Goal

The goal of the mail.js file is to send emails with the given content to the given user. Emails are used in An Scéalaí for verifying new accounts and for resetting passwords.

Current Setup

The mail.js file can be found at AnScealai/api/mail.js. This file is required in the authentication controller1 as well as in the Taidgín chatbot files. Emails are sent over SMTP2 using the mail service provider Sendinblue3 as well as the middleware Node.js module nodemailer4. The SMTP keys for the Sendinblue authentication are stored in a .gitignore5 file at AnScealai/api/sendinblue.json. Contact a member of the team to save a copy of the keys to your own dev file.

Usage

The mail.js file contains one function: sendEmail(). This function is exported6 at the bottom of the file so that it can be accessed elsewhere in the backend code. The file first checks if there are no emails currently stored in the application’s system environment7. If so, then the sendEmail() function sends a true boolean value. Otherwise, it needs to send a new email with the given data.

The code first reads in the Sendinblue API authentication data from the filesystem and then parses it to JSON. This username and password data allows the API to send an email.

The function sendEmail() then executes the following steps below:

  • Assigns the data passed to the function to the variables from, recipients, subject, and message
  • Uses nodemailer to create a transporter for sending the email. The host for the transporter is Sendinblue, and the port is 587. The parsed API username and password are then added for API authentication
  • Uses the transporter to send an email with the variables initialised at the beginning of the function
  • Returns the mail status and logs any errors

Footnotes

  1. Authentication Controller documentation

  2. SMTP definition: https://sendgrid.com/blog/what-is-an-smtp-server/#:~:text=The%20Simple%20Mail%20Transfer%20Protocol,to%20send%20and%20receive%20emails.

  3. Sendinblue: https://developers.sendinblue.com/docs

  4. Nodemailer: https://nodemailer.com/about/

  5. .Git Ignore: https://www.w3schools.com/git/git_ignore.asp?remote=github

  6. See modules in api documentation

  7. Process.env guide: https://codeburst.io/process-env-what-it-is-and-why-when-how-to-use-it-effectively-505d0b2831e7