Chat freely about the open source Javascript projects for ESP8266

User avatar
By Erni
#32190 This is an example of sending mail with ESP8266.
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


Code: Select allESP8266WiFi.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
User avatar
By jankop
#32212 Good inspiration! I modified it a bit.

Code: Select allvar SSID     =  "yourSSID";
var ApPassw  =  "yourApPassword";
var MailUser =  "MailUser";
var MailPassw=  "MailPassword";
var Recipient=  "recipient";
var hostname =  "smtp.domain.cz";
var SUBJECT  =  "ESP8266 email test";
var TEXT     =  "This is email from my ESP8266 with javascript";
//******************************************************
var ESP8266=require("ESP8266");
var wifi=require("wifi");
var net = require("net");
wifi.connect(SSID,ApPassw,null, function(err,ipInfo){if (err) {print("Error connecting to access point");}});
var mail = ["EHLO esp8266.fancon.cz\r\n",
"AUTH LOGIN\r\n",
btoa(MailUser)+"\r\n",          //user
btoa(MailPassw)+"\r\n",          //passw 
"MAIL FROM: <esp8266@fancon.cz>\r\n", //from
"RCPT TO: <"+Recipient+">\r\n", //send to
"DATA\r\n",
"To: <"+Recipient+">\r\n",
"Subject:"+SUBJECT+"\r\n",
TEXT+"\r\n.\r\n",
"QUIT\r\n"];
function sendMail() {
ESP8266.getHostByName(hostname, function(ipAddress)
{var client=net.connect({host:ESP8266.getAddressAsString(ipAddress), port:25}, function(){

  print('Connected');
  var i=0;
  client.write(mail[0]);
  client.on('data',  function(data)
                     {print(data);i++;client.write(mail[i]);});
  client.on('close', function()
                      {print('Connection closed');});
  });
  });
}

 sendMail();


 

Edit 25.10.2015> ad function btoa() for convert string to base64, change modules ( "wifi" , "ESP8266") for new API . Tested with 1V81.660 Espruino firmware.
Edit 27.10.2015> Removing no longer needed ESP8266.WiFi.init()
Last edited by jankop on Tue Oct 27, 2015 2:14 pm, edited 2 times in total.
User avatar
By Erni
#32229 Wow jankop

You did it. :D
So this is how to implement a recursive loop (Is that the right name for it ?).
I just couldn't get my head to see it.
Thankyou
User avatar
By jankop
#32234 It is not recursion, it is only a reaction to the data received. Let's say callback.
Recursion is here, blink() calls blink():
Code: Select allvar led = true;
var pin = new Pin(0);
function blink() {
  digitalWrite(pin, led);
  print("Pin" + pin + " = " + led);
  led = !led;
  setTimeout(blink, 1000);
}
blink();