Thursday, 8 June 2017

HTML form data not being sent in Heroku. ENV variable issue.

so at the risk of getting more down votes, here goes my question (be nice!): Ive spent hours trying to figure out how to get Heroku to accept the ENV Variables I have just added through (heroku config:set PASS_CODE='password').

I have a simple HTML contact form within an Express app, I am using the npm package nodemailer, added ENV variables in an .env file for use with detenv, and calling them using the code below. Heroku does state to call their ENV variables in the same way, however the form data is not sending, and I am still getting the following error in the Heroku logs: { Error: Invalid login: 535 5.0.0 Authentication Failed. Both dotenv and heroku say to call the ENV variable using .process.env. It works perfectly in my local node.js server, but not when I push to heroku.

Here are my code snippets:

require('dotenv').config(); 

var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');

router.get('/', function(req, res, next) {
        res.render('contact');
});

router.post('/send', function(req, res, next){

        // create reusable transporter object using the default SMTP transport
        var transporter = nodemailer.createTransport({
            host: 'smtp-mail.outlook.com',
            port: 587,
            secure: false, // secure:true for port 465, secure:false for port 587
            tls: {
                ciphers: 'SSLv3'
            },
            auth: {
                user: 'example.outlook.com',
                pass: process.env.PASS_CODE
            }
        });

        // setup email data with unicode symbols
        var mailOptions = {
            from: '"Fred Foo 👻" <sdawes@outlook.com>', // sender address
            to: 'example.outlook.com', // list of receivers
            subject: '😍 New message from your website!', // Subject line
            text: '😍 You have a new message from your website with the following details: Name: ' +req.body.first_name + 'Surname: ' + req.body.last_name + 'Email: ' + req.body.email + 'Phone Number: ' + req.body.number + 'Message: ' + req.body.message, // plain text body
            html: '<b>😍 You have a new message from your website with the following details: </b><br><ul><li>First Name: ' + req.body.first_name + '</li><li>Surname: ' + req.body.last_name + '</li><li>Email: ' + req.body.email + '</li><li>Phone Number: ' + req.body.number + '</li><li>Message: ' + req.body.message + '</li></ul>' // html body
        };

        // send mail with defined transport object
        transporter.sendMail(mailOptions, function(error, info) {
            if (error) {
                console.log(error);
                res.redirect('/');
            } else {
                console.log('Message sent: ' + info.response);
                res.redirect('/');
                
            }
            
        });
});

I have also declared the ENV variable in a .env file like so: PASS_CODE=password, and you can see I am requiring dotenv.

It works fine when I run locally through the node.js server, but not when I push through to Heroku. I have set the ENV variables in Heroku and checked by typing heroku config.

For completeness, here is a copy of my package.json file:

{
  "name": "podeg",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "debug": "node --debug ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.17.1",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.3",
    "express": "~4.15.2",
    "jade": "~1.11.0",
    "morgan": "~1.8.1",
    "multer": "^1.3.0",
    "nodemailer": "^4.0.1",
    "serve-favicon": "~2.4.2",
    "dotenv": "^4.0.0"
  },
  "engines": {
    "node": "7.10.0"
  }
}

Many thanks indeed!



via sdawes

No comments:

Post a Comment