0

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: 'user@gmail.com', // generated ethereal user
      pass: 'password' // generated ethereal password
    },
    tls:{
      rejectUnauthorized:false
    }
  });

  // send mail with defined transport object
  let info = await transporter.sendMail({
    from: '"Tipi" <aj.production15@gmail.com>', // sender address
    to: "test@gmail.com", // 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.

MoekUgD
  • 83
  • 9
  • 1
    That `iptoy` function makes no sense. You're not even using it. And it sends back a response to the client, instead of putting the ip into the email (which is what you presumably want?). – Bergi Apr 15 '20 at 15:19
  • I added that by mistake testing to fix the ip problem – MoekUgD Apr 15 '20 at 15:40
  • Does this answer your question? [Express.js: how to get remote client address](https://stackoverflow.com/questions/10849687/express-js-how-to-get-remote-client-address) – Lawrence Cherone Apr 15 '20 at 15:52

1 Answers1

1

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" <no-reply@yourhost.com>', 
to: 'emailReceiver@receiver.com',
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

stacks
  • 339
  • 2
  • 4
  • 13
  • hey stacks,Where do i put ```const ip_address = req.ip;``` in my code – MoekUgD Apr 15 '20 at 15:43
  • @MoekUgD declare this variable just before sending your mail, in your `app.post('/send')` before declaring `const output` and if it solved your problem please don't forget to mark this as good answer or upvote it. – stacks Apr 15 '20 at 15:45
  • Hello,when i recieve the email it says the ip: ::1 – MoekUgD Apr 15 '20 at 15:50
  • This is normal, you are running it on localhost and it will always return ::1. When deploying your app, `req.ip` will return a real IP address. – stacks Apr 15 '20 at 15:51
  • hey stacks,Im using firebase when i upload it asks me to upload the files in the public folder i do that but when i go to the website/firebase it shows not found/when i do website.com/views/contact.handlebars it shows but it does not send the email – MoekUgD Apr 15 '20 at 16:30
  • @MoekUgD this is another issue. I got your issue fixed here on this topic. So please mark my answer as solving this issue you had, and then we can discuss other issues on other topics or I can also invite you in a private chatroom. – stacks Apr 15 '20 at 16:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211734/discussion-between-moekugd-and-stacks). – MoekUgD Apr 15 '20 at 17:01