Nodemailer sends blank emails when using EJS templates

Viewed 67

The following code in my server successfully sends an email when a submit button is hit on the splash page. I am pulling a list that is input from the user coming from mongoDB. When I call console.log on the variable data it shows that everything is in it's right place. This is what I get in the terminal, followed by the code itself. The problem is that everything looks correct except the emails are blank in the body. Subject is there but everything that the console is printing out in html through ejs is gone. Terminal says its there but when I receive email, blank...

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:\ws\deps\openssl\openssl\ssl\record\ssl3_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");
});
```
1 Answers

So I answered my own question for the first time. In my code, when I initialize the transporter, I have the handshake connection on secure set to 'true'. I did some reading and found that there is something going on there not matching the handshake. Without going into a ton of detail, I changed it to 'false', instead of 'true' and for the first time, the app works perfectly.

Related