-->
Page 1 of 2

SMTP?

PostPosted: Sun Dec 14, 2014 12:48 pm
by mct75
Has anyone messed with SMTP from the ESP8266 yet? I want to set up some ESP8266's for home monitoring. My ISP blocks inbound port 80, but SMTP via a local relay works fine. It sounds like it would be as simple as opening a connection on port 25 and sending basic text strings, right up the alley of the ESP8266. I've just began playing with my ESP8266, but soon I will give it a shot. I'm going to try to bang it out with an Arduino, but a library would be nice.

Re: SMTP?

PostPosted: Mon Dec 15, 2014 4:26 am
by RogerClark
I've done this using other Wifi modules but not on the ESP8266 yet.

As long as you connect to your ISP's outbound mail server, there should be no problems in sending email.

I'm not sure if its much use, but this is the code I wrote to send email from a Arduino to a UART based wifi module

You can probably work out the flow...


Code: Select allint UARTWifi::sendEmail(char *toAddress,char *fromAddress,char *toFriendlyName,char *subject,char *message,char *loginDomain,char *mailServer)
{
char dataBuf[256];
int theSocket;
int responseCode = socketCreate("0","0",mailServer,"25",&theSocket);// 0=client,0=tcp,mailserver,port 25 for smtp, var into which the socket num is returned
if (responseCode==0)
{
#if DEBUG_LEVEL > 2
Serial.print(F("Socket created was "));//Serial.println(theSocket);
#endif
delay(250);
socketReceive(dataBuf,sizeof(dataBuf),theSocket);
//Serial.print(F("socketReceive got "));//Serial.println(dataBuf);
if (strncmp(dataBuf,"220",3)==0)
{
//Serial.println(F("Connection to SMTP server OK"));
strcpy(dataBuf,"HELO ");
strcat(dataBuf,loginDomain);
strcat(dataBuf,"\r\n");
socketSend(dataBuf,strlen(dataBuf),theSocket);
//Serial.println("waiting for 250 message");
while (socketReceive(dataBuf,sizeof(dataBuf),theSocket)==0)
{
//Serial.println(F("No data yet;-)"));
delay(SOCKET_RECEIVE_RETRY_TIME);
}
//Serial.print("Response was ");//Serial.println(dataBuf);
if (strncmp_P(dataBuf,PSTR("250"),3)==0)
{
//Serial.println("HELO message was OK");
strcpy_P(dataBuf,PSTR("MAIL FROM: "));
strcat(dataBuf,fromAddress);
strcat_P(dataBuf,PSTR("\r\n"));
socketSend(dataBuf,strlen(dataBuf),theSocket);
while (socketReceive(dataBuf,sizeof(dataBuf),theSocket)==0)
{
//Serial.println(F("Wait socketReceive retry"));
delay(SOCKET_RECEIVE_RETRY_TIME);
}
//Serial.print(F("socketReceive got "));//Serial.println(dataBuf);
}
if (strncmp_P(dataBuf,PSTR("250"),3)==0)
{
//Serial.println(F("MAIL FROM message was OK"));
strcpy_P(dataBuf,PSTR("RCPT TO: "));
strcat(dataBuf,toAddress);
strcat_P(dataBuf,PSTR("\r\n"));
socketSend(dataBuf,strlen(dataBuf),theSocket);
while (socketReceive(dataBuf,sizeof(dataBuf),theSocket)==0)
{
//Serial.println(F("Wait socketReceive retry"));
delay(SOCKET_RECEIVE_RETRY_TIME);// if there wasn't any data, wait and try again
}
//Serial.print(F("socketReceive 1 got "));
//Serial.println(dataBuf);
if (strncmp(dataBuf,"250",3)==0)
{
//Serial.println(F("RCPT TO message was OK"));
strcpy_P(dataBuf,PSTR("DATA\r\n"));
socketSend(dataBuf,strlen(dataBuf),theSocket);
while (socketReceive(dataBuf,sizeof(dataBuf),theSocket)==0)
{
//Serial.println(F("Wait socketReceive retry"));
delay(SOCKET_RECEIVE_RETRY_TIME);// if there wasn't any data, wait and try again
}
//Serial.print(F("socketReceive got "));//Serial.println(dataBuf);
if (strncmp(dataBuf,"354",3)==0)
{
//Serial.println(F("DATA message was OK"));
strcpy_P(dataBuf,PSTR("SUBJECT: "));
strcat(dataBuf,subject);
strcat_P(dataBuf,PSTR("\r\n"));
socketSend(dataBuf,strlen(dataBuf),theSocket);
strcpy_P(dataBuf,PSTR("FROM: "));
strcat(dataBuf,toFriendlyName);
strcat_P(dataBuf,PSTR(" <"));
strcat(dataBuf,fromAddress);
strcat_P(dataBuf,PSTR(">\r\n"));
socketSend(dataBuf,strlen(dataBuf),theSocket);
strcpy_P(dataBuf,PSTR("To: "));
strcat(dataBuf,toAddress);
strcat_P(dataBuf,PSTR("\r\n")) ;
socketSend(dataBuf,strlen(dataBuf),theSocket);
socketSend(message,strlen(message),theSocket);
strcpy_P(dataBuf,PSTR("\r\n.\r\n"));// termination string
socketSend(dataBuf,strlen(dataBuf),theSocket);
while (socketReceive(dataBuf,sizeof(dataBuf),theSocket)==0)
{
//Serial.println("Wait socketReceive retry");
delay(SOCKET_RECEIVE_RETRY_TIME);// if there wasn't any data, wait and try again
}
//Serial.print(F("socketReceive got"));Serial.println(dataBuf);
if (strncmp_P(dataBuf,PSTR("250"),3)==0)
{
strcpy_P(dataBuf,PSTR("quit\r\n"));
socketSend(dataBuf,strlen(dataBuf),theSocket);
while (socketReceive(dataBuf,sizeof(dataBuf),theSocket)==0)
{
//Serial.println(F("Wait socketReceive retry"));
delay(SOCKET_RECEIVE_RETRY_TIME);// if there wasn't any data, wait 500ms and try again
}
}
else
{
socketClose(theSocket);
return -250;
}
}
else
{
socketClose(theSocket);
return -354;
}
}
else
{
socketClose(theSocket);
return -250;
}
}
else
{
socketClose(theSocket);
return -250;
}
}
socketClose(theSocket);
return 0;
}
else
{
//Serial.println(responseCode);
return responseCode;
}
}

Re: SMTP?

PostPosted: Mon Dec 15, 2014 7:27 am
by Erni
I have done it using my Arduino Yun, but I had to install support for SSL first, not sure how you could do that on a esp8266.
Another option is to use an external service like pushingbox:
viewtopic.php?f=19&t=691

or if you have your own server, make a php script using sendmail or similar

Re: SMTP?

PostPosted: Mon Dec 15, 2014 11:24 am
by mct75
I actually have a local SMTP relay that takes unauthenticated traffic (from the same subnet only) and then connects to Gmail's SMTP server using TLS. So no worries about authentication or encryption.

Thanks Roger, for the code!