Email

The Mailer class in "leanpro.mail" provides the function of sending mail.

The Mailer class has the following definition

JavaScript
class Mailer {
    /* Get an instance of the Mailer class */
    public static getMailer(mailConfig: MailServerConfig): Mailer;
    
    /* send email */
    public sendMail(message: MailMessage): Promise<void>;
}

in:

getMailer

JavaScript
public static getMailer(mailConfig: MailServerConfig): Mailer;

Static method, pass in the configuration parameters of the mail server (MailServerConfig), and return the instance of Mailer. This instance can be used to perform subsequent mail sending operations. MailServerConfig has the following definition:

JavaScript
interface MailServerConfig {
    host: string,       //mail server
    port?: number,      //mail server port
    secure: boolean,    //secure connection
    auth: {
        "user": string, //authenticate user
        "pass": string  //password
    }
}

When the port parameter is not passed, the default port 25 is used; when the port parameter is not passed and secure=true, the default port 465 is used.

sendMail

JavaScript
public sendMail(message: MailMessage): Promise<void>;

Pass in mail-related parameters and send mail. Among them, MailMessage should have the following definition:

JavaScript
interface MailMessage {
    from: string,    // sender address
    to: string,      // list of receivers
    subject: string, // Subject line
    text: string,    // plain text body
    html: string     // html body
}
text is the content in text format, and html is the data in html format

Sample

Here is an example of sending an email:

JavaScript
const { Mailer } = require('leanpro.mail');
let mailer = Mailer.getMailer({
    "host": "smtp.domain.com",
    "port": 465,
    "secure": true,
    "auth": {
        "user": "noreply@domain.com",
        "pass": "<mypassword>"
    }
});

(async function () {
    let mailMessage = {
        "from": "noreply@domain.com",
        "to": "someone@domain.com",
        "subject": "test mail",
        "text": 'some test content',
        "html": '<p>some <span style="font-weight:bold">test</span> content</p>',
    };
    await mailer.sendMail(mailMessage);
})();

The use needs to modify the above parameters according to the actual SMTP server information.

results matching ""

    No results matching ""