Protzyyy
Protzyyy

Reputation: 120

Nodejs and Nodemailer to receive user ip address?

Hello i want to to echo this command request.connection.remoteAddress In node.js | to echo the user ip address can someone help

i am trying to receive contact emails through nodemailer with this code

const bodyParses = 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');

// Main Folder
app.use('/public', express.static(path.join(__dirname, 'public')));
//Body Parses Middleware
app.use(bodyParses.urlencoded({ extended: false}));
app.use(bodyParses.json());


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

app.post("/send", async (req,res,next) => {
  const output = `
  <p>You have a new contact requiest</p>
  <h3>Detajet</h3>
  <ul>
  <li>Name: ${req.body.name}</li>
  <li>Comapny: ${req.body.company}</li>
  <li>Email: ${req.body.email}</li>
  <li>Phone: ${req.body.phone}</li>
  <li>${res.send(ip+"\n")}</li>
  </ul>
  <h3>Message</h3>
  <p>${req.body.message}</p>
  `;
  let transporter = nodemailer.createTransport({
    host: "smtp.gmail.com",
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
      user: '[email protected]', // generated ethereal user
      pass: 'password' // generated ethereal password
    },
    tls:{
      rejectUnauthorized:false
    }
  });

  // send mail with defined transport object
  let info = await transporter.sendMail({
    from: '"Tipi" <[email protected]>', // sender address
    to: "[email protected]", // list of receivers
    subject: "test", // 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('tjetra', {layout: false});

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

app.listen(3000, () => console.log('Server started...'));

So i am trying when i receive the email to get and the user ip too. I think i should use the code : request.connection.remoteAddress but i do not know how to connect when the user submit the form to send me and the user ip address.

Upvotes: 0

Views: 922

Answers (1)

stacks
stacks

Reputation: 359

You would need to be a little more precise on what you want to do, but I will try to help… If I understood well you want to send the IP within the body of the mail, using nodemailer ?

First here is how to get the ip in node.js. The request element has an ip parameter with express (which you use), so just use:

const ip_address = req.ip;  

Then send it through nodemailer :

let transporter = nodeMailer.createTransport({
host: 'smtp.free.fr',
port: 25,
secure: false
});
let mailOptions = {
from: '"Your subject" <[email protected]>', 
to: '[email protected]',
subject: 'Welcome!',
text: 'Here is your text + ip: '+ ip_address, 
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});

Also know that running this on localhost your ip will always get returned as so : ::1

Upvotes: 1

Related Questions