6. User email sent by nodemailer system - API nodejs + Express + MySQL practice at the back end of Blog

Posted by djwiltsh on Sun, 17 Nov 2019 19:45:08 +0100

NodeMailer
Nodemailer, a mail sending component of Nodejs
http://blog.fens.me/nodejs-em...
Node.js uses NodeMailer to send mail
http://www.jianshu.com/p/ee20...
https://github.com/nodemailer...
https://nodemailer.com/about/

In our daily development, we sometimes encounter the need for the system to push e-mail to relevant users (for example: send activation mailbox after registration, etc.), and then we need to use NodeMailer;

First, go to XXX cloud / xxx cloud / xxx cloud email to push [take a-l-i as an example]

Nodemailer Is a Node based mail service module.

It is very simple to use Nodemailer to complete an email function, which only needs three steps:

1 lead in module

2 create transport

3 send mail

/**
 * mailbox service
 * add by wwj
 * 2017-02-15 23:47:16
 */
var Promise = require("bluebird");
var i18n = require('i18n');
var config = require('config-lite'); //To configure
var nodemailer = require('nodemailer'); //mail serve

module.exports = {
    /**
     * Send mail
     */
    sendSystemEmail: function(opts) {
        return new Promise(function(resolve, reject) {
            //Verify that the recipient and title of the message and the content of the message are passed in
            if (!opts.to || !opts.subject || !opts.html) {
                console.log(i18n.__('pleasePassParamsComplete'));
                reject(i18n.__('pleasePassParamsComplete'));
                return;
            }
            //From where?
            opts.from = opts.from || '"Blog system" <' + config.email.service + '>';
            //If it's not for the administrator, then copy it to the administrator
            if(opts.to.indexOf(config.email.admin)<0){
                //CC
                opts.cc = '"Blog system Admin" <'+ config.email.admin +'>';
            }
            var transporter = nodemailer.createTransport({
                pool: true,
                host: 'smtpdm.aliyun.com', //smtp.gmail.com
                port: 465, // 25
                secure: true, // use SSL, [not applicable https can be closed]
                auth: {
                    user: config.email.service,
                    pass: config.email.spassword,
                },
            });
            console.log(opts);
            transporter.sendMail(opts, function(error, info) {
                if (error) {
                    console.log("Failed to send email");
                    console.log(error);
                    reject('error');
                    return;
                }
                if (info) {
                    console.log('Message sent success: ' + JSON.stringify(info));
                }
                resolve('success');
            });
        });
    },
};

Topics: node.js github SSL JSON