Emailing Directly from an Arduino
To send an email from the Arduino, we’ll basically implement the
telnet
session
from the previous chapter. Instead of hardwiring the email’s attributes into
the networking code, we’ll create create something more advanced.
We start with an
Email
class:
Ethernet/Email/email.h
#ifndef __EMAIL__H_
#define __EMAIL__H_
class Email {
String _from, _to, _subject, _body;
public:
Email(
const String& from,
const String& to,
const String& subject,
const String& body
) : _from(from), _to(to), _subject(subject), _body(body) {}
const String& getFrom() const { return _from; }
const String& getTo() const { return _to; }
const String& getSubject() const { return _subject; }
const String& getBody() const { return _body; }
};
#endif
This class encapsulates an email’s four most important attributes—the email
addresses of the sender and the recipient, a subject, and a message body.
We store all attributes as
String
objects.
Wait a minute…a
String
class? Yes! The Arduino IDE comes with a full-blown
string class.
4
It doesn’t have as many features as the C++ or Java string
classes, but it’s still way better than messing around with
char
pointers. You’ll
see how to use it in a few paragraphs.
The rest of our
Email
class is pretty straightforward. In the constructor, we
initialize all instance variables, and we have methods for getting every single
attribute. We now need an
SmtpService
class for sending
Email
objects:
4.
http://arduino.cc/en/Reference/StringObject
report erratum • discuss
Emailing Directly from an Arduino • 189
www.it-ebooks.info