【发布时间】:2017-10-06 19:56:17
【问题描述】:
我正在尝试向使用表单提交的电子邮件 ID 发送动态邮件。
下面是我的 app.js 代码。
//Importing Packages
var express = require('express');
var nodemailer = require('nodemailer');
var bodyParser = require('body-parser');
//Applying Express,Ejs to Node
app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:true}));
//Creating Nodemailer Transport
var transporter = nodemailer.createTransport({
host: 'smtp.zoho.com',
port: 465,
secure: true,
auth: {
user: 'noreply@*****.com',
pass: '******'
}
});
//Root Route Setup
app.get('/', function(req, res){
res.render("landing")
});
//Route to send the mail
app.post('/send', function(req,res){
//Setting up Email settings
var mailOptions = {
from: 'noreply@*****.com',
to : req.body.mail,
subject: 'Verify Your Email',
generateTextFromHtml : true,
html: { path: './tmpl.html'}
};
//Execute this to send the mail
transporter.sendMail(mailOptions, function(error, response){
if(error) {
console.log(error);
} else {
console.log(response);
}
});
res.send("Mail succesfully sent!")
});
//Server &Port Settings
app.listen(3333, function(){
console.log("Server is running...")
});
下面是我的Form页面代码,是一个ejs文件
<form action="/send" method="POST">
<input type="email" name="mail" placeholder="Enter your email">
<input type="text" name="name" placeholder="Enter your name">
<input type="submit">
</form>
下面是我的 html 模板,它被邮寄到使用表单提交的 ID。
<html>
<body>
<h1>Hello World!</h1>
<a href="http://www.google/com">Link</a>
</body>
</html>
如何从表单中读取姓名,然后将其包含在电子邮件中,以便在每封电子邮件中,我可以使用变量向该人发送地址,例如“Hello Mr {{name}}”
我无法弄清楚如何在没有电子邮件阻止的情况下将变量传递给 html 文件,因为我无法在 HTML 文件中使用 Script 标签使用 Javascript,因为几乎所有邮件提供商都在电子邮件中阻止 JS!
有人可以帮我解决这个问题吗?
【问题讨论】:
-
您需要先生成 HTML,然后将其包含在电子邮件对象中。
-
我该怎么做?我需要哪些工具?很抱歉问了一些愚蠢的问题,我是 JS 和 Node 的初学者。 :S
标签: html node.js express ejs nodemailer