refactor sendmail

This commit is contained in:
gemu2015 2019-10-18 11:33:52 +02:00
parent b0a64b9147
commit d989abfb45
4 changed files with 207 additions and 288 deletions

View File

@ -24,7 +24,7 @@
#ifndef wificlientlightbearssl_h
#define wificlientlightbearssl_h
#ifdef USE_MQTT_TLS
#if defined(USE_MQTT_TLS) || defined (USE_SENDMAIL)
#include <vector>
#include "WiFiClient.h"
#include <t_bearssl.h>

View File

@ -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;}
};

View File

@ -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 (mem<SEND_MAIL_MINRAM) {
return 4;
}
while (*buffer==' ') buffer++;
if (*buffer!='[') {
goto exit;
}
buffer++;
endcmd=strchr(buffer,']');
if (!endcmd) {
goto exit;
}
// copy params
blen=(uint32_t)endcmd-(uint32_t)buffer;
oparams=(char*)calloc(blen+2,1);
if (!oparams) return 4;
params=oparams;
strncpy(oparams,buffer,blen+2);
oparams[blen]=0;
cmd=endcmd+1;
#ifdef DEBUG_EMAIL_PORT
SetSerialBaudrate(115200);
DEBUG_EMAIL_PORT.print("mailsize: ");
DEBUG_EMAIL_PORT.println(blen);
#endif
mserv=strtok(params,":");
if (!mserv) {
goto exit;
}
// port
pstr=strtok(NULL,":");
if (!pstr) {
goto exit;
}
#ifdef EMAIL_PORT
if (*pstr=='*') {
port=EMAIL_PORT;
} else {
port=atoi(pstr);
}
#else
port=atoi(pstr);
#endif
user=strtok(NULL,":");
if (!user) {
goto exit;
}
passwd=strtok(NULL,":");
if (!passwd) {
goto exit;
}
from=strtok(NULL,":");
if (!from) {
goto exit;
}
to=strtok(NULL,":");
if (!to) {
goto exit;
}
subject=strtok(NULL,"]");
if (!subject) {
goto exit;
}
#ifdef EMAIL_USER
if (*user=='*') {
user=(char*)EMAIL_USER;
}
#endif
#ifdef EMAIL_PASSWORD
if (*passwd=='*') {
passwd=(char*)EMAIL_PASSWORD;
}
#endif
#ifdef EMAIL_SERVER
if (*mserv=='*') {
mserv=(char*)EMAIL_SERVER;
}
#endif //USE_SENDMAIL
#ifdef DEBUG_EMAIL_PORT
DEBUG_EMAIL_PORT.println(mserv);
DEBUG_EMAIL_PORT.println(port);
DEBUG_EMAIL_PORT.println(user);
DEBUG_EMAIL_PORT.println(passwd);
#endif
// 2 seconds timeout
#define MAIL_TIMEOUT 500
mail = new SendEmail(mserv,port,user,passwd, MAIL_TIMEOUT, auth);
#ifdef EMAIL_FROM
if (*from=='*') {
from=(char*)EMAIL_FROM;
}
#endif
#ifdef DEBUG_EMAIL_PORT
DEBUG_EMAIL_PORT.println(from);
DEBUG_EMAIL_PORT.println(to);
DEBUG_EMAIL_PORT.println(subject);
DEBUG_EMAIL_PORT.println(cmd);
#endif
if (mail) {
bool result=mail->send(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

View File

@ -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<SEND_MAIL_MINRAM) {
return 4;
}
while (*buffer==' ') buffer++;
// copy params
oparams=(char*)calloc(strlen(buffer)+2,1);
if (!oparams) return 4;
params=oparams;
strcpy(params,buffer);
if (*params=='p') {
auth=1;
params++;
}
if (*params!='[') {
goto exit;
}
params++;
mserv=strtok(params,":");
if (!mserv) {
goto exit;
}
// port
pstr=strtok(NULL,":");
if (!pstr) {
goto exit;
}
#ifdef EMAIL_PORT
if (*pstr=='*') {
port=EMAIL_PORT;
} else {
port=atoi(pstr);
}
#else
port=atoi(pstr);
#endif
user=strtok(NULL,":");
if (!user) {
goto exit;
}
passwd=strtok(NULL,":");
if (!passwd) {
goto exit;
}
from=strtok(NULL,":");
if (!from) {
goto exit;
}
to=strtok(NULL,":");
if (!to) {
goto exit;
}
subject=strtok(NULL,"]");
if (!subject) {
goto exit;
}
cmd=subject+strlen(subject)+1;
#ifdef EMAIL_USER
if (*user=='*') {
user=(char*)EMAIL_USER;
}
#endif
#ifdef EMAIL_PASSWORD
if (*passwd=='*') {
passwd=(char*)EMAIL_PASSWORD;
}
#endif
#ifdef EMAIL_SERVER
if (*mserv=='*') {
mserv=(char*)EMAIL_SERVER;
}
#endif //USE_SENDMAIL
// auth = 0 => 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