Nodemailer在使用EJS模板时发送空白电子邮件



当启动页面上点击提交按钮时,我的服务器中的以下代码成功地发送了一封电子邮件。我正在提取一个来自mongoDB的用户输入的列表。当我对变量数据调用console.log时,它表明一切都在正确的位置。这是我在终端中得到的,后面是代码本身。问题是,除了正文中的电子邮件是空白的之外,其他一切看起来都是正确的。主题在那里,但控制台通过ejs以html打印出来的所有内容都不见了。终端说它在那里,但当我收到电子邮件时,空白。。。

DB CONNECTION SUCCESSFUL
Monday, March 21
Server is Flying
html data ======================> <body>
<h1></h1>
<ul>
<li>Welcome to Fedex Staffing Beta!</li>
<li>Geoffrey Singfred</li>
<li>Elizabeth Holmes</li>
<li>Diana Ross</li>
<li>Frank</li>
</ul>
</body>
[Error: 31772:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:wsdepsopensslopensslsslrecordssl3_record.c:332:
] {
library: 'SSL routines',
function: 'ssl3_get_record',
reason: 'wrong version number',
code: 'ESOCKET',
command: 'CONN'
const express = require("express");
const ejs = require("ejs");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const _ = require("lodash");
const date = require(__dirname + "/date.js");
const nodemailer = require("nodemailer");

// MONGO CONNECTION //
// This is the connection to the database. 27017 is the port mongo chooses by default
mongoose.connect("mongodb+srv://user-admin:password@cluster0.zrsn0.mongodb.net/cluster0?retryWrites=true&w=majority")
console.log("DB CONNECTION SUCCESSFUL")

// EE TABLE //
// This is the employee default schema. Just a name.
const itemsSchema = new mongoose.Schema ({
item: String,
});
const listSchema = new mongoose.Schema({
name: String,
items: [itemsSchema]
});

// This initializes a new schema or what Mongo calls a collection. Each collection
// is essentially the equivalent of a table in SQL
const Item = mongoose.model("Item", itemsSchema);
const List = mongoose.model("List", listSchema);

// Write now the app starts with items by default. These are them. They can be deleted
// easily by checking the box in the app. I'm sure there is a better way to do this.
const item1 = new Item({
item: "Welcome to Fedex Staffing!",
});

// Just a default list I use to call when a new page is created.
const defaultItems = [item1];

////// BEGIN APP //////
// Intitalize date
const day = date.getDate();
// Initialize express
const app = express();

// Initialize ejs
app.set("view engine", "ejs");
app.use(express.static("public"));

// Intialize body parser. 
app.use(bodyParser.urlencoded({
extended: true
}));

// MAIN PAGE //

// A GET call to retrieve all current items in the database, if there are less
// than zero then it adds back the default items listed below
app.get("/", function(req, res) {
Item.find({}, function(err, foundItems){ 
if (foundItems.length === 0){
Item.insertMany(defaultItems, function(err){
if (err) {
console.log(err);
} else {
console.log("Items successfully added.");
}
});
res.redirect("/");
} else {
res.render("list", {listTitle: day,newListItems: foundItems});
};
});
});
// A POST call to add items to the list and the database
app.post("/", function(req, res) {

const itemName = req.body.newItem;    
const listName = req.body.list;      
const item = new Item({
item: itemName
});
if (listName === day){
item.save();
res.redirect("/");
} else {
List.findOne({name: listName}, function(err, foundList){  
foundList.items.push(item);
foundList.save();
res.redirect("/" + listName);
});
};
});

// This delete route for when you check a checkbox, it then deletes the item. 
app.post("/delete", function(req, res){
const checkedItemId = req.body.checkbox;
const listName = req.body.listName;
if (listName === day){
Item.findByIdAndRemove(checkedItemId, function(err){ 
if(!err) {
console.log("Successfully deleted checked item")
res.redirect("/");
};
});
} else {
List.findOneAndUpdate({name: listName}, {$pull: {items: {_id: checkedItemId}}}, function(err, foundList){
if(!err){
res.redirect("/" + listName);
};
});
};
});
app.post("/send", function(req, res){
const title = req.body.customName
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 666,
secure: true, // use SSL
auth: {
user: "***********@hellmail.com",
pass: "*********"
}
});

Item.find({}, function(err, foundItems){
const data = ejs.renderFile(__dirname + "/views/emailtemp.ejs", {newListItems: foundItems, listTitle: title}, function (err, data) {
if (err) {
console.log(err);
} else {
var mainOptions = {
from: '"Jesus"*************@gmail.com',
to: "*********@gmail.com",
subject: 'Hello, world',
html: data
};
console.log("html data ======================>", mainOptions.html);
transporter.sendMail(mainOptions, function (err, info) {
if (err) {
console.log(err);
} else {
console.log('Message sent: ' + info.response);
}
});
}
});
});
});

console.log(day)

app.listen(process.env.PORT || 3000, function() {
console.log("Server is Flying");
});
```

所以我第一次回答了自己的问题。在我的代码中,当我初始化传输程序时,我将安全的握手连接设置为"true"。我读了一些书,发现那里有一些东西与握手不匹配。在没有详细说明的情况下,我将其改为"false",而不是"true",这是该应用程序第一次完美运行。

最新更新