So I'm trying to send myself my IP address through node.js and so far have come up empty handed. So far my code looks like this:
var exec = require("child_process").exec;
var ipAddress = exec("ifconfig | grep -m 1 inet", function (error, stdout, stderr) {
ipAddress = stdout;
});
var email = require('nodemailer');
email.SMTP = {
host: 'smtp.gmail.com',
port: 465,
ssl: true,
user_authentication: true,
user: 'sendingemail@gmail.com',
pass: 'mypass'
}
email.send_mail({
sender: 'sendingemail@gmail.com',
to: 'receivingemail@gmail.com',
subject: 'Testing!',
body: 'IP Address of the machine is ' + ipAddress
},
function(error, success) {
console.log('Message ' + success ? 'sent' : 'failed');
console.log('IP Address is ' + ipAddress);
process.exit();
}
);
So far it is sending the email but it never inserts the IP address. It puts the appropriate IP address in the console log that I can see, but cannot get it to send in an email. Can anyone help me see what I am doing wrong in my code?
That is because the send_mail
function starts before the exec
has returned the ip.
So just start sending the mail once exec has returned the ip.
This should work:
var exec = require("child_process").exec;
var ipAddress;
var child = exec("ifconfig | grep -m 1 inet", function (error, stdout, stderr) {
ipAddress = stdout;
start();
});
var email = require('nodemailer');
function start(){
email.SMTP = {
host: 'smtp.gmail.com',
port: 465,
ssl: true,
user_authentication: true,
user: 'sendingemail@gmail.com',
pass: 'mypass'
}
email.send_mail({
sender: 'sendingemail@gmail.com',
to: 'receivingemail@gmail.com',
subject: 'Testing!',
body: 'IP Address of the machine is ' + ipAddress
},
function(error, success) {
console.log('Message ' + success ? 'sent' : 'failed');
console.log('IP Address is ' + ipAddress);
process.exit();
}
);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.