Configuration settings

When writing code that is sending emails, it makes sense to use different settings for your email services depending on the deployment environment. When developers are working with email code, they should be able to use a local SMTP server, so that they can quickly verify emails that are sent to and from different email accounts, without actually sending out emails. In a testing environment, testers should be able to specify which accounts they wish to use as the sending account, and what SMTP server to use. In a Factory Acceptance Testing (FAT) environment, these email settings may change once more, so that emails from any of the test environments don't affect the FAT environment. The final settings would, of course, be set for a production environment.

Changing settings depending on where the code is deployed is a common problem that is generally solved through a configuration file of some sort. Configuration values are read in from a file on disk, and these are used throughout the system. Different environments use different configuration files, and the system code does not need to be changed simply to change these settings.

In our code samples, there are currently two values that are good candidates for configuration settings. These are the SMTP server connection string, and the from email address that all emails are sent from.

These settings can easily be expressed as an interface, as follows:

export interface ISystemSettings { 
    SmtpServerConnectionString: string; 
    SmtpFromAddress: string; 
} 

Here, the ISystemSettings interface defines the two properties that will need to change when changing environments. The SmtpServerConnectionString property will be used to connect to the SMTP server, and the SmtpFromAddress property will be used to specify the originating address for all emails.

We can now modify our MailService class to use this interface, as follows:

import * as nodemailer from 'nodemailer'; 
import { ISystemSettings } from './ISystemSettings'; 
 
export class MailService { 
    private _transporter: nodemailer.Transporter; 
    private _settings: ISystemSettings; 
    constructor(settings: ISystemSettings) { 
        this._settings = settings; 
        this._transporter = nodemailer.createTransport( 
            this._settings.SmtpServerConnectionString 
        ); 
    } 
 
    sendMail(to: string, subject: string, content: string): 
      Promise<void> { 
        let options: nodemailer.SendMailOptions = { 
            from: this._settings.SmtpFromAddress, 
            to: to, 
            subject: subject, 
            text: content 
        } 
    ... existing code ... 

Here, we have imported the ISystemSettings interface, created a local variable named _settings to hold this information, and have modified our constructor function to accept an instance of an object that implements the ISystemSettings interface.

This ISystemSettings interface is used in two places. Firstly, when we call the nodemailer.createTransport function, we use the SmtpServerConnectionString property. Secondly, when we construct the options object, we use the SmtpFromAddress property.

Using the MailService class now means that we must provide both of these parameters when constructing the object, as follows:

let mailService = new MailService({ 
    SmtpServerConnectionString : 'smtp://localhost:1025', 
    SmtpFromAddress : '[email protected]' 
}); 
 
mailService.sendMail( 
    "[email protected]",  
    "subject",  
    "content").then( (msg) => { 
        console.log(`sendMail result :(${msg})`); 
} ); 

Here, we have constructed an object that conforms to the ISystemSettings interface, that is, it has both an SmtpServerConnectionString property and an SmtpFromAddress property. This object is then passed into the GMailService constructor.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.77.98