I wrote a sketch that reads a POP3 email account looking for an email with the subject "Arduino" in order to control the Arduino via email. It works with POP3 email servers that do not require TLS or SSL security to log in. If you test it, let me know if it works with your email account or not.
#include <SPI.h>
#include <Ethernet.h>
// this must be unique on your localnet
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// change server to your email server ip or domain
IPAddress server( 1,2,3,4 );
// char server[] = "myemailserver.com";
int port = 110;
// change to plain text user and password
char userName[] = "myusername";
char passWord[] = "mypassword";
EthernetClient client;
char rtnBuf[100];
void setup()
{
delay(2000);
Serial.begin(115200);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
while(!Ethernet.begin(mac)) {
Serial.println("DHCP failed. Retry in 5 seconds");
delay(5000);
}
Serial.println(Ethernet.localIP());
delay(2000);
Serial.println(F("Ready. Press 'e' to check email."));
}
void loop()
{
byte inChar;
inChar = Serial.read();
if(inChar == 'e')
{
if(getEmail()) Serial.println(F("Email retrieved"));
else Serial.println(F("Email failed"));
}
Ethernet.maintain();
}
byte getEmail()
{
byte thisByte = 0;
byte respCode;
char rtnVal[64];
int mailCount,mailSize;
char tBuf[64];
if(client.connect(server,port) == 1) {
Serial.println(F("connected"));
} else {
Serial.println(F("connection failed"));
return 0;
}
if(!eRcv()) return 0;
Serial.println(rtnBuf);
Serial.println(F("Sending User"));
strcpy_P(tBuf,PSTR("USER "));
strcat(tBuf,userName);
strcat_P(tBuf,PSTR("\r\n"));
client.write(tBuf);
if(!eRcv()) return 0;
Serial.println(rtnBuf);
Serial.print(F("Sending Password "));
strcpy_P(tBuf,PSTR("PASS "));
strcat(tBuf,passWord);
strcat_P(tBuf,PSTR("\r\n"));
Serial.println(tBuf);
client.write(tBuf);
if(!eRcv()) return 0;
Serial.println(rtnBuf);
Serial.println(F("Sending STAT"));
strcpy_P(tBuf,PSTR("STAT\r\n"));
client.write(tBuf);
if(!eRcv()) return 0;
Serial.println(rtnBuf);
sscanf(rtnBuf,"%s %u %u",rtnVal,&mailCount,&mailSize);
Serial.print("mail count: ");
Serial.println(mailCount);
for(int i = 1;i<=mailCount;i++) {
strcpy_P(tBuf,PSTR("RETR "));
itoa(i,rtnBuf,10);
strcat(tBuf,rtnBuf);
strcat(tBuf,"\r\n");
client.write(tBuf);
ePrint();
Serial.println("\r\nEND");
}
Serial.println(F("Sending QUIT"));
strcpy_P(tBuf,PSTR("QUIT\r\n"));
client.write(tBuf);
if(!eRcv()) return 0;
Serial.println(rtnBuf);
client.stop();
Serial.println(F("disconnected"));
return 1;
}
byte eRcv()
{
byte respCode;
byte thisByte;
int loopCount = 0;
byte rtnCount = 0;
while(!client.available()) {
delay(1);
loopCount++;
// if nothing received for 10 seconds, timeout
if(loopCount > 10000) {
client.stop();
Serial.println(F("\r\nTimeout"));
return 0;
}
}
while(client.available())
{
thisByte = client.read();
// Serial.write(thisByte);
if(rtnCount < 99) {
rtnBuf[rtnCount]=thisByte;
rtnCount++;
rtnBuf[rtnCount]=0;
}
}
if(rtnBuf[0] == '-') return 0;
return 1;
}
byte ePrint()
{
byte respCode;
byte thisByte;
int loopCount = 0;
byte rtnCount = 0;
while(!client.available()) {
delay(1);
loopCount++;
// if nothing received for 10 seconds, timeout
if(loopCount > 10000) {
client.stop();
Serial.println(F("\r\nTimeout"));
return 0;
}
}
byte lineLength = 0;
bool endMsg = false;
bool msgBody = false;
char lineBuf[64];
while(!endMsg) {
while(client.available())
{
thisByte = client.read();
if(thisByte == '\n') {
// end of line
if(strlen(lineBuf) == 0) {
if(!msgBody) Serial.println("Message body");
msgBody = true;
}
if(strcmp(lineBuf,".") == 0) {
// end of message
endMsg = true;
}
if(!msgBody && (strncmp(lineBuf,"From:",5) == 0)) {
// from
Serial.println(lineBuf);
}
if(!msgBody && (strncmp(lineBuf,"Subject:",8) == 0)) {
// subject
Serial.println(lineBuf);
if(strncmp(&lineBuf[9],"Arduino",7) == 0) Serial.println(F("For my Arduino!"));
}
lineLength = 0;
lineBuf[0] = 0;
}
else if(thisByte != '\r') {
// another character
if(lineLength < 63) {
lineBuf[lineLength] = thisByte;
lineLength++;
lineBuf[lineLength] = 0;
}
}
}
}
return 1;
}
void efail()
{
byte thisByte = 0;
int loopCount = 0;
client.println("QUIT");
while(!client.available()) {
delay(1);
loopCount++;
// if nothing received for 10 seconds, timeout
if(loopCount > 10000) {
client.stop();
Serial.println(F("\r\nTimeout"));
return;
}
}
while(client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}
client.stop();
Serial.println(F("disconnected"));
}
If it finds an email with the subject "Arduino", it will display "For my Arduino!" on the serial monitor.