Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By Stephen
#19003 I checked some of the libraries for email on Arduino and it looks like most of them are designed for an ethernet shield or deprecated (or both).

Can anyone suggest a email library which will work on the ESP8266? :(
User avatar
By tytower
#19521 Hey Stephen here is a prog I'm working on atm for email
Maybe you can give me a hand . I've gotta stop for a while .
If you put it on your ESP8266 you must change where I have deleted my personal stuff and put yours in . Its pretty scrappy cause I need to see where it hangs.

It seems I can get it to connect but I'm not getting a response from yahoo yet .I think it needs a secure SSL but i've gotta sort that yet
Read that the public mail servers don't let you in so maybe its going to have to be an ISP provided one.
Code: Select all//Adapted from SurferTims EthernetEmail by Ty Tower

#include <ESP8266WiFi.h>

#include <WiFiClient.h>

const char* ssid = "---------";
const char* password = "";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
WiFiClient client;

// this must be unique for your module
byte mac[] = { 0x18, 0xFE, 0x34, 0xA1 ,0x5C, 0x44 }; //from ESP8266
// change network settings to yours
IPAddress ip( 192, 168, 0, 3 );   
IPAddress gateway( 192, 168, 0, 1 );
IPAddress subnet( 255, 255, 255, 0 );
//IPAddress server(?,?,?,?);  // numeric IP for Yahoo (no DNS)
char myserver[] = "smtp.mail.yahoo.com";
int port = 465;



void setup()
{
  Serial.begin(115200);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  server.begin();
  delay(10000);
  Serial.println(F("Ready. Press 'e' to send."));
}

void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(inChar == 'e')
  {
      if(sendEmail()) Serial.println(F("Email sent"));
      else Serial.println(F("Email failed"));
  }
}

byte sendEmail()
{
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

 
  byte thisByte = 0;
  byte respCode;

  if(client.connect(myserver,port) == 1) {
    Serial.println(F("connected"));
  } else {
    Serial.println(F("connection failed"));
    return 0;
  }
  if(!eRcv()) {Serial.println("before ehlo");return 0 ;}

  Serial.println(F("Sending hello"));
// replace 1.2.3.4 with your ESP8266's ip
  client.println("EHLO 192.168.0.3");
  if(!eRcv()) {Serial.println("ehlo");return 0 ;}

  Serial.println(F("Sending auth login"));
  client.println("auth login");
  if(!eRcv()) {Serial.println("auth");return 0 ;}

  Serial.println("Sending User");
// Change to your base64 encoded user
  client.println("==========");
 
  if(!eRcv()) {Serial.println("user");return 0 ;}

  Serial.println(F("Sending Password"));
// change to your base64 encoded password
  client.println("===============");

  if(!eRcv()) {Serial.println("ehlo");return 0;}

// change to your email address (sender)
  Serial.println(F("Sending From"));
  client.println("MAIL From: <ESP8266-3>");
  if(!eRcv()) {Serial.println("email");return 0 ;}

// change to recipient address
  Serial.println(F("Sending To"));
  client.println("RCPT To: <==========>");
  if(!eRcv()) {Serial.println("email");return 0 ;}

  Serial.println(F("Sending DATA"));
  client.println("DATA");
  if(!eRcv()) {Serial.println("email");return 0 ;}

  Serial.println(F("Sending email"));

// change to recipient address
  client.println("To: Ty Tower <============>");

// change to your address
  client.println("From: Weather <ESP8266-3>");

  client.println("Subject: ESP8266 email test\r\n");

  client.println("This is from my ESP8266-12 Module 3!");

  client.println(".");

  if(!eRcv()) return 0;

  Serial.println(F("Sending QUIT")); //Serial.println(F("Sending QUIT"));
  client.println("QUIT");
 
  if(!eRcv()) return 0;

  client.stop();

  Serial.println(F("disconnected"));

  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;
  int loopCount = 0;

  while(!client.available()) {
    delay(1);
    loopCount++;

    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
      client.stop();
      Serial.println(F("10 sec \r\nTimeout"));
      return 0;
    }
  }

  respCode = client.peek();

  while(client.available())
  { 
    thisByte = client.read();   
    Serial.write(thisByte);
  }

  if(respCode >= '4')
  {
    efail();
    return 0; 
  }

  return 1;
}


void efail()
{
  byte thisByte = 0;
  int loopCount = 0;

  client.println(F("QUIT"));

  while(!client.available()) {
    delay(1);
    loopCount++;

    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
      client.stop();
      Serial.println(F("efail \r\nTimeout"));
      return;
    }
  }

  while(client.available())
  { 
    thisByte = client.read();   
    Serial.write(thisByte);
  }

  client.stop();

  Serial.println(F("disconnected"));
}
[/code]
Last edited by tytower on Fri Jun 05, 2015 6:11 pm, edited 1 time in total.