Email test code that sends an email when an "e" is sent from the serial monitor to the arduino.
//zoomkat 4/08/12
//email test code using DNS
//developed from code posted by various persons
#include <SPI.h>
#include <Ethernet.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "smtp.yourISP.net"; // your ISP's SMTP server
EthernetClient client;
//////////////////////
void setup(){
Serial.begin(9600);
Serial.println("DNS and DHCP-based email test 4/08/12"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
// print your local IP address:
Serial.print("Arduino IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
Serial.println();
}
void loop()
{
byte inChar;
inChar = Serial.read();
if(inChar == 'e')
{
if(sendEmail()) Serial.println("Email sent"); //start sendEmail()
else Serial.println("Email failed");
}
}
//////////////////////////////// email function
byte sendEmail() //sendEmail() function that returns 1 or 0
{
//start and check for email server connection
if (client.connect(serverName, 25)) {
Serial.println("connected");
}
else {
Serial.println("connection failed");
return 0; //send 0 (failed) back to sendEmail() function
}
//wait for server "queing" response
while(!client.available()) delay(1);
client.println("HELO itismeletschat"); /*hello (statement after helo is needed but irrelevant)*/
//wait for server "hello" response
while(!client.available()) delay(1);
client.println("MAIL From: me@athome.net"); // identify sender, this should be the same as the smtp server you are using*/
//wait for server "sender ok" response
while(!client.available()) delay(1);
client.println("RCPT To: you@athome.net"); /* identify recipient */
//wait for server "receipent ok" response
while(!client.available()) delay(1);
client.println("DATA");
//wait for server to say "enter your message" response
while(!client.available()) delay(1);
//send email message to server
client.println("To: you@athome.net"); /* identify recipient */
client.println("Subject: You Have Arduino Mail!!"); /* insert subject */
client.println("Please let me know it worked!!!"); /* insert body */
client.println("."); /* end email */
//wait for server "message accepted" response
while(!client.available()) delay(1);
client.println("QUIT"); /* terminate connection */
//wait for server "goodby" response
while(!client.available()) delay(1);
//stop client connection
client.stop();
Serial.println("disconnected");
return 1; //send 1 (success) back to sendEmail() function
}