栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Node.js中将HTML页面作为电子邮件发送

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Node.js中将HTML页面作为电子邮件发送

我一直在使用此模块:https :
//github.com/andris9/Nodemailer

更新了示例(使用express和nodemailer),包括从文件系统获取index.jade模板并将其作为电子邮件发送:

var _jade = require('jade');var fs = require('fs');var nodemailer = require("nodemailer");var FROM_ADDRESS = 'foo@bar.com';var TO_ADDRESS = 'test@test.com';// create reusable transport method (opens pool of SMTP connections)var smtpTransport = nodemailer.createTransport("SMTP",{    service: "Gmail",    auth: {        user: "bar@foo.com",        pass: "PASSWORD"    }});var sendMail = function(toAddress, subject, content, next){  var mailOptions = {    from: "SENDERS NAME <" + FROM_ADDRESS + ">",    to: toAddress,    replyTo: fromAddress,    subject: subject,    html: content  };  smtpTransport.sendMail(mailOptions, next);};exports.index = function(req, res){  // res.render('index', { title: 'Express' });  // specify jade template to load  var template = process.cwd() + '/views/index.jade';  // get template from file system  fs.readFile(template, 'utf8', function(err, file){    if(err){      //handle errors      console.log('ERROR!');      return res.send('ERROR!');    }    else {      //compile jade template into function      var compiledTmpl = _jade.compile(file, {filename: template});      // set context to be used in template      var context = {title: 'Express'};      // get html back as a string with the context applied;      var html = compiledTmpl(context);      sendMail(TO_ADDRESS, 'test', html, function(err, response){        if(err){          console.log('ERROR!');          return res.send('ERROR');        }        res.send("Email sent!");      });    }  });};

我可能会将邮件程序部分移至其自己的模块,但是我在此处包括了所有内容,因此您可以一起看到它们。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/387514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号