Hi all. For sometime now I wanted to have my TM4C12nnYYYYY launchpads and self built ARM based modules being enabled to reliably deliver e-mail messages to certain addresses so to have,
apart from any other type of TCP/UDP sockets connection that I use for data transfers, a backup/hidden channel that I could use to receive regular updates of modules's status and/or
exceptions that I should be aware of, at any time, anywhere, on my mobile phone. So I refreshed my old knowledge on the SMTP protocol and within a day I could have my module to
behave as a very accurate e-mail agent/notifier, so much for the events and data that monitors and controls as for any other exception that I should know. (sudden External boots or watchdog boots)
So I coded the following source which is free for anyone to use. The only thing one needs to use is the real address of his/her e-mail server system (smtp.gmail.com, smtp.myserver.com, e.t.c.)
The TCP client that I use in this example has the name emailIMEI and is created in setup() routine as EthernetClient emailIMEI; I use many distinct routines for almost doing the same
thing, but I wanted to have full understanding of what was happening during debugging sessions. (It is obvious that this source may be used for any embedded project, on any processor or
launchpad. Even, with minor modifications, on Linux, Raspberry-Pi, Windows-C# and or C/C++ platforms) This source does not use any library and implements the SMTP socket based communication
protocol (telnet @ port 25 or 2525) using only the native SMTP protocol's commands. Any suggestions for code improvement are welcomed. Soon, also a POP3 client will be ready for tests.
Hope this source will be useful to many in this forum and beyond. All the best, John Piliounis
int emailSMTP_Sender()
{
uint8_t input_buffer[512];
byte out_buffer [512];
String localSUBJECT = "I am the subject line's content bla-bla-bla-bla";
char l ;
int q = 0;
String inbound = "";
String smtp_connection_1 = "EHLO mail.mySMTPserver.com\r\n" ;
String smtp_connection_2 = "MAIL FROM: <IoT_Remote_Module_Somewhere_in_the_World@cosmos.universe> BODY=8BITMIME\r\n" ; //whatever address
String smtp_connection_3 = "RCPT TO: <my_name@mySMTPserver.com>\r\n" ; //This needs to be an actual e-mail address on the SMTP server that you are sending the EHLO message
String smtp_connection_4 = "DATA\r\n" ;
String emailFrom_IoT = "From: <IoT_Module@outerspace.cosmos.universe>\r\n" ; // whatever address
String emailTo_IoT = "To: <IoT_Authentication_Control@galaxy.cosmos.universe>\r\n" ; // whatever address
String emailSubject_IoT = "Subject: >< " + localSUBJECT + " ><\r\nDATE:\r\n\r\n" ; // DATE: does not need to exist since recepient's server will add local DT stamp
String body_message = "This is an automated IoT e-mail from EK-TM4C1294XL module..\r\n\r\nPlease check whatever you need to check..\r\n"; //At least two .. (dots) are needed
// at the end of each sentence of the body message, so one ESCAPES the other.
String smtp_connection_5 = "\r\n.\r\n"; // This one sends you message to mail server's SMTP queue and ends entries, waiting either for new session or QUIT
String smtp_connection_6 = "QUIT\r\n";
String emailcontent = "";
emailcontent = emailFrom_IoT + emailTo_IoT + emailSubject_IoT + body_message ;
Serial.println(emailcontent);
while(1)
{
mywatchdogfeed();
emailIMEI.connect("smtp.gmail.com", 25, 360000) ; // use any other e-mail smtp server that you may actually use for your e-mail service
if ( emailIMEI.connected() )
{
Serial.println("Connected to GMAIL Mail Server");
Serial.println(" ");
memset(input_buffer, 0, sizeof(input_buffer));
memset(out_buffer, 0, sizeof(out_buffer));
q = smtp_connection_1.length();
inbound = "";
for(int i = 0 ; i < q ; i++)
{
l = (char) smtp_connection_1.charAt(i);
out_buffer[i] = l;
}
emailIMEI.write(out_buffer, q);
emailIMEI.flush();
delay(200);
emailIMEI.read(input_buffer, 512);
for (int i = 0 ; i <= 512 ; i++)
{
inbound += char(input_buffer[i]) ;
if ( input_buffer[i] == '\0')
{
break;
}
}
Serial.println(inbound);
Serial.println(" ");
if ( inbound.length() == 0)
{
Serial.println("EHLO message did not get a respond from server.");
break;
}
/////////////////////////
memset(input_buffer, 0, sizeof(input_buffer));
memset(out_buffer, 0, sizeof(out_buffer));
q = smtp_connection_2.length();
inbound = "";
for(int i = 0 ; i < q ; i++)
{
l = (char) smtp_connection_2.charAt(i);
out_buffer[i] = l;
}
emailIMEI.write(out_buffer, q);
emailIMEI.flush();
delay(200);
emailIMEI.read(input_buffer, 512);
for (int i = 0 ; i <= 512 ; i++)
{
inbound += char(input_buffer[i]) ;
if ( input_buffer[i] == '\0')
{
break;
}
}
Serial.println(inbound);
Serial.println(" ");
if ( inbound.length() == 0)
{
Serial.println("FROM: message did not get a respond from server.");
break;
}
/////////////////////////
memset(input_buffer, 0, sizeof(input_buffer));
memset(out_buffer, 0, sizeof(out_buffer));
q = smtp_connection_3.length();
inbound = "";
for(int i = 0 ; i < q ; i++)
{
l = (char) smtp_connection_3.charAt(i);
out_buffer[i] = l;
}
emailIMEI.write(out_buffer, q);
emailIMEI.flush();
delay(200);
emailIMEI.read(input_buffer, 512);
for (int i = 0 ; i <= 512 ; i++)
{
inbound += char(input_buffer[i]) ;
if ( input_buffer[i] == '\0')
{
break;
}
}
Serial.println(inbound);
Serial.println(" ");
if ( inbound.length() == 0)
{
Serial.println("RCPT TO: message did not get a respond from server.");
break;
}
/////////////////////////
memset(input_buffer, 0, sizeof(input_buffer));
memset(out_buffer, 0, sizeof(out_buffer));
q = smtp_connection_4.length();
inbound = "";
for(int i = 0 ; i < q ; i++)
{
l = (char) smtp_connection_4.charAt(i);
out_buffer[i] = l;
}
emailIMEI.write(out_buffer, q);
emailIMEI.flush();
delay(200);
emailIMEI.read(input_buffer, 512);
for (int i = 0 ; i <= 512 ; i++)
{
inbound += char(input_buffer[i]) ;
if ( input_buffer[i] == '\0')
{
break;
}
}
Serial.println(inbound);
Serial.println(" ");
if ( inbound.length() == 0)
{
Serial.println("DATA: message did not get a respond from server.");
break;
}
/////////////////////////
memset(input_buffer, 0, sizeof(input_buffer));
memset(out_buffer, 0, sizeof(out_buffer));
q = emailcontent.length();
inbound = "";
for(int i = 0 ; i < q ; i++)
{
l = (char) emailcontent.charAt(i);
out_buffer[i] = l;
}
emailIMEI.write(out_buffer, q);
emailIMEI.flush();
delay(200);
emailIMEI.read(input_buffer, 512);
for (int i = 0 ; i <= 512 ; i++)
{
inbound += char(input_buffer[i]) ;
if ( input_buffer[i] == '\0')
{
break;
}
}
Serial.println(inbound);
Serial.println(" ");
if ( inbound.length() == 0)
{
Serial.println("");
break;
}
/////////////////////////
memset(input_buffer, 0, sizeof(input_buffer));
memset(out_buffer, 0, sizeof(out_buffer));
q = smtp_connection_5.length();
inbound = "";
for(int i = 0 ; i < q ; i++)
{
l = (char) smtp_connection_5.charAt(i);
out_buffer[i] = l;
}
emailIMEI.write(out_buffer, q);
emailIMEI.flush();
delay(200);
emailIMEI.read(input_buffer, 512);
for (int i = 0 ; i <= 512 ; i++)
{
inbound += char(input_buffer[i]) ;
if ( input_buffer[i] == '\0')
{
break;
}
}
Serial.println(inbound);
Serial.println(" ");
if ( inbound.length() == 0)
{
Serial.println("Closing Command did not get a respond from server.");
break;
}
/////////////////////////
memset(input_buffer, 0, sizeof(input_buffer));
memset(out_buffer, 0, sizeof(out_buffer));
q = smtp_connection_6.length();
inbound = "";
for(int i = 0 ; i < q ; i++)
{
l = (char) smtp_connection_6.charAt(i);
out_buffer[i] = l;
}
emailIMEI.write(out_buffer, q);
emailIMEI.flush();
emailIMEI.read(input_buffer, 512);
for (int i = 0 ; i <= 512 ; i++)
{
inbound += char(input_buffer[i]) ;
if ( input_buffer[i] == '\0')
{
break;
}
}
Serial.println(inbound);
Serial.println(" ");
if ( inbound.length() == 0)
{
Serial.println("QUIT Command did not get a respond from server.");
break;
}
}
else
{
Serial.println("Can not Connect to GMAIL Mail Server");
Serial.println(" ");
break;
}
emailIMEI.stop();
Serial.println("Client emailIMEI closed");
Serial.println(" ");
return 1;
}
}