2

I am doing email code in NodeJS. I am trying to get configurations from Laravel .env file it's not going on. I am trying like this:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.ionos.com
MAIL_PORT=587
MAIL_USERNAME=my_email
MAIL_PASSWORD=my_password
MAIL_ENCRYPTION=null

And accesing it like this:

let transporter = nodemailer.createTransport({
    host: process.env.MAIL_HOST,
    port: process.env.MAIL_PORT,
    secure: false, // true for 465, false for other ports
    auth: {
        user: process.env.MAIL_USERNAME, // generated ethereal user
        pass: process.env.MAIL_PASSWORD // generated ethereal password
    }
});
4
  • Are you using Laravel Mix ? Commented Dec 10, 2018 at 5:46
  • No. I have a separate folder for all nodejs code. Commented Dec 10, 2018 at 5:47
  • 1
    Did you put the .env file in Nodejs root folder ? Have you given in require('dotenv').config() in the nodejs starter file ? Commented Dec 10, 2018 at 5:48
  • 1
    Possible duplicate of How to access laravel env variables in node js? Commented Dec 10, 2018 at 5:50

1 Answer 1

0

I think you should use dotenv package to import your Laravel .env variables, somthing like

require('dotenv').config({path: '/path/to/laravel/env/file/vars.env'})

then you can use process.env.MAIL_USERNAME or process.env.MAIL_PASSWORD

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.