diff --git a/sonoff/WiFiClientSecureLightBearSSL.h b/sonoff/WiFiClientSecureLightBearSSL.h index 653c75502..5dd0df35e 100644 --- a/sonoff/WiFiClientSecureLightBearSSL.h +++ b/sonoff/WiFiClientSecureLightBearSSL.h @@ -24,7 +24,7 @@ #ifndef wificlientlightbearssl_h #define wificlientlightbearssl_h -#ifdef USE_MQTT_TLS +#if defined(USE_MQTT_TLS) || defined (USE_SENDMAIL) #include #include "WiFiClient.h" #include diff --git a/sonoff/sendemail.h b/sonoff/sendemail.h index 4b911c266..2422ad91f 100755 --- a/sonoff/sendemail.h +++ b/sonoff/sendemail.h @@ -31,7 +31,7 @@ class SendEmail int base64_encode(char *output, const char *input, int inputLen); public: SendEmail(const String& host, const int port, const String& user, const String& passwd, const int timeout, const int auth_used); - bool send(const String& from, const String& to, const String& subject, const String& msg); + bool send(const String& from, const String& to, const String& subject, const char *msg); ~SendEmail() {client->stop(); delete client;} }; diff --git a/sonoff/sendemail.ino b/sonoff/sendemail.ino index 3d4f59159..4a54c85e0 100755 --- a/sonoff/sendemail.ino +++ b/sonoff/sendemail.ino @@ -5,17 +5,183 @@ // enable serial debugging //#define DEBUG_EMAIL_PORT Serial +//SendEmail(const String& host, const int port, const String& user, const String& passwd, const int timeout, const bool ssl); +//SendEmail::send(const String& from, const String& to, const String& subject, const String& msg) +// sendmail [server:port:user:passwd:from:to:subject] data +// sendmail [*:*:*:*:*:to:subject] data uses defines from user_config +// sendmail currently only works with core 2.4.2 +//HW Watchdog 8.44 sec. +//SW Watchdog 3.2 sec. + +#define SEND_MAIL_MINRAM 12*1024 + +uint16_t SendMail(char *buffer) { + uint16_t count; + char *params,*oparams; + char *mserv; + uint16_t port; + char *user; + char *pstr; + char *passwd; + char *from; + char *to; + char *subject; + char *cmd; + char secure=0,auth=0; + uint16_t status=1; + SendEmail *mail=0; + uint16_t blen; + char *endcmd; + + //DebugFreeMem(); + +// return if not enough memory + uint16_t mem=ESP.getFreeHeap(); + if (memsend(from,to,subject,cmd); + delete mail; + if (result==true) status=0; + } + +exit: + if (oparams) free(oparams); + return status; +} + + + + SendEmail::SendEmail(const String& host, const int port, const String& user, const String& passwd, const int timeout, const int auth_used) : host(host), port(port), user(user), passwd(passwd), timeout(timeout), ssl(ssl), auth_used(auth_used), client(new BearSSL::WiFiClientSecure_light(1024,1024)) { } - - -String SendEmail::readClient() -{ +String SendEmail::readClient() { + delay(0); String r = client->readStringUntil('\n'); + r.trim(); while (client->available()) { delay(0); @@ -26,12 +192,15 @@ String SendEmail::readClient() //void SetSerialBaudrate(int baudrate); -bool SendEmail::send(const String& from, const String& to, const String& subject, const String& msg) +bool SendEmail::send(const String& from, const String& to, const String& subject, const char *msg) { - if (!host.length()) - { - return false; +bool status=false; +String buffer; + + if (!host.length()) { + return status; } + client->setTimeout(timeout); // smtp connect #ifdef DEBUG_EMAIL_PORT @@ -42,39 +211,22 @@ bool SendEmail::send(const String& from, const String& to, const String& subject DEBUG_EMAIL_PORT.println(port); #endif -#if defined(ARDUINO_ESP8266_RELEASE_2_3_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_2) -#else - //client->setInsecure(); - /* - bool mfln = client->probeMaxFragmentLength(host.c_str(), port, 512); -#ifdef DEBUG_EMAIL_PORT - DEBUG_EMAIL_PORT.printf("MFLN supported: %s\n", mfln ? "yes" : "no"); -#endif - if (mfln) { - client->setBufferSizes(512, 512); - }*/ -#endif - - - if (!client->connect(host.c_str(), port)) - { + if (!client->connect(host.c_str(), port)) { #ifdef DEBUG_EMAIL_PORT DEBUG_EMAIL_PORT.println("Connection failed: "); //DEBUG_EMAIL_PORT.println (client->getLastSSLError()); #endif - return false; + goto exit; } - String buffer = readClient(); + buffer = readClient(); #ifdef DEBUG_EMAIL_PORT DEBUG_EMAIL_PORT.println(buffer); #endif - if (!buffer.startsWith(F("220"))) - { - return false; + if (!buffer.startsWith(F("220"))) { + goto exit; } - buffer = F("EHLO "); buffer += client->localIP().toString(); @@ -86,58 +238,14 @@ bool SendEmail::send(const String& from, const String& to, const String& subject #ifdef DEBUG_EMAIL_PORT DEBUG_EMAIL_PORT.println(buffer); #endif - if (!buffer.startsWith(F("250"))) - { - return false; + if (!buffer.startsWith(F("250"))) { + goto exit; } - if (user.length()>0 && passwd.length()>0 ) - { + if (user.length()>0 && passwd.length()>0 ) { //buffer = F("STARTTLS"); //client->println(buffer); -if (auth_used==1) { - // plain -#ifdef USE_PLAIN - buffer = F("AUTH PLAIN"); - client->println(buffer); -#ifdef DEBUG_EMAIL_PORT - DEBUG_EMAIL_PORT.println(buffer); -#endif - buffer = readClient(); -#ifdef DEBUG_EMAIL_PORT - DEBUG_EMAIL_PORT.println(buffer); -#endif - if (!buffer.startsWith(F("334"))) - { - return false; - } - - char plainAuth[100]; - memset(plainAuth,sizeof(plainAuth),0); - plainAuth[0] = '\0'; - strcpy(&plainAuth[1], user.c_str()); - strcpy(&plainAuth[2+user.length()],passwd.c_str()); - const char* pA = (const char*)&plainAuth; - char buf[100]; - base64_encode(buf, pA, user.length()+passwd.length()+2); - client->println(buf); - -#ifdef DEBUG_EMAIL_PORT - DEBUG_EMAIL_PORT.println(buf); -#endif - buffer = readClient(); -#ifdef DEBUG_EMAIL_PORT - DEBUG_EMAIL_PORT.println(buffer); -#endif - if (!buffer.startsWith(F("235"))) - { - return false; - } -#endif - -} else { - buffer = F("AUTH LOGIN"); client->println(buffer); #ifdef DEBUG_EMAIL_PORT @@ -149,7 +257,7 @@ if (auth_used==1) { #endif if (!buffer.startsWith(F("334"))) { - return false; + goto exit; } base64 b; //buffer = user; @@ -165,9 +273,8 @@ if (auth_used==1) { #ifdef DEBUG_EMAIL_PORT DEBUG_EMAIL_PORT.println(buffer); #endif - if (!buffer.startsWith(F("334"))) - { - return false; + if (!buffer.startsWith(F("334"))) { + goto exit; } //buffer = this->passwd; //buffer = b.encode(buffer); @@ -181,12 +288,9 @@ if (auth_used==1) { #ifdef DEBUG_EMAIL_PORT DEBUG_EMAIL_PORT.println(buffer); #endif - if (!buffer.startsWith(F("235"))) - { - return false; + if (!buffer.startsWith(F("235"))) { + goto exit; } -} - } // smtp send mail @@ -200,9 +304,8 @@ if (auth_used==1) { #ifdef DEBUG_EMAIL_PORT DEBUG_EMAIL_PORT.println(buffer); #endif - if (!buffer.startsWith(F("250"))) - { - return false; + if (!buffer.startsWith(F("250"))) { + goto exit; } buffer = F("RCPT TO:"); buffer += to; @@ -214,12 +317,10 @@ if (auth_used==1) { #ifdef DEBUG_EMAIL_PORT DEBUG_EMAIL_PORT.println(buffer); #endif - if (!buffer.startsWith(F("250"))) - { - return false; + if (!buffer.startsWith(F("250"))) { + goto exit; } - buffer = F("DATA"); client->println(buffer); #ifdef DEBUG_EMAIL_PORT @@ -229,9 +330,8 @@ if (auth_used==1) { #ifdef DEBUG_EMAIL_PORT DEBUG_EMAIL_PORT.println(buffer); #endif - if (!buffer.startsWith(F("354"))) - { - return false; + if (!buffer.startsWith(F("354"))) { + goto exit; } buffer = F("From: "); buffer += from; @@ -252,59 +352,24 @@ if (auth_used==1) { #ifdef DEBUG_EMAIL_PORT DEBUG_EMAIL_PORT.println(buffer); #endif - buffer = msg; - client->println(buffer); + + client->println(msg); client->println('.'); #ifdef DEBUG_EMAIL_PORT - DEBUG_EMAIL_PORT.println(buffer); + DEBUG_EMAIL_PORT.println(msg); #endif + buffer = F("QUIT"); client->println(buffer); #ifdef DEBUG_EMAIL_PORT DEBUG_EMAIL_PORT.println(buffer); #endif - return true; + + status=true; +exit: + + return status; } -#ifdef USE_PLAIN -void SendEmail::a3_to_a4(unsigned char * a4, unsigned char * a3) { - a4[0] = (a3[0] & 0xfc) >> 2; - a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4); - a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6); - a4[3] = (a3[2] & 0x3f); -} - -int SendEmail::base64_encode(char *output, const char *input, int inputLen) { - const char* _b64_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - int i = 0, j = 0; - int encLen = 0; - unsigned char a3[3]; - unsigned char a4[4]; - while(inputLen--) { - a3[i++] = *(input++); - if(i == 3) { - a3_to_a4(a4, a3); - for(i = 0; i < 4; i++) { - output[encLen++] = _b64_alphabet[a4[i]]; - } - i = 0; - } - } - if(i) { - for(j = i; j < 3; j++) { - a3[j] = '\0'; - } - a3_to_a4(a4, a3); - for(j = 0; j < i + 1; j++) { - output[encLen++] = _b64_alphabet[a4[j]]; - } - while((i++ < 3)) { - output[encLen++] = '='; - } - } - output[encLen] = '\0'; - return encLen; -} -#endif #endif // USE_SENDMAIL diff --git a/sonoff/xdrv_01_webserver.ino b/sonoff/xdrv_01_webserver.ino index 009f0fa39..04af1906f 100644 --- a/sonoff/xdrv_01_webserver.ino +++ b/sonoff/xdrv_01_webserver.ino @@ -2450,152 +2450,6 @@ String UrlEncode(const String& text) return encoded; } -#ifdef USE_SENDMAIL - -#include "sendemail.h" - -//SendEmail(const String& host, const int port, const String& user, const String& passwd, const int timeout, const bool ssl); -//SendEmail::send(const String& from, const String& to, const String& subject, const String& msg) -// sendmail [server:port:user:passwd:from:to:subject] data -// sendmail [*:*:*:*:*:to:subject] data uses defines from user_config -// sendmail currently only works with core 2.4.2 - - -#define SEND_MAIL_MINRAM 12*1024 - -uint16_t SendMail(char *buffer) { - uint16_t count; - char *params,*oparams; - char *mserv; - uint16_t port; - char *user; - char *pstr; - char *passwd; - char *from; - char *to; - char *subject; - char *cmd; - char secure=0,auth=0; - uint16_t status=1; - SendEmail *mail=0; - - //DebugFreeMem(); - -// return if not enough memory - uint16_t mem=ESP.getFreeHeap(); - if (mem AUTH LOGIN 1 => PLAIN LOGIN - // 2 seconds timeout - #define MAIL_TIMEOUT 2000 - mail = new SendEmail(mserv, port,user,passwd, MAIL_TIMEOUT, auth); - -#ifdef EMAIL_FROM - if (*from=='*') { - from=(char*)EMAIL_FROM; - } -#endif - -exit: - if (mail) { - bool result=mail->send(from,to,subject,cmd); - delete mail; - if (result==true) status=0; - } - - - if (oparams) free(oparams); - return status; -} - -#endif - int WebSend(char *buffer) { // [sonoff] POWER1 ON --> Sends http://sonoff/cm?cmnd=POWER1 ON