Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29767

I cannot make a mail system with Nodemailer

$
0
0

I have been creating the system for several hours. I almost put the project into an end, but somehow I cannot receive any emails after filling the form. Does anyone know what is the cause of this problem ?? First, I installed node.js, then nodemon. I have four main files and a folder in the project folder - app.js, package-lock.json, contact.handlebars, package.json and node_module.

the code below is app.js

const express = require("express");
const bodyParser = require("body-parser");
const exphbs = require("express-handlebars");
const path = require("path");
const nodemailer = require("nodemailer");

const app = express();

// view engine setup
app.engine("handlebars", exphbs());
app.set("view engine", "handlebars");

// Static folder
app.use("/public", express.static(path.join(__dirname, "public")));

// Body Parser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.get("/", (req, res) => {
  res.render("contact", { layout: false });
});

app.post("/send", (req, res) => {
  const output = `
        <p>You have a new contact request</p>
        <h3>Contact Details</h3>
        <ul>
            <li>Name: ${req.body.name}</li>
            <li>Company: ${req.body.company}</li>
            <li>Email: ${req.body.email}</li>
            <li>Phone: ${req.body.phone}</li>
        </ul>
        <h3>Message</h3>
        <p>${req.body.message}</p>
    `;
  async function main() {
    // Generate test SMTP service account from ethereal.email
    // Only needed if you don't have a real mail account for testing
    let testAccount = await nodemailer.createTestAccount();

    // create reusable transporter object using the default SMTP transport
    let transporter = nodemailer.createTransport({
      host: "I PUT 1ST SERVERNAME HERE OF HOSTGATOR",
      port: 587,
      secure: false, // true for 465, false for other ports
      auth: {
        user: "MY 1ST EMAIL ACCOUNT", // generated ethereal user
        pass: "********" // generated ethereal password
      },
      tls: {
        rejectUnauthorized: false
      }
    });

    // send mail with defined transport object
    let info = await transporter.sendMail({
      from: '"Nodemailer Contact"<MY 1ST EMAIL ACCOUNT>', // sender address
      to: "MY SECOND EMAIL ACCOUNT", // list of receivers
      subject: "Node Contact Request", // Subject line
      text: "Hello world?", // plain text body
      html: output // html body
    });

    console.log("Message sent: %s", info.messageId);
    console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));

    res.render("contact", { layout: false }, { msg: "Email has been sent" });
  }

  main().catch(console.error);
});

app.listen(3000, () => console.log("server started..."));

Viewing all articles
Browse latest Browse all 29767

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>