Nodejs使用mailer發送郵件

在實際應用中,有時需要服務器有特點的信息時及時的推送消息給你,而讓你有所準備和處理,項目中服務器的個數比較多,所以管理服務器集羣也不是很方便,我就是每臺服務器上出現異常退出的時候都發送郵件到我的郵箱同時並重新啓動Nodejs服務器。

廢話不多說,實例上之:

首先,需要安裝一個發送郵件的模塊:

npm install mailer

然後在代碼中引入模塊:

var email = require("mailer");

最後使用模塊發送郵件:

email.send(
        {
            ssl: true,
            host: "smtp.exmail.qq.com",//發送 smtp.qq.com,接收 pop.qq.com
            domain: "[xxx.xxx.xxx.xxx]",//可以在瀏覽器中輸入 http://ip.qq.com/ 得到
            to: "[email protected]",
            from: "[email protected]",
            subject: "myemail test email",
//            reply_to: "[email protected]",
            body: "Hello! This is a test of the myemail.",
            authentication: "login",
            username: "[email protected]",
            password: "xxx",
            debug: false
        },
        function (err, result) {
            if (err) {
                console.log("the err:" + err);
                response.write(JSON.stringify({
                    "提示信息": "發送失敗",
                    "失敗原因": "數據異常"
                }));
                response.end();
            } else {
                if (result) {
                    console.log("發送成功");
                    response.write(JSON.stringify({
                        "提示信息": "發送成功"
                    }));
                    response.end();
                } else {
                    response.write(JSON.stringify({
                        "提示信息": "發送失敗",
                        "失敗原因": "數據異常"
                    }));
                    response.end();
                }
            }
        }
    );

Nodejs這個模塊簡單易用,希望對你有所幫助。

代碼下載:http://download.csdn.net/detail/qxs965266509/8896463

希望可以幫助到大家,對你有幫助或者覺得值得借鑑的,可以在下方點個贊!謝謝!!!
如有轉載請著名來自http://blog.csdn.net/qxs965266509

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章