I am using smtp2go as mail server because they don't require SSL.
The username and password needs to be base64 encoded, you can use this to do that:
https://www.base64encode.org/
I am using the setInterval function to let each command finish, before sending the next. I don't think that is the way to do it, but i can't come up with a better method at the moment.
From what i have googled a better way would be using a queue or recursive loop, but I am running out of brainpower on this.
I found this article:
http://www.richardrodger.com/2011/04/21/node-js-how-to-write-a-for-loop-with-callbacks/#.ViuEP7crIdV
ESP8266WiFi.init();
var net = require('net');
var hostname="mail.smtp2go.com";
var mail = [
"EHLO www.example.com\r\n",
"auth login\r\n",
"kkdjdjhstmNvbQ==\r\n", //user
"dfgdIwMDE=\r\n", //passw
"MAIL From:xyz0@gmail.com\r\n", //from
"RCPT To: you@yourmail.com\r\n", //sending to
"DATA\r\n",
"To: you@yourmail.com\r\n",
"From: you@yourmail.com\r\n",
"Subject: Esp8266 email test\r\n",
"This is from my ESP8266 with javascript\r\n",
".\r\n", // email ends with .
"QUIT\r\n"
];
var i=0;
ESP8266WiFi.getHostByName(hostname, function(ipAddress){
var client=net.connect({host: ESP8266WiFi.getAddressAsString(ipAddress), port:80}, function() {
print('Connected');
setInterval(function() {
if( i < mail.length ) {
client.write(mail[i]);
client.on('data', function(data) {
print(data);
}); //client on data
i++;
}
}, 1000);
client.on('close', function() {
print('Connection closed');
}); //client on close
}); //client connect
}); //gethost