Go Down

Topic: Email from ESP8266 for $3 (Read 191 times) previous topic - next topic

b-james

Jun 09, 2015, 07:16 am Last Edit: Jun 09, 2015, 07:19 am by b-james
All the details are in the header ESP8266 is a WiFi chip for $3
Public servers like yahoo and gmail etc won't accept email without memory expensive secure authorisation so you must use something like smtp2go as an itermediary.
 
Code: [Select]

//Adapted from SurferTim's Ethernet email program by Ty Tower May 2015
Register at smtp2go.com --easy. Encode user and password for smtp2go.com in base 64 utf8 here [url]https://www.base64encode.org/[/url]
Put those in the right places in code and change the IP details to yours
Put in where you want to send the email and who it is from.
Change the MAC id to your module's


#include <ESP8266WiFi.h>
//#include <WiFiClient.h>

const char* ssid = "----------HOME";
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-from 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 smtpcorp.com (no DNS)
char smtpcorp[] = "smtpcorp.com";
int port = 2525;



void setup()
{
  Serial.begin(115200);
  pinMode(13,OUTPUT);
  digitalWrite(13,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( smtpcorp,port) == 1) {
    Serial.println(F("connected smtp2go"));
  } else {
    Serial.println(F("connection failed smtp2go"));
    return 0;
  }
  if(!eRcv()) {Serial.println("before ehlo");return 0 ;}

  Serial.println(F("Sending ehlo"));
// replace 192.168.0.3 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@home.come>");
  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()) {Serial.println("aftersending");return 0 ;}

  Serial.println(F("Sending QUIT")); //Serial.println(F("Sending QUIT"));
  client.println("QUIT");
 
  if(!eRcv()) {Serial.println("afterQuit");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"));
}

Go Up