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:

  1. You can use a single function across all of your Arduino projects.
  2. You can easily find example on how to code with the required server (Exchange, Gmail, SMTP, etc.)
  3. You can change the way emails are sent even after you’ve compiled and installed your sketch.
  4. 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 our 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.

This code uses our PHP page (pointed to https://ourserver.com/sendEmail.php or wherever we hosted it on) and passes 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.

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 🙂

Leave a comment