(Behind Corporate Lines) ESP8266 Sends Emails through the Firewall

In my last post, I just ordered my ESP8266 boards and an ESP8266-EVB from OLIMEX.  I don’t have my PCBs yet, but I did get in the ESP8266-EVB and it was very simple to wire up.

  • FTDI RED (+5V)  <–> +5V of ESP8266-EVB
  • FTDI BLK (GND) <–> GND of ESP8266-EVB
  • FTDI ORG (TX)    <–> RX of ESP8266-EVB
  • FTDI YEL (RX)    <–> TX of ESP8266-EVB

The FTDI cable provides enough power when connected to a typical USB port to supply the ESP8266.  (Note: I used an TTL-232R-3V3 which provides 5V on the Red wire, but the Orange (TX) and Yellow (RX) are at 3.3V levels.  If you do any reading on the ESP8266 you’ll see everyone SCREAMING at you to not connect 5V TTL levels to the chip or you could damage it.

Initially, I just wanted to see how easy it was to connect the ESP8266 to Wifi and get some simple data going back and forth.  (Full Disclosure: I’m not a web developer!!  GET, POST, HTML, PHP…. These are all pretty foreign concepts to me, but for the sake of doing something cool with this chip, I dove in.)  I initially looked at the list of AT commands from the data sheet but found that it was more helpful to follow some step-by-step tutorials by some of the kind folks in the community who wanted to share their experience as well and that really helped. I recommend the same to anyone getting into this chip as well.

Finally, I decided that a fun first “project” would be to have my ESP8266 email me something.  Having developed (interpreted as “copied someone’s open source example online”) some Java and .Net applications that send emails from Windows and Linux machines, one of the problems I ran into was corporate firewalls that block anything but Port 80 and don’t allow emails sent out from their network unless it’s one of their own.  To get around this, I created a simple PHP script using the example from Inmotion Hosting.  My PHP script resides in a LAMP (Linux Apache MySQL PHP) server outside of whatever network I intend to send the emails from.  To send an email, I simply issue an HTML POST command to the script location with the FROM, MESSAGE, etc… content and my script sends the email from the server.  The advantage of this is that using this method, I am able to utilize Port 80 which is open by most corporate firewalls.

My goal is to ultimately connect the ESP8266 to microcontrollers, but because the communication is simply a serial protocol, I’ve been doing all my initial testing using terminal programs.  This quickly proved frustrating because I had to keep switching back and forth between CR (Carriage Return) termination and CRLF (Carriage Return + Line Feed) termination when sending POST and GET HTML commands.  I also had to manually count how many characters I was going to send with the HTML commands each time.  Too slow…  To speed up my development, I wrote a simple Java application that allows me to do all this automatically.

Java ESP8266 UI for easy Development
Java ESP8266 UI for easy Development

The following output shows the ESP8266 sending requesting an email be sent from my@email.com with a message of 12345.  The script on the server has the email location where the message will be sent but it could be modified so that recipient is just another variable provided by the ESP8266.

AT+RST

OK

 ets Jan  8 2013,rst cause:4, boot mode:(3,7)

wdt reset


load 0x40100000, len 24444, room 16 


tail 12
chksum 0xe0
ho 0 tail 12 room 4
load 0x3ffe8000, len 3168, room 12 
tail 4
chksum 0x93
load 0x3ffe8c60, len 4956, room 4 
tail 8
chksum 0xbd
csum 0xbd

ready

AT+CIFSR

192.168.1.22

AT+CIPMUX=1

OK

AT+CIPSTART=4,"TCP","xxx.xx.xx.xxx",80

OK

Linked

AT+CIPSEND=4,<# of characters to send>
> 

POST /email.php HTTP/1.1Host: www.yourwebsite.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 34
email=my%40email.com&message=12345

busy

SEND OK

+IPD,4,193:

HTTP/1.1 200 OK
Date: Sat, 04 Apr 2015 20:00:37 GMT
Server: Apache
X-Powered-By: PHP/5.4.39
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

15
Message has been sent

OK

+IPD,4,5:0

OK

OK
Unlink

Here is the email.php script used to send the email.  This is residing on my server.

<?php

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require("lib/PHPMailer/PHPMailerAutoload.php");
require("PHPMailer_5.2.0/class.phpmailer.php");
$mail = new PHPMailer();

// set mailer to use SMTP
$mail->IsSMTP();

// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "www.yourserver.com";  // specify main and backup server

//$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug  = 1;
$mail->SMTPAuth = true;     // turn on SMTP authentication

$mail->SMTPSecure = "ssl";
$mail->Port= 465;
//$mail->SMTPSecure = "tls";
//$mail->Port= 587;

// When sending email using PHPMailer, you need to send from a valid email address
$mail->Username = "you@whatever.com";  // SMTP username
$mail->Password = "password"; // SMTP password

// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;

// below we want to set the email address we will be sending our email to.
$mail->AddAddress("someperson@whatever.com", "Person's Name");

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);

$mail->Subject = "Sending you a note from my microcontroller with a $5 wifi module!!";

// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body    = $message;
$mail->AltBody = $message;

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
?>

My next project with this chip will be to set up a similar PHP script and a MySQL database that allows me to pass data back and forth from the ESP8266 to any other phone or computer through my server again utilizing Port 80 to avoid firewall blockage in corporate environments.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>