diff --git a/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.cpp b/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.cpp index 85cb0663e..687661ac9 100755 --- a/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.cpp +++ b/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.cpp @@ -654,7 +654,7 @@ bool ESP32_MailClient::readMail(IMAPData &imapData) { _sdOk = sdTest(); if (_sdOk) - if (!SD.exists(imapData._savePath.c_str())) + if (!imapData.fsp->exists(imapData._savePath.c_str())) createDirs(imapData._savePath); } else if (imapData._storageType == MailClientStorageType::SPIFFS) @@ -1788,6 +1788,7 @@ bool ESP32_MailClient::sendMail(SMTPData &smtpData) else { +/* if (!_sdOk) { if (smtpData._storageType == MailClientStorageType::SD) @@ -1798,13 +1799,18 @@ bool ESP32_MailClient::sendMail(SMTPData &smtpData) if (!_sdOk) continue; - +*/ bool file_existed = false; +/* if (smtpData._storageType == MailClientStorageType::SD) file_existed = SD.exists(smtpData._attach._filename[i].c_str()); else if (smtpData._storageType == MailClientStorageType::SPIFFS) file_existed = SPIFFS.exists(smtpData._attach._filename[i].c_str()); + else if (smtpData._storageType == MailClientStorageType::FFat) + file_existed = FFat.exists(smtpData._attach._filename[i].c_str()); +*/ + file_existed = smtpData.fsp->exists(smtpData._attach._filename[i].c_str()); if (file_existed) { smtpData._cbData._info = smtpData._attach._filename[i]; @@ -1820,10 +1826,15 @@ bool ESP32_MailClient::sendMail(SMTPData &smtpData) smtpData._net->getStreamPtr()->print(buf.c_str()); File file; + /* if (smtpData._storageType == MailClientStorageType::SD) file = SD.open(smtpData._attach._filename[i].c_str(), FILE_READ); else if (smtpData._storageType == MailClientStorageType::SPIFFS) file = SPIFFS.open(smtpData._attach._filename[i].c_str(), FILE_READ); + else if (smtpData._storageType == MailClientStorageType::FFat) + file = FFat.open(smtpData._attach._filename[i].c_str(), FILE_READ); + */ + file = smtpData.fsp->open(smtpData._attach._filename[i].c_str(), FILE_READ); send_base64_encode_mime_file(smtpData._net->getStreamPtr(), file); smtpData._net->getStreamPtr()->print(ESP32_MAIL_STR_34); @@ -2036,6 +2047,7 @@ bool ESP32_MailClient::sdBegin(uint8_t sck, uint8_t miso, uint8_t mosi, uint8_t _mosi = mosi; _ss = ss; _sdConfigSet = true; + SPI.begin(_sck, _miso, _mosi, _ss); return SD.begin(_ss, SPI); } @@ -2331,7 +2343,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT delete[] midx; if (imapData._storageType == MailClientStorageType::SD) - if (!SD.exists(filepath.c_str())) + if (!imapData.fsp->exists(filepath.c_str())) createDirs(filepath); if (!imapData._headerSaved) @@ -2353,7 +2365,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT } if (imapData._storageType == MailClientStorageType::SD) - file = SD.open(filepath.c_str(), FILE_WRITE); + file = imapData.fsp->open(filepath.c_str(), FILE_WRITE); else if (imapData._storageType == MailClientStorageType::SPIFFS) file = SPIFFS.open(filepath.c_str(), FILE_WRITE); } @@ -2922,7 +2934,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT delete[] midx; if (imapData._storageType == MailClientStorageType::SD) - if (!SD.exists(filepath.c_str())) + if (!imapData.fsp->exists(filepath.c_str())) createDirs(filepath); if (!imapData._headerSaved) @@ -2944,7 +2956,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT } if (imapData._storageType == MailClientStorageType::SD) - file = SD.open(filepath.c_str(), FILE_WRITE); + file = imapData.fsp->open(filepath.c_str(), FILE_WRITE); else if (imapData._storageType == MailClientStorageType::SPIFFS) file = SPIFFS.open(filepath.c_str(), FILE_WRITE); } @@ -3003,7 +3015,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT delete[] midx; if (imapData._storageType == MailClientStorageType::SD) - if (!SD.exists(filepath.c_str())) + if (!imapData.fsp->exists(filepath.c_str())) createDirs(filepath); filepath += ESP32_MAIL_STR_202; @@ -3011,7 +3023,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT filepath += imapData._messageDataInfo[mailIndex][messageDataIndex]._filename; if (imapData._storageType == MailClientStorageType::SD) - file = SD.open(filepath.c_str(), FILE_WRITE); + file = imapData.fsp->open(filepath.c_str(), FILE_WRITE); else if (imapData._storageType == MailClientStorageType::SPIFFS) file = SPIFFS.open(filepath.c_str(), FILE_WRITE); } @@ -3167,7 +3179,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT { if (imapData._storageType == MailClientStorageType::SD) - file = SD.open(hpath.c_str(), FILE_WRITE); + file = imapData.fsp->open(hpath.c_str(), FILE_WRITE); else if (imapData._storageType == MailClientStorageType::SPIFFS) file = SPIFFS.open(hpath.c_str(), FILE_WRITE); @@ -3714,6 +3726,17 @@ void IMAPData::setFetchUID(const String &fetchUID) void IMAPData::setFileStorageType(uint8_t storageType) { _storageType = storageType; + switch (storageType) { + case MailClientStorageType::SPIFFS: + fsp = &SPIFFS; + break; + case MailClientStorageType::SD: + fsp = &SD; + break; + case MailClientStorageType::FFat: + fsp = &FFat; + break; + } } void IMAPData::setDownloadAttachment(bool download) @@ -4733,6 +4756,17 @@ void SMTPData::removeAttachFile(uint8_t index) void SMTPData::setFileStorageType(uint8_t storageType) { _storageType = storageType; + switch (storageType) { + case MailClientStorageType::SPIFFS: + fsp = &SPIFFS; + break; + case MailClientStorageType::SD: + fsp = &SD; + break; + case MailClientStorageType::FFat: + fsp = &FFat; + break; + } } void SMTPData::clearAttachData() diff --git a/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.h b/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.h index 943cd62f7..b6a8da775 100755 --- a/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.h +++ b/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.h @@ -48,6 +48,7 @@ #include "RFC2047.h" #include "ESP32MailHTTPClient.h" #include "ESP32TimeHelper.h" +#include #define FORMAT_SPIFFS_IF_FAILED true @@ -90,6 +91,7 @@ struct MailClientStorageType { static const uint8_t SPIFFS = 0; static const uint8_t SD = 1; + static const uint8_t FFat = 2; }; static const char ESP32_MAIL_STR_1[] PROGMEM = "Content-Type: multipart/mixed; boundary=\""; @@ -1302,6 +1304,7 @@ private: std::string _host = ""; uint16_t _port = 993; uint8_t _storageType = 1; + FS *fsp = &SD; bool _unseen = false; std::string _loginEmail = ""; std::string _loginPassword = ""; @@ -1859,7 +1862,7 @@ protected: string _host = ""; uint16_t _port = 0; uint8_t _storageType = 1; - + FS *fsp = &SD; string _fromName = ""; string _senderEmail = ""; string _subject = ""; diff --git a/tasmota/sendemail.ino b/tasmota/sendemail.ino index 0a4c4de0e..bc080bd7f 100644 --- a/tasmota/sendemail.ino +++ b/tasmota/sendemail.ino @@ -330,6 +330,12 @@ String buffer; if (!buffer.startsWith(F("354"))) { goto exit; } + + buffer = F("MIME-Version: 1.0\r\n"); + client->print(buffer); + buffer = F("Content-Type: Multipart/mixed; boundary=frontier\r\n"); + client->print(buffer); + buffer = F("From: "); buffer += from; client->println(buffer); @@ -350,6 +356,7 @@ String buffer; AddLog_P2(LOG_LEVEL_INFO, PSTR("%s"),buffer.c_str()); #endif + #ifdef USE_SCRIPT if (*msg=='*' && *(msg+1)==0) { g_client=client; @@ -378,9 +385,98 @@ exit: } void xsend_message_txt(char *msg) { - g_client->println(msg); -} +#ifdef DEBUG_EMAIL_PORT + AddLog_P2(LOG_LEVEL_INFO, PSTR("%s"),msg); +#endif +#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) + if (*msg=='@') { + msg++; + attach_File(msg); + } else if (*msg=='&') { + msg++; + attach_Array(msg); + } else { + g_client->print(F("--frontier\r\n")); + g_client->print(F("Content-Type: text/plain\r\n\r\n")); + g_client->println(msg); + g_client->print(F("\r\n--frontier\r\n")); + } #else + g_client->print(F("--frontier\r\n")); + g_client->print(F("Content-Type: text/plain\r\n\r\n")); + g_client->println(msg); + g_client->print(F("\r\n--frontier\r\n")); +#endif +} + +#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) +#include +extern FS *fsp; + +void attach_File(char *path) { + g_client->print(F("--frontier\r\n")); + g_client->print(F("Content-Type: text/plain\r\n")); + char buff[64]; + char *cp = path; + while (*cp=='/') cp++; + File file = fsp->open(path, "r"); + if (file) { + sprintf_P(buff,PSTR("Content-Disposition: attachment; filename=\"%s\"\r\n\r\n"), cp); + g_client->write(buff); + uint16_t flen = file.size(); + uint8_t fbuff[64]; + uint16_t blen = sizeof(fbuff); + while (flen>0) { + file.read(fbuff, blen); + flen -= blen; + g_client->write(fbuff, blen); + if (flenprint(F("\r\n\r\nfile not found!\r\n")); + } + g_client->print(F("\r\n--frontier\r\n")); +} + +float *get_array_by_name(char *name, uint16_t *alen); +void flt2char(float num, char *nbuff); + +void attach_Array(char *aname) { + float *array = 0; + uint16_t alen; + array = get_array_by_name(aname, &alen); + g_client->print(F("--frontier\r\n")); + g_client->print(F("Content-Type: text/plain\r\n")); + if (array && alen) { +#ifdef DEBUG_EMAIL_PORT + AddLog_P2(LOG_LEVEL_INFO, PSTR("array found %d"),alen); +#endif + char buff[64]; + sprintf_P(buff,PSTR("Content-Disposition: attachment; filename=\"%s.txt\"\r\n\r\n"), aname); + g_client->write(buff); + float *fp=array; + for (uint32_t cnt = 0; cntwrite(nbuff, strlen(nbuff)); + } + } else { + g_client->print(F("\r\n\r\narray not found!\r\n")); + } + g_client->print(F("\r\n--frontier\r\n")); +} + +#endif // defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) + +#else + /* * Created by K. Suwatchai (Mobizt) @@ -413,6 +509,9 @@ void script_send_email_body(void(*func)(char *)); #define xPSTR(a) a //The Email Sending data object contains config and data to send SMTPData smtpData; +#define MAX_ATTCHMENTS 8 +char *attachments[MAX_ATTCHMENTS]; +uint8_t num_attachments; //Callback function to get the Email sending status //void sendCallback(SendStatus info); @@ -435,7 +534,7 @@ uint16_t SendMail(char *buffer) { // return if not enough memory uint32_t mem=ESP.getFreeHeap(); - AddLog_P2(LOG_LEVEL_INFO, PSTR("heap: %d"),mem); + //AddLog_P2(LOG_LEVEL_INFO, PSTR("heap: %d"),mem); if (memprint(F("\r\n\r\narray not found!\r\n")); + } +} void send_message_txt(char *txt) { - if (*txt=='&') { + if (*txt=='@') { txt++; smtpData.addAttachFile(txt); + } else if (*txt=='&') { + txt++; + attach_Array(txt); } else if (*txt=='$') { txt++; #if defined(ESP32) && defined(USE_WEBCAM)