From dba5cf29024178a795eb46bdfd8e71bd9364acc3 Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Thu, 3 Jun 2021 16:17:41 +0200 Subject: [PATCH] fixes from GEMU2015 --- lib/lib_div/lib_mail/src/ESP_Mail_Client.cpp | 99 ++++++++++---------- lib/lib_div/lib_mail/src/ESP_Mail_Client.h | 16 +++- tasmota/sendemail_ESP32.ino | 35 ++++--- 3 files changed, 84 insertions(+), 66 deletions(-) diff --git a/lib/lib_div/lib_mail/src/ESP_Mail_Client.cpp b/lib/lib_div/lib_mail/src/ESP_Mail_Client.cpp index e2e4608e2..f1acdf53f 100755 --- a/lib/lib_div/lib_mail/src/ESP_Mail_Client.cpp +++ b/lib/lib_div/lib_mail/src/ESP_Mail_Client.cpp @@ -1,30 +1,30 @@ /** * Mail Client Arduino Library for Espressif's ESP32 and ESP8266 - * + * * Version: 1.2.0 * Released: May 17, 2021 * * Updates: * - Add support ESP8266 Core SDK v3.x.x. - * - * - * This library allows Espressif's ESP32 and ESP8266 devices to send and read Email + * + * + * This library allows Espressif's ESP32 and ESP8266 devices to send and read Email * through the SMTP and IMAP servers. - * + * * The MIT License (MIT) * Copyright (c) 2021 K. Suwatchai (Mobizt) - * - * + * + * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR @@ -2746,20 +2746,16 @@ bool ESP_Mail_Client::sendAttachments(SMTPSession *smtp, SMTP_Message *msg, cons else { - if (att->file.storage_type == esp_mail_file_storage_type_univ) { - - } else { - if (!_sdOk && att->file.storage_type == esp_mail_file_storage_type_sd) _sdOk = sdTest(); - if (!_flashOk && att->file.storage_type == esp_mail_file_storage_type_flash) { + if (!_flashOk && att->file.storage_type == esp_mail_file_storage_type_flash) #if defined(ESP32) - //_flashOk = ESP_MAIL_FLASH_FS.begin(FORMAT_FLASH); + _flashOk = ESP_MAIL_FLASH_FS.begin(FORMAT_FLASH); #elif defined(ESP8266) - //_flashOk = ESP_MAIL_FLASH_FS.begin(); + _flashOk = ESP_MAIL_FLASH_FS.begin(); #endif - } + if ((!_sdOk && att->file.storage_type == esp_mail_file_storage_type_sd) || (!_flashOk && att->file.storage_type == esp_mail_file_storage_type_flash)) { @@ -2812,13 +2808,12 @@ bool ESP_Mail_Client::openFileRead(SMTPSession *smtp, SMTP_Message *msg, SMTP_At if (att->file.storage_type == esp_mail_file_storage_type_sd) { file_existed = ESP_MAIL_SD_FS.exists(filepath.c_str()); - } - else if (att->file.storage_type == esp_mail_file_storage_type_flash) { - } - else if (att->file.storage_type == esp_mail_file_storage_type_univ) { + } else if (att->file.storage_type == esp_mail_file_storage_type_flash) { file_existed = ESP_MAIL_FLASH_FS.exists(filepath.c_str()); - } -} + } else if (att->file.storage_type == esp_mail_file_storage_type_univ) { + file_existed = ufsp->exists(filepath.c_str()); + } + if (!file_existed) { @@ -2832,12 +2827,11 @@ bool ESP_Mail_Client::openFileRead(SMTPSession *smtp, SMTP_Message *msg, SMTP_At if (att->file.storage_type == esp_mail_file_storage_type_sd) { file_existed = ESP_MAIL_SD_FS.exists(filepath.c_str()); - } - else if (att->file.storage_type == esp_mail_file_storage_type_flash) { - } - else if (att->file.storage_type == esp_mail_file_storage_type_univ) { + } else if (att->file.storage_type == esp_mail_file_storage_type_flash) { file_existed = ESP_MAIL_FLASH_FS.exists(filepath.c_str()); - } + } else if (att->file.storage_type == esp_mail_file_storage_type_univ) { + file_existed = ufsp->exists(filepath.c_str()); + } } if (!file_existed) @@ -2861,17 +2855,16 @@ bool ESP_Mail_Client::openFileRead(SMTPSession *smtp, SMTP_Message *msg, SMTP_At if (att->file.storage_type == esp_mail_file_storage_type_sd) { file = ESP_MAIL_SD_FS.open(filepath.c_str(), FILE_READ); - } - else if (att->file.storage_type == esp_mail_file_storage_type_flash) { - } + } else if (att->file.storage_type == esp_mail_file_storage_type_flash) { #if defined(ESP32) - //file = ESP_MAIL_FLASH_FS.open(filepath.c_str(), FILE_READ); + file = ESP_MAIL_FLASH_FS.open(filepath.c_str(), FILE_READ); #elif defined(ESP8266) - //file = ESP_MAIL_FLASH_FS.open(filepath.c_str(), "r"); + file = ESP_MAIL_FLASH_FS.open(filepath.c_str(), "r"); #endif - else if (att->file.storage_type == esp_mail_file_storage_type_univ) { + } else if (att->file.storage_type == esp_mail_file_storage_type_univ) { file = ufsp->open(filepath.c_str(), "r"); } + if (!file) return false; @@ -2904,10 +2897,13 @@ bool ESP_Mail_Client::openFileRead2(SMTPSession *smtp, SMTP_Message *msg, File & filepath += path; } - if (storageType == esp_mail_file_storage_type_sd) + if (storageType == esp_mail_file_storage_type_sd) { file_existed = ESP_MAIL_SD_FS.exists(filepath.c_str()); - else if (storageType == esp_mail_file_storage_type_flash) + } else if (storageType == esp_mail_file_storage_type_flash) { file_existed = ESP_MAIL_FLASH_FS.exists(filepath.c_str()); + } else if (storageType == esp_mail_file_storage_type_univ) { + file_existed = ufsp->exists(filepath.c_str()); + } if (!file_existed) { @@ -3034,18 +3030,16 @@ bool ESP_Mail_Client::sendInline(SMTPSession *smtp, SMTP_Message *msg, const std } else { - if (att->file.storage_type == esp_mail_file_storage_type_univ) { - } else { if (!_sdOk && att->file.storage_type == esp_mail_file_storage_type_sd) _sdOk = sdTest(); - if (!_flashOk && att->file.storage_type == esp_mail_file_storage_type_flash) { + if (!_flashOk && att->file.storage_type == esp_mail_file_storage_type_flash) #if defined(ESP32) - //_flashOk = ESP_MAIL_FLASH_FS.begin(FORMAT_FLASH); + _flashOk = ESP_MAIL_FLASH_FS.begin(FORMAT_FLASH); #elif defined(ESP8266) - //_flashOk = ESP_MAIL_FLASH_FS.begin(); + _flashOk = ESP_MAIL_FLASH_FS.begin(); #endif - } + if ((!_sdOk && att->file.storage_type == esp_mail_file_storage_type_sd) || (!_flashOk && att->file.storage_type == esp_mail_file_storage_type_flash)) { @@ -3934,14 +3928,14 @@ void ESP_Mail_Client::splitTk(std::string &str, std::vector &tk, co std::string().swap(s); } -/** Add the soft line break to the long text line (rfc 3676) +/** Add the soft line break to the long text line (rfc 3676) * and add Format=flowed parameter in the plain text content-type header. * We use the existing white space as a part of this soft line break * and set delSp="no" parameter to the header. - * + * * Some servers are not rfc 3676 compliant. * This causes the text lines are wrapped instead of joined. - * + * * Some mail clients trim the space before the line break * which makes the soft line break cannot be seen. */ @@ -6072,14 +6066,17 @@ void ESP_Mail_Client::saveHeader(IMAPSession *imap) if (file) file.close(); - if (imap->_storageType == esp_mail_file_storage_type_sd) + if (imap->_storageType == esp_mail_file_storage_type_sd) { file = ESP_MAIL_SD_FS.open(headerFilePath.c_str(), FILE_WRITE); - else if (imap->_storageType == esp_mail_file_storage_type_flash) + } else if (imap->_storageType == esp_mail_file_storage_type_flash) { #if defined(ESP32) file = ESP_MAIL_FLASH_FS.open(headerFilePath.c_str(), FILE_WRITE); #elif defined(ESP8266) file = ESP_MAIL_FLASH_FS.open(headerFilePath.c_str(), "w"); #endif + } else if (imap->_storageType == esp_mail_file_storage_type_univ) { + file = ufsp->open(headerFilePath.c_str(), "w"); + } if (file) { @@ -7613,9 +7610,9 @@ bool IMAPSession::closeSession() if (!_tcpConnected) return false; #if defined(ESP32) - /** + /** * The strange behavior in ESP8266 SSL client, BearSSLWiFiClientSecure - * The client disposed without memory released after the server close + * The client disposed without memory released after the server close * the connection due to LOGOUT command, which caused the memory leaks. */ if (!MailClient.imapLogout(this)) @@ -8454,9 +8451,9 @@ bool SMTPSession::closeSession() /* Sign out */ #if defined(ESP32) - /** + /** * The strange behavior in ESP8266 SSL client, BearSSLWiFiClientSecure - * The client disposed without memory released after the server close + * The client disposed without memory released after the server close * the connection due to QUIT command, which caused the memory leaks. */ MailClient.smtpSendP(this, esp_mail_str_7, true); diff --git a/lib/lib_div/lib_mail/src/ESP_Mail_Client.h b/lib/lib_div/lib_mail/src/ESP_Mail_Client.h index 0eda23a80..9d3c97d5b 100755 --- a/lib/lib_div/lib_mail/src/ESP_Mail_Client.h +++ b/lib/lib_div/lib_mail/src/ESP_Mail_Client.h @@ -2789,10 +2789,18 @@ private: #endif }; + +//#define EMAIL_DEBUG_PRINTLN Serial.println +//#define EMAIL_DEBUG_PRINT Serial.print +#define EMAIL_DEBUG_PRINTLN Tasmota_print +#define EMAIL_DEBUG_PRINT Tasmota_print + +extern void Tasmota_print(const char *); + static void __attribute__((used)) esp_mail_debug(const char *msg) { delay(0); - Serial.println(msg); + EMAIL_DEBUG_PRINTLN(msg); } static void __attribute__((used)) @@ -2800,11 +2808,13 @@ esp_mail_debug_line(const char *msg, bool newline) { delay(0); if (newline) - Serial.println(msg); + EMAIL_DEBUG_PRINTLN(msg); else - Serial.print(msg); + EMAIL_DEBUG_PRINT(msg); } extern ESP_Mail_Client MailClient; +extern FS *ufsp; + #endif // ESP_Mail_Client_H diff --git a/tasmota/sendemail_ESP32.ino b/tasmota/sendemail_ESP32.ino index 359440474..b518d537f 100644 --- a/tasmota/sendemail_ESP32.ino +++ b/tasmota/sendemail_ESP32.ino @@ -3,19 +3,11 @@ #include -#ifdef ESP8266 #ifndef SEND_MAIL32_MINRAM #undef SEND_MAIL32_MINRAM -#define SEND_MAIL32_MINRAM 13*1024 +#define SEND_MAIL32_MINRAM 70*1024 #endif -#endif // ESP8266 -#ifdef ESP32 -#ifndef SEND_MAIL32_MINRAM -#undef SEND_MAIL32_MINRAM -#define SEND_MAIL32_MINRAM 30*1024 -#endif -#endif // ESP32 #define xPSTR(a) a #define MAX_ATTCHMENTS 8 @@ -41,7 +33,7 @@ uint16_t SendMail(char *buffer) { const char *to; const char *subject; const char *cmd; - uint16_t status = 0; + uint16_t status = 1; uint16_t blen; char *endcmd; ESP_Mail_Session session; @@ -173,8 +165,12 @@ uint16_t SendMail(char *buffer) { #endif - //smtp->debug(true); +#ifdef DEBUG_EMAIL_PORT + smtp->debug(true); +#else smtp->debug(false); +#endif + // smtp->callback(smtpCallback); message.clearRecipients(); @@ -219,14 +215,17 @@ uint16_t SendMail(char *buffer) { /* Connect to server with the session config */ delay(0); - if (!smtp->connect(&session)) + if (!smtp->connect(&session)) { goto exit; + } /* Start sending the Email and close the session */ delay(0); if (!MailClient.sendMail(smtp, &message, true)) { Serial.println("Error sending Email, " + smtp->errorReason()); + } else { + status = 0; } exit: @@ -372,4 +371,16 @@ if (status.success()) } } + + +void Tasmota_print(const char *txt) { +#ifdef DEBUG_EMAIL_PORT + AddLog(LOG_LEVEL_INFO, PSTR("ESP32mail: %s"),txt); +#endif +} + + + + + #endif // USE_ESP32MAIL