Sending an email is probably the most convenient way of communicating data or informing us of an event from our ESP8266. It’s cross-platform, works on every device and very reliable. It can alert us when one of the sensors detected something (PIR sensor for movement, MQ2 for air quality or any other environmental sensor).
There are a few ways to get that done, and in this post, I’ll show you my preferable way.
In the first few projects I’ve done with the ESP8266, I used native Arduino code to do the actual email sending work. It proved to be complicated and not always a reliable solution. For instance, sending an email with Gmail is different from sending it via Microsoft’s Exchange server, and there aren’t a lot of examples out there.
Send Emails with ESP8266
After a few projects, I decided to change my approach, instead of writing Arduino code to send emails, I started using PHP code to perform the email sending. The benefits of using this approach are many and the most notable ones are:
- You can use a single function across all of your Arduino projects.
- You can easily find an example on how to code with the required server (Exchange, Gmail, SMTP, etc.)
- You can change the way emails are sent even after you’ve compiled and installed your sketch.
- Credentials with the email server are not stored on your chip, so you could easily replace them when necessary.
What you’ll need for this?
Web Hosting capable of PHP
The most important factor is web hosting. You’ll need to host a web page that can run a script for sending an email by request, and in this example, PHP web hosting. There are many hosting companies out there that are very cheap, or even free for this kind of usage.
And, if you’re worried about security then think about this, if you want to send information outside, that means that your chip already needs access to the internet, so using an external tool to send an email is not much different (if not more secure).
Arduino Code for sending Email
This is a simple function for using a page to send an email. note that we’re using basic authentication to connect to our web page. This is essential for a secure usage. Please use basic authentication on https only.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
void sendEmail (String Address, String Subject, String Body){ HTTPClient http2; http2.begin("https://ourserver.com/sendEmail.php?Address=" + Address + "&Subject=" + Subject + "&Body=" + Body); //HTTP http2.setAuthorization("MyUserName", "MyPassword"); // authenticate with basic authenticaion esp8266 digitalWrite ( led, 1 ); int httpCode = http2.GET(); Serial.print("Sending the email"); if(httpCode > 0) { if(httpCode == HTTP_CODE_OK) { Serial.println("Success hb"); } } else { Serial.printf("Couldnt send Email: %s\n", http2.errorToString(httpCode).c_str()); } http2.end(); digitalWrite ( led, 0 ); } |
This code uses our PHP page (pointed to https://ourserver.com/sendEmail.php or wherever we hosted it on) and passes to it the required arguments for sending the mail.
PHP code for sending an email with ESP8266
The Arduino code from the previous paragraph will call a PHP page with a code for sending emails. Here is an example of how to do it.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
if ($_SERVER['PHP_AUTH_USER']!="MyUserName" && $_SERVER['PHP_AUTH_PW'] != "MyPassword") { header('WWW-Authenticate: Basic realm="Sending Email Realm"'); header('HTTP/1.0 401 Unauthorized'); echo 'Please enter the corret username and password.'; exit; } $address = $_GET['Address']; $subject = $_GET['Subject']; $body = $_GET['body']; mail ($address, $subject, $body); ?> |
This simple code’s purpose is to demonstrate how easy it is to send an email with PHP. So what we actually did here is transforming a large and a complicated question “How to send Email with ESP8266” to a much easier and documented one: “How to send Email with PHP”. Changing this script to send an email using any other server, configuration or requirement is as easy as searching google for the solution.
Let me know what you think in the comments below 🙂
Pingback: Make your own smart gas sensor with MQ2 and ESP8266 WiFi Arudino | Roei's Tips Stream