3.5 Writing the Web Mailer
Libraries for sending email directly from the Arduino abound. But these all rely on a stand-alone, dedicated email server providing the mail gateway. So even though the mailer code can be compiled into the Arduino sketch, the solution still relies on an intermediary to send messages from the Arduino to the email inbox of the intended recipient(s).
If you have access to an SMTP mail server that you can connect to for outbound message transmission, check out Maik Schmidt’s Arduino: A Quick Start Guide [Sch11]. His book supplies the necessary code and walkthrough on how to make this work. If you don’t have access to a dedicated SMTP gateway, we can use an Internet web hosting service that supports sending email from a PHP script.
For this project, I have chosen a popular, preconfigured PHP-enabled web server with an SMTP outbound gateway, a configuration that popular website hosting companies like Dreamhost.net, Godaddy.com, and others offer to their customers.
The PHP script for sending email consists of only a few short lines of code. First, we will pass two parameters to the server: the type of alert to send and the recorded value of the flex resistor. Then we will compose a mail message containing the recipient’s email address, the subject, and the message contents. Then we will send the email.
WaterLevelNotifier/wateralert.php | |
<?php |
|
// Grab the type of
alert to email and |
|
// the current value
of the flex resistor. |
|
$alertvalue = $_GET["alert"]; |
|
$flexvalue = $_GET["flex"]; |
|
|
|
$contact
= 'your@emailaddress.com'; |
|
|
|
if ($alertvalue == "1") { |
|
$subject = "Water Level
Alert"; |
|
$message = "The water level has deflected the
flex |
|
resistor to a value of " . $flexvalue .
"."; |
|
mail($contact, $subject, $message); |
|
echo("<p>Water Level Alert email
sent.</p>"); |
|
}
elseif ($alertvalue == "0") { |
|
$subject = "Water Level OK"; |
|
$message = "The water level is within acceptable
levels. |
|
Flex resistor value is " . $flexvalue .
"."; |
|
mail($contact, $subject, $message); |
|
echo("<p>Water Level OK email
sent.</p>"); |
|
} |
|
|
|
?> |
The script calls the built-in PHP mail
function that passes three required parameters:
recipient(s), subject, and the body of the email. Yes, it’s that
simple.
Save the code to a file called wateralert.php in the
root web directory of your PHP server. You can test the script by
opening your web browser and visiting http://MYPHPSERVERNAME/wateralert.php?alert=1&flex=486
.
The page should return a Water Level Alert
email sent.
message in the browser window, and a
corresponding email message should appear in the defined
recipient’s inbox. If it doesn’t, check your PHP server settings
and make sure that your web server is properly configured to use a
working email gateway. If you’re still not having luck with the
message test, contact your website hosting provider to make sure
your hosted solution is correctly configured for PHP email
messaging.
By abstracting the delivery mechanism from the logic running in the Arduino, we can easily modify the message recipients and contents.
Now that we have a working message gateway, we can hook up the Arduino to an Ethernet shield so the deflected flex resistor can talk to the rest of the world.