如何发送html页面作为电子邮件在nodejs [英] How to send an html page as email in nodejs
本文介绍了如何发送html页面作为电子邮件在nodejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
我一直在使用这个模块: 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';
//创建可重用的传输方法(打开SMTP连接池)
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'});
//指定加载
的模板模板var template = process.cwd()+'/views/index.jade';
//从文件系统获取模板
fs.readFile(template,'utf8',function(err,file){
if(err){
/ / handle errors
console.log('ERROR!');
return res.send('ERROR!');
}
else {
// compile玉模板进入函数
var compiledTmpl = _jade.compile(file,{filename:template});
//设置上下文以在模板中使用
var context = {title:'Express' };
//将HTML返回为一个应用上下文的字符串;
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!);
});
}
});
};
我可能会将邮件部分移动到自己的模块,但我将所有内容都包含在这里,以便您可以看到这一切都在一起。
I recently started programming my first node.js. I can't find any modules from node that is able to send html page as email. please help, thanks!
解决方案
I have been using this module: https://github.com/andris9/Nodemailer
Updated example(using express and nodemailer) that includes getting index.jade template from the file system and sending it as an email:
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!");
});
}
});
};
I'd probably move the mailer part to its own module but I included everything here so you can see it all together.
这篇关于如何发送html页面作为电子邮件在nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文