Skip to content

Instantly share code, notes, and snippets.

@elktros
Created June 25, 2018 07:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elktros/c3758eaba57902c4aaf60e14e0339227 to your computer and use it in GitHub Desktop.
Save elktros/c3758eaba57902c4aaf60e14e0339227 to your computer and use it in GitHub Desktop.
Code for Sending an Email from ESP8266 Module.
#include <ESP8266WiFi.h>
const char* ssid = "SSID"; // Enter the SSID of your WiFi Network.
const char* password = "PASSWORD";// Enter the Password of your WiFi Network.
char server[] = "mail.smtp2go.com"; // The SMTP Server
WiFiClient espClient;
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("");
Serial.println("");
Serial.print("Connecting To: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("*");
}
Serial.println("");
Serial.println("WiFi Connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
byte ret = sendEmail();
}
void loop()
{
}
byte sendEmail()
{
if (espClient.connect(server, 2525) == 1)
{
Serial.println(F("connected"));
}
else
{
Serial.println(F("connection failed"));
return 0;
}
if (!emailResp())
return 0;
//
Serial.println(F("Sending EHLO"));
espClient.println("EHLO www.example.com");
if (!emailResp())
return 0;
//
/*Serial.println(F("Sending TTLS"));
espClient.println("STARTTLS");
if (!emailResp())
return 0;*/
//
Serial.println(F("Sending auth login"));
espClient.println("AUTH LOGIN");
if (!emailResp())
return 0;
//
Serial.println(F("Sending User"));
// Change this to your base64, ASCII encoded username
/*
For example, the email address test@gmail.com would be encoded as dGVzdEBnbWFpbC5jb20=
*/
espClient.println("dGVzdEBnbWFpbC5jb20="); //base64, ASCII encoded Username
if (!emailResp())
return 0;
//
Serial.println(F("Sending Password"));
// change to your base64, ASCII encoded password
/*
For example, if your password is "testpassword" (excluding the quotes),
it would be encoded as dGVzdHBhc3N3b3Jk
*/
espClient.println("dGVzdHBhc3N3b3Jk");//base64, ASCII encoded Password
if (!emailResp())
return 0;
//
Serial.println(F("Sending From"));
// change to sender email address
espClient.println(F("MAIL From: sender@gmail.com"));
if (!emailResp())
return 0;
// change to recipient address
Serial.println(F("Sending To"));
espClient.println(F("RCPT To: receiver@gmail.com"));
if (!emailResp())
return 0;
//
Serial.println(F("Sending DATA"));
espClient.println(F("DATA"));
if (!emailResp())
return 0;
Serial.println(F("Sending email"));
// change to recipient address
espClient.println(F("To: receiver@gmail.com"));
// change to your address
espClient.println(F("From: sender@gmail.com"));
espClient.println(F("Subject: ESP8266 test e-mail\r\n"));
espClient.println(F("This is is a test e-mail sent from ESP8266.\n"));
espClient.println(F("Second line of the test e-mail."));
espClient.println(F("Third line of the test e-mail."));
//
espClient.println(F("."));
if (!emailResp())
return 0;
//
Serial.println(F("Sending QUIT"));
espClient.println(F("QUIT"));
if (!emailResp())
return 0;
//
espClient.stop();
Serial.println(F("disconnected"));
return 1;
}
byte emailResp()
{
byte responseCode;
byte readByte;
int loopCount = 0;
while (!espClient.available())
{
delay(1);
loopCount++;
// Wait for 20 seconds and if nothing is received, stop.
if (loopCount > 20000)
{
espClient.stop();
Serial.println(F("\r\nTimeout"));
return 0;
}
}
responseCode = espClient.peek();
while (espClient.available())
{
readByte = espClient.read();
Serial.write(readByte);
}
if (responseCode >= '4')
{
// efail();
return 0;
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment