var nodemailer = require('nodemailer');
class MailClient{
    constructor(){
      this.mailer=nodemailer.createTransport({
          host:"smtp.mxhichina.com",
          service: 'aliyun',
          secureConnection: true,
          port: 465,
          auth: {
              user: 'czhd_ip@gongsibao.com',
              pass: 'HANtang2018'
          }
      });
    }
    //嵌入图片
    // var mailOptions = {
    //     from: 'bsspirit ',
    //     to: 'xxxxx@163.com',
    //     subject: 'Embedded Image',
    //     html: '<b>Hello world ✔</b><br/>Embedded image: <img src="cid:00000001"/>',
    //     attachments: [{
    //         filename: '01.png',
    //         path: './img/r-book1.png',
    //         cid: '00000001'
    //     }]
    // }
    //发送邮件
    // var mailOptions = {
    // from: 'bsspirit ',
    // to: 'xxxxx@163.com',
    // subject: 'Hello ✔',
    // text: 'Hello world ✔',
    // html: '<b>Hello world ✔</b>' // html body
    // attachments: [
    //     {
    //         filename: 'text0.txt',
    //         content: 'hello world!'
    //     },
    //     {
    //         filename: 'text1.txt',
    //         path: './attach/text1.txt'
    //     }
    // ]
    // };
    async sendMsg(to,title,text,html,cc,bcc,atts){
      var options={
        from: "czhd_ip@gongsibao.com", // sender address
        to: to, // list of receivers
        cc: cc||'',
        bcc: bcc||'',
        subject: title, // Subject line
        text: text || '', // plaintext body
        html: html || '',// html body
        attachments: atts||[
        ]
      };
      var self=this;
      var p=new Promise(function(resv,rej){
        self.mailer.sendMail(options, function(error, info){
            if(error){
                return rej(error);
            }else{
                return resv(info.response);
            }
        });
      });
      return p;
    }
}
module.exports=MailClient;
// var d=new MailClient();
// d.sendMsg("zhuangbing@gongsibao.com","test","see","hello txt",null,null,[
//       {
//           filename: 'text1.jpg',
//           path: 'https://gsb-zc.oss-cn-beijing.aliyuncs.com/zc_3369154019592833720182216128337mmexport1540195729827.jpg'
//       }
// ]).then(r=>{
//   console.log(r);
// }).catch(e=>{
//   console.log(e);
// });
