编程语言
首页 > 编程语言> > nodejs 发送邮件(阿里云)

nodejs 发送邮件(阿里云)

作者:互联网

 1.下载  模块

       

2.  编辑文件  

   

var nodemailer = require('nodemailer');
var sendEmail = function(emailinfo,callback){
    var nodemailer = require('nodemailer');
    var mail = emailinfo.emailaddress;
    //检测邮箱地址是否为空
    if (!mail) {
        callback("请输入邮箱地址!");
    }else{
        //检测邮箱地址是否符合规范
        if (!mail.match()) {
            callback("邮箱地址不符合规范,请重新输入!");
        }else{
            //邮件发送
            var transporter = nodemailer.createTransport({
                host: 'smtp.mxhichina.com',
                port: 25,
                service: 'smtp.mxhichina.com ',
                auth: {
                    user: 'xxx@xx.com',//你的邮箱账号
                    pass: 'XXXXXX'//你的邮箱密码
                }
            });
            var mailOptions = {
                from: '阿泽 xxxx@xxxx.com', // sender address
                to:mail, // list of receivers
                subject: '测试邮件', // Subject line
                text: 'Nodejs之邮件发送', // plaintext body
                html:"<h2><a href='test.jiucaiyun.cn/jiucaiyunstatic/'  >"+emailinfo.inputperson+"邀请您来报价("+emailinfo.inquirynum+"),请登录采购平台来报价!</a></h2>",
                attachments:[  
              {  
                filename : 'a.jpg',  
                path: 'http://img13.360buyimg.com/n0/jfs/t4642/55/3978388666/72688/c8b2dcf/59080db2N7ae9168e.jpg'  
              }
            ] 
            };

            transporter.sendMail(mailOptions, function(error, info){
                if(!error){
                    callback( "邮件发送成功,请注意查收!");
                }else{
                    console.log(error);
                    callback("邮件发送失败,请稍后重试!");
                }
            });
        }
    }




}

var emailinfo = {"emailaddress":"chengzeqi@163.com","inputperson":"成泽琦(chengzq)","inquirynum":"XJDD201901100001"}
sendEmail(emailinfo,function(result){
  console.log(result)
})

 

3.  在 nodemailer-wellknown 模块下 的services.json  文件最后一行添加  阿里云的SMTP地址配置

    

        

"aliyun":{
        "host": "smtp.mxhichina.com",
        "port": 25,
        "secure": true
    }

4. 发送邮件

  

5.  查看邮件

    

 

   

 

标签:emailinfo,nodejs,nodemailer,callback,阿里,var,com,邮件
来源: https://www.cnblogs.com/czq-0214/p/10364634.html