2012-03-26 3 views
1

그래서 node.js를 통해 내 IP 주소를 보내려고 노력 중이며 지금까지는 손을 들지 않았습니다. 지금까지 내 코드는 다음과 같습니다.node.js를 사용하여 이메일에 IP 주소 보내기

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: '[email protected]', 
    pass: 'mypass' 
} 

email.send_mail({ 
    sender: '[email protected]', 
    to: '[email protected]', 
    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(); 
    } 
); 

지금까지 이메일을 보내지 만 IP 주소를 삽입하지 않습니다. 그것은 볼 수있는 콘솔 로그에 적절한 IP 주소를 넣지 만 전자 메일로 보낼 수는 없습니다. 아무도 내 코드에서 내가 뭘 잘못하고 있는지 알 수 있습니까?

+0

당신이 교차 OS 인 대신 os.networkInterfaces''의 "간부"사용하는 이유는 무엇입니까? 출처 : http://nodejs.org/docs/latest/api/os.html#os_os_networkinterfaces – seppo0010

답변

0

exec이 ip를 반환하기 전에 send_mail 함수가 시작되기 때문입니다.

그래서 일단 exec가 ip를 반환하면 메일 보내기를 시작하십시오.

이 작동합니다 :

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: '[email protected]', 
     pass: 'mypass' 
    } 

    email.send_mail({ 
     sender: '[email protected]', 
     to: '[email protected]', 
     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(); 
     } 
    ); 
} 
+0

네, 이것은 매력처럼 작동합니다! 고마워, 내가 node.js로 뭘하고 있는지 전혀 알 수 없으므로 :-) – noiz77

+0

당신은 환영합니다! 모든 시작은 어려움 : D – stewe