Email 使用node.js在电子邮件中发送IP地址
所以我试图通过node.js向自己发送我的IP地址,但到目前为止都是空手而归。到目前为止,我的代码如下所示:Email 使用node.js在电子邮件中发送IP地址,email,node.js,ip-address,Email,Node.js,Ip Address,所以我试图通过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
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();
}
);
到目前为止,它正在发送电子邮件,但从未插入IP地址。它将适当的IP地址放在控制台日志中,我可以看到,但无法将其发送到电子邮件中。有人能帮我看看我的代码有什么错误吗?这是因为
send\u mail
功能在exec
返回ip之前启动
所以,只要在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: '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();
}
);
}
是的,这很有魅力!非常感谢,因为您可能会告诉我,我真的不知道我在用node.js做什么:-)为什么要使用“exec”而不是跨操作系统的
os.networkInterfaces
?资料来源: