ESP32/ESP8266 Send Email Notification using PHP Script
Build an ESP32 or ESP8266 client that makes an HTTP POST request to a PHP script to send an Email Notification with sensor readings. Set a threshold value with temperature/humidity/pressure.
- Suitable forB.Tech/Diploma IoT/ESP32 students
- Build time1-2 weeks
- LevelBeginner
- Available atAjvika Labs, Kakinada
About this project
In this project, you’ll build an ESP32 or ESP8266 client that makes an HTTP POST request to a PHP script to send an email notification with sensor readings.
You can also set a threshold value, so your email notification is only sent if the temperature/humidity/pressure is above a certain value.
As an example, we’ll be using a BME280 sensor connected to an ESP32 or ESP8266 board. You can modify the code provided to send readings from a different sensor or even use multiple boards.
This is a great way to send email notifications using the ESP32 or ESP8266 without relying on IFTTT or an SMTP server.
In order to build this project, you will:
Host a server and setup a domain name
Create PHP script to send email notifications
Program ESP32 or ESP8266 with Arduino IDE
This project is also a great addition to build upon our previous projects:
ESP32/ESP8266 Insert Data into MySQL Database using PHP and Arduino IDE
Visualize Your Sensor Readings from Anywhere in the World
1. Hosting Your PHP Application
The goal of this project is to have your own domain name and hosting account that allows you to send email notifications (without using an SMTP server, IFTTT, etc…).
Here’s a high-level overview of how the project works:
I recommend using one of the following hosting services that can handle all the project requirements:
: free domain name when you sign up for the 3-year plan. I recommend choosing the unlimited websites option;
Digital Ocean
: Linux server that you manage through a command line. I only recommended this option for advanced users.
Those two services are the ones that I use and personally recommend, but you can use any other hosting service. Any hosting service that offers PHP and MySQL will work with this tutorial. If you don’t have a hosting account, I recommend
Note:
you can also run a
LAMP (Linux, Apache, MySQL, PHP) server
on a Raspberry Pi, but it can’t send emails standalone. To send an email with a Raspberry Pi using PHP, you need to use an SMTP (Simple Mail Transfer Protocol) server.
2. PHP Script HTTP – Send Email Notification
After signing up for a
hosting account and setting up a domain name
, you can login to your cPanel or similar dashboard.
After that, follow the next steps to create a PHP script that is responsible for receiving incoming requests from the ESP32/ESP8266 and sending an email.
If you’re using a hosting provider with cPanel, go to Advanced and search for “
File Manager
“:
Then, select the
public_html
option and press the “
+ File
” button to create a new
.php
file.
Note:
if you’re following this tutorial and you’re not familiar with PHP, I recommend creating that exact file. Otherwise, you’ll need to modify the ESP sketch provided with a different URL path.
Create a new file in
/public_html
with this exact name and extension:
email-notification.php
Edit the newly created file (
email-notification.php
) and copy the following script:
<?php
/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
// Receiver Email Address (where to send email notification)
$email_address = \"
[email protected]
\";
// Keep this API Key value to be compatible with the ESP code provided in the project page. If you change this value, the ESP sketch needs to match
$api_key_value = \"tPmAT5Ab3j7F9\";
$api_key = $value1 = $value2 = $value3 = \"\";
if ($_SERVER[\"REQUEST_METHOD\"] == \"POST\") {
$api_key = test_input($_POST[\"api_key\"]);
if($api_key == $api_key_value) {
$value1 = test_input($_POST[\"value1\"]);
$value2 = test_input($_POST[\"value2\"]);
$value3 = test_input($_POST[\"value3\"]);
// Email message
$email_msg = \"Temperature: \". $value1. \"ºC\
Humidity: \". $value2. \"%\
Pressure: \". $value3. \"hPa\";
// Use wordwrap() if lines are longer than 70 characters
$email_msg = wordwrap($email_msg, 70);
// Uncomment the next if statement to set a threshold
// ($value1 = temperature, $value2 = humidity, $value3 = pressure)
/*if($value1 < 24.0){
echo \"Temperature below threshold, don't send email\";
exit;
}*/
// send email with mail(receiver email address, email subject, email message)
mail($email_address, \"[NEW] ESP BME280 Readings\", $email_msg);
echo \"Email sent\";
}
else {
echo \"Wrong API Key provided.\";
}
}
else {
echo \"No data posted with HTTP POST.\";
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Before saving the file, you need to modify the
$email_address
variable with the receiver email address:
// Receiver Email Address (where to send email notification)
$email_address = \"
[email protected]
\";
After adding the receiver email, save the file and continue with this tutorial. If you try to access your domain name in the next URL path, you’ll see the following:
https://example.com/email-notification.php
If you see that message, it means that everything is being setup properly. You can continue with this project.
3. Setting Up Your ESP32 or ESP8266
This project is compatible with both the ESP32 and ESP8266 boards. You just need to assemble a simple circuit and upload the sketch provided to publish temperature, humidity, and pressure readings to your PHP script, which will then be responsible to handle email notifications. The sketch is slightly different for each board.
Parts Required
For this example, we’ll get sensor readings from the BME280 sensor. Here’s a list of parts you need to build the circuit for this project:
ESP32 board
(read
Best ESP32 dev boards
)
Alternative –
ESP8266
board (read
Best ESP8266 dev boards
)
BME280 sensor
Jumper wires
Breadboard
You can use the preceding links or go directly to
MakerAdvisor.com/tools
to find all the parts for your projects at the best price!
Schematics
The BME280 sensor module we’re using communicates via I2C communication protocol, so you need to connect it to the ESP32 or ESP8266 I2C pins.
BME280 wiring to ESP32
BME280
ESP32
SCK (SCL Pin)
GPIO 22
SDI (SDA pin)
GPIO 21
So, assemble your circuit as shown in the next schematic diagram (
read the complete Guide for ESP32 with BME280
).
Recommended reading:
ESP32 Pinout Reference Guide
BME280 wiring to ESP8266
BME280
ESP8266
SCK (SCL Pin)
GPIO 5
SDI (SDA pin)
GPIO 4
Assemble your circuit as in the next schematic diagram if you’re using an ESP8266 board (
read the complete Guide for ESP8266 with BME280
).
Recommended reading:
ESP8266 Pinout Reference Guide
Installing Libraries
We’ll program the ESP32/ESP8266 using Arduino IDE, so you must have the ESP32/ESP8266 add-on installed in your Arduino IDE. Follow one of the next tutorials depending on the board you’re using:
Install the ESP32 Board in Arduino IDE
– you also need to
install the BME280 Library
and
Adafruit_Sensor library
Install the ESP8266 Board in Arduino IDE
– you also need to
install the BME280 Library
and
Adafruit_Sensor library
ESP32 Code
For an ESP8266 click here
After installing the necessary board add-ons, copy the following code to your Arduino IDE, but don’t upload it yet. You need to make some changes to make it work for yo
You can also set a threshold value, so your email notification is only sent if the temperature/humidity/pressure is above a certain value.
As an example, we’ll be using a BME280 sensor connected to an ESP32 or ESP8266 board. You can modify the code provided to send readings from a different sensor or even use multiple boards.
This is a great way to send email notifications using the ESP32 or ESP8266 without relying on IFTTT or an SMTP server.
In order to build this project, you will:
Host a server and setup a domain name
Create PHP script to send email notifications
Program ESP32 or ESP8266 with Arduino IDE
This project is also a great addition to build upon our previous projects:
ESP32/ESP8266 Insert Data into MySQL Database using PHP and Arduino IDE
Visualize Your Sensor Readings from Anywhere in the World
1. Hosting Your PHP Application
The goal of this project is to have your own domain name and hosting account that allows you to send email notifications (without using an SMTP server, IFTTT, etc…).
Here’s a high-level overview of how the project works:
I recommend using one of the following hosting services that can handle all the project requirements:
: free domain name when you sign up for the 3-year plan. I recommend choosing the unlimited websites option;
Digital Ocean
: Linux server that you manage through a command line. I only recommended this option for advanced users.
Those two services are the ones that I use and personally recommend, but you can use any other hosting service. Any hosting service that offers PHP and MySQL will work with this tutorial. If you don’t have a hosting account, I recommend
Note:
you can also run a
LAMP (Linux, Apache, MySQL, PHP) server
on a Raspberry Pi, but it can’t send emails standalone. To send an email with a Raspberry Pi using PHP, you need to use an SMTP (Simple Mail Transfer Protocol) server.
2. PHP Script HTTP – Send Email Notification
After signing up for a
hosting account and setting up a domain name
, you can login to your cPanel or similar dashboard.
After that, follow the next steps to create a PHP script that is responsible for receiving incoming requests from the ESP32/ESP8266 and sending an email.
If you’re using a hosting provider with cPanel, go to Advanced and search for “
File Manager
“:
Then, select the
public_html
option and press the “
+ File
” button to create a new
.php
file.
Note:
if you’re following this tutorial and you’re not familiar with PHP, I recommend creating that exact file. Otherwise, you’ll need to modify the ESP sketch provided with a different URL path.
Create a new file in
/public_html
with this exact name and extension:
email-notification.php
Edit the newly created file (
email-notification.php
) and copy the following script:
<?php
/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
// Receiver Email Address (where to send email notification)
$email_address = \"
[email protected]
\";
// Keep this API Key value to be compatible with the ESP code provided in the project page. If you change this value, the ESP sketch needs to match
$api_key_value = \"tPmAT5Ab3j7F9\";
$api_key = $value1 = $value2 = $value3 = \"\";
if ($_SERVER[\"REQUEST_METHOD\"] == \"POST\") {
$api_key = test_input($_POST[\"api_key\"]);
if($api_key == $api_key_value) {
$value1 = test_input($_POST[\"value1\"]);
$value2 = test_input($_POST[\"value2\"]);
$value3 = test_input($_POST[\"value3\"]);
// Email message
$email_msg = \"Temperature: \". $value1. \"ºC\
Humidity: \". $value2. \"%\
Pressure: \". $value3. \"hPa\";
// Use wordwrap() if lines are longer than 70 characters
$email_msg = wordwrap($email_msg, 70);
// Uncomment the next if statement to set a threshold
// ($value1 = temperature, $value2 = humidity, $value3 = pressure)
/*if($value1 < 24.0){
echo \"Temperature below threshold, don't send email\";
exit;
}*/
// send email with mail(receiver email address, email subject, email message)
mail($email_address, \"[NEW] ESP BME280 Readings\", $email_msg);
echo \"Email sent\";
}
else {
echo \"Wrong API Key provided.\";
}
}
else {
echo \"No data posted with HTTP POST.\";
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Before saving the file, you need to modify the
$email_address
variable with the receiver email address:
// Receiver Email Address (where to send email notification)
$email_address = \"
[email protected]
\";
After adding the receiver email, save the file and continue with this tutorial. If you try to access your domain name in the next URL path, you’ll see the following:
https://example.com/email-notification.php
If you see that message, it means that everything is being setup properly. You can continue with this project.
3. Setting Up Your ESP32 or ESP8266
This project is compatible with both the ESP32 and ESP8266 boards. You just need to assemble a simple circuit and upload the sketch provided to publish temperature, humidity, and pressure readings to your PHP script, which will then be responsible to handle email notifications. The sketch is slightly different for each board.
Parts Required
For this example, we’ll get sensor readings from the BME280 sensor. Here’s a list of parts you need to build the circuit for this project:
ESP32 board
(read
Best ESP32 dev boards
)
Alternative –
ESP8266
board (read
Best ESP8266 dev boards
)
BME280 sensor
Jumper wires
Breadboard
You can use the preceding links or go directly to
MakerAdvisor.com/tools
to find all the parts for your projects at the best price!
Schematics
The BME280 sensor module we’re using communicates via I2C communication protocol, so you need to connect it to the ESP32 or ESP8266 I2C pins.
BME280 wiring to ESP32
BME280
ESP32
SCK (SCL Pin)
GPIO 22
SDI (SDA pin)
GPIO 21
So, assemble your circuit as shown in the next schematic diagram (
read the complete Guide for ESP32 with BME280
).
Recommended reading:
ESP32 Pinout Reference Guide
BME280 wiring to ESP8266
BME280
ESP8266
SCK (SCL Pin)
GPIO 5
SDI (SDA pin)
GPIO 4
Assemble your circuit as in the next schematic diagram if you’re using an ESP8266 board (
read the complete Guide for ESP8266 with BME280
).
Recommended reading:
ESP8266 Pinout Reference Guide
Installing Libraries
We’ll program the ESP32/ESP8266 using Arduino IDE, so you must have the ESP32/ESP8266 add-on installed in your Arduino IDE. Follow one of the next tutorials depending on the board you’re using:
Install the ESP32 Board in Arduino IDE
– you also need to
install the BME280 Library
and
Adafruit_Sensor library
Install the ESP8266 Board in Arduino IDE
– you also need to
install the BME280 Library
and
Adafruit_Sensor library
ESP32 Code
For an ESP8266 click here
After installing the necessary board add-ons, copy the following code to your Arduino IDE, but don’t upload it yet. You need to make some changes to make it work for yo
Technologies used
Hardware components
- ESP32
- ESP8266
- Arduino
- Raspberry Pi
- BME280
Software & tools
- Arduino IDE
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.