Merge pull request #11025 from gemu2015/sendmail

remove spiffs from sendmail lib
This commit is contained in:
Theo Arends 2021-02-18 16:58:03 +01:00 committed by GitHub
commit 49d9cf3ce5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 153 additions and 94 deletions

View File

@ -405,12 +405,13 @@ bool ESP_Mail_Client::readMail(IMAPSession *imap, bool closeSession)
if (!SD.exists(imap->_config->storage.saved_path))
createDirs(imap->_config->storage.saved_path);
}
else if (!_flashOk && imap->_storageType == esp_mail_file_storage_type_flash)
else if (!_flashOk && imap->_storageType == esp_mail_file_storage_type_flash) {
#if defined(ESP32)
_flashOk = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
// _flashOk = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
#elif defined(ESP8266)
_flashOk = FLASH_FS.begin();
// _flashOk = FLASH_FS.begin();
#endif
}
}
if (cHeader(imap)->part_headers.size() > 0)
@ -602,13 +603,13 @@ bool ESP_Mail_Client::readMail(IMAPSession *imap, bool closeSession)
}
else if (imap->_storageType == esp_mail_file_storage_type_flash)
{
if (_flashOk)
if (_flashOk) {
#if defined(ESP32)
SPIFFS.end();
// SPIFFS.end();
#elif defined(ESP8266)
FLASH_FS.end();
// FLASH_FS.end();
#endif
}
_flashOk = false;
}
}
@ -2752,13 +2753,13 @@ bool ESP_Mail_Client::sendAttachments(SMTPSession *smtp, SMTP_Message *msg, cons
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 = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
// _flashOk = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
#elif defined(ESP8266)
_flashOk = FLASH_FS.begin();
// _flashOk = 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))
{
@ -2815,9 +2816,9 @@ bool ESP_Mail_Client::openFileRead(SMTPSession *smtp, SMTP_Message *msg, SMTP_At
}
else if (att->file.storage_type == esp_mail_file_storage_type_flash) {
#if defined(ESP32)
file_existed = SPIFFS.exists(filepath.c_str());
//file_existed = SPIFFS.exists(filepath.c_str());
#elif defined(ESP8266)
file_existed = FLASH_FS.exists(filepath.c_str());
//file_existed = FLASH_FS.exists(filepath.c_str());
#endif
} else if (att->file.storage_type == esp_mail_file_storage_type_univ) {
file_existed = ufsp->exists(filepath.c_str());
@ -2838,9 +2839,9 @@ bool ESP_Mail_Client::openFileRead(SMTPSession *smtp, SMTP_Message *msg, SMTP_At
}
else if (att->file.storage_type == esp_mail_file_storage_type_flash) {
#if defined(ESP32)
file_existed = SPIFFS.exists(filepath.c_str());
//file_existed = SPIFFS.exists(filepath.c_str());
#elif defined(ESP8266)
file_existed = FLASH_FS.exists(filepath.c_str());
//file_existed = FLASH_FS.exists(filepath.c_str());
#endif
}
else if (att->file.storage_type == esp_mail_file_storage_type_univ) {
@ -2882,9 +2883,9 @@ bool ESP_Mail_Client::openFileRead(SMTPSession *smtp, SMTP_Message *msg, SMTP_At
}
else if (att->file.storage_type == esp_mail_file_storage_type_flash) {
#if defined(ESP32)
file = SPIFFS.open(filepath.c_str(), FILE_READ);
//file = SPIFFS.open(filepath.c_str(), FILE_READ);
#elif defined(ESP8266)
file = FLASH_FS.open(filepath.c_str(), "r");
//file = FLASH_FS.open(filepath.c_str(), "r");
#endif
}
else if (att->file.storage_type == esp_mail_file_storage_type_univ) {
@ -2997,13 +2998,13 @@ bool ESP_Mail_Client::sendInline(SMTPSession *smtp, SMTP_Message *msg, const std
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 = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
// _flashOk = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
#elif defined(ESP8266)
_flashOk = FLASH_FS.begin();
// _flashOk = 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))
{
@ -5694,13 +5695,13 @@ void ESP_Mail_Client::saveHeader(IMAPSession *imap)
prepareFilePath(imap, headerFilePath, true);
if (imap->_storageType == esp_mail_file_storage_type_sd && !_sdOk)
_sdOk = sdTest();
else if (imap->_storageType == esp_mail_file_storage_type_flash && !_flashOk)
else if (imap->_storageType == esp_mail_file_storage_type_flash && !_flashOk) {
#if defined(ESP32)
_flashOk = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
// _flashOk = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
#elif defined(ESP8266)
_flashOk = FLASH_FS.begin();
// _flashOk = FLASH_FS.begin();
#endif
}
if (_sdOk || _flashOk)
{
if (file)
@ -5708,13 +5709,13 @@ void ESP_Mail_Client::saveHeader(IMAPSession *imap)
if (imap->_storageType == esp_mail_file_storage_type_sd)
file = SD.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 = SPIFFS.open(headerFilePath.c_str(), FILE_WRITE);
// file = SPIFFS.open(headerFilePath.c_str(), FILE_WRITE);
#elif defined(ESP8266)
file = FLASH_FS.open(headerFilePath.c_str(), "w");
// file = FLASH_FS.open(headerFilePath.c_str(), "w");
#endif
}
if (file)
{
std::string s;
@ -5932,13 +5933,13 @@ bool ESP_Mail_Client::handleAttachment(IMAPSession *imap, char *buf, int bufLen,
if (imap->_storageType == esp_mail_file_storage_type_sd && !_sdOk)
_sdOk = sdTest();
else if (imap->_storageType == esp_mail_file_storage_type_flash && !_flashOk)
else if (imap->_storageType == esp_mail_file_storage_type_flash && !_flashOk) {
#if defined(ESP32)
_flashOk = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
// _flashOk = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
#elif defined(ESP8266)
_flashOk = FLASH_FS.begin();
// _flashOk = FLASH_FS.begin();
#endif
}
if (_sdOk || _flashOk)
{
@ -5962,12 +5963,13 @@ bool ESP_Mail_Client::handleAttachment(IMAPSession *imap, char *buf, int bufLen,
if (imap->_storageType == esp_mail_file_storage_type_sd)
file = SD.open(filePath.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 = SPIFFS.open(filePath.c_str(), FILE_WRITE);
// file = SPIFFS.open(filePath.c_str(), FILE_WRITE);
#elif defined(ESP8266)
file = FLASH_FS.open(filePath.c_str(), "w");
// file = FLASH_FS.open(filePath.c_str(), "w");
#endif
}
}
}
@ -6362,12 +6364,13 @@ void ESP_Mail_Client::decodeText(IMAPSession *imap, char *buf, int bufLen, int &
if (imap->_storageType == esp_mail_file_storage_type_sd)
file = SD.open(filePath.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 = SPIFFS.open(filePath.c_str(), FILE_WRITE);
// file = SPIFFS.open(filePath.c_str(), FILE_WRITE);
#elif defined(ESP8266)
file = FLASH_FS.open(filePath.c_str(), "w");
// file = FLASH_FS.open(filePath.c_str(), "w");
#endif
}
}
}
@ -7294,12 +7297,13 @@ bool IMAPSession::connect(ESP_Mail_Session *sesssion, IMAP_Config *config)
{
if (_sesson_cfg->certificate.cert_file_storage_type == esp_mail_file_storage_type::esp_mail_file_storage_type_sd && !MailClient._sdOk)
MailClient._sdOk = MailClient.sdTest();
if (_sesson_cfg->certificate.cert_file_storage_type == esp_mail_file_storage_type::esp_mail_file_storage_type_flash && !MailClient._flashOk)
if (_sesson_cfg->certificate.cert_file_storage_type == esp_mail_file_storage_type::esp_mail_file_storage_type_flash && !MailClient._flashOk) {
#if defined(ESP32)
MailClient._flashOk = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
// MailClient._flashOk = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
#elif defined(ESP8266)
MailClient._flashOk = FLASH_FS.begin();
// MailClient._flashOk = FLASH_FS.begin();
#endif
}
}
return MailClient.imapAuth(this);
@ -7978,12 +7982,14 @@ bool SMTPSession::connect(ESP_Mail_Session *config)
{
if (_sesson_cfg->certificate.cert_file_storage_type == esp_mail_file_storage_type::esp_mail_file_storage_type_sd && !MailClient._sdOk)
MailClient._sdOk = MailClient.sdTest();
if (_sesson_cfg->certificate.cert_file_storage_type == esp_mail_file_storage_type::esp_mail_file_storage_type_flash && !MailClient._flashOk)
if (_sesson_cfg->certificate.cert_file_storage_type == esp_mail_file_storage_type::esp_mail_file_storage_type_flash && !MailClient._flashOk) {
#if defined(ESP32)
MailClient._flashOk = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
// MailClient._flashOk = SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED);
#elif defined(ESP8266)
MailClient._flashOk = FLASH_FS.begin();
// MailClient._flashOk = FLASH_FS.begin();
#endif
MailClient._flashOk = 1;
}
}
return MailClient.smtpAuth(this);
}

View File

@ -48,7 +48,7 @@
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <FS.h>
#include <SPIFFS.h>
//#include <SPIFFS.h>
#include <ETH.h>
#include "wcs/esp32/ESP_Mail_HTTPClient32.h"
#include "extras/ESPTimeHelper.h"

View File

@ -205,8 +205,9 @@ void ESP_Mail_HTTPClient32::setCertFile(const char *caCertFile, esp_mail_file_st
bool t = false;
_certType = 2;
if (storageType == esp_mail_file_storage_type::esp_mail_file_storage_type_flash)
t = SPIFFS.begin(true);
if (storageType == esp_mail_file_storage_type::esp_mail_file_storage_type_flash) {
// t = SPIFFS.begin(true);
}
else if (storageType == esp_mail_file_storage_type::esp_mail_file_storage_type_sd)
t = SD.begin();
if (!t)
@ -215,8 +216,8 @@ void ESP_Mail_HTTPClient32::setCertFile(const char *caCertFile, esp_mail_file_st
File f;
if (storageType == esp_mail_file_storage_type::esp_mail_file_storage_type_flash)
{
if (SPIFFS.exists(caCertFile))
f = SPIFFS.open(caCertFile, FILE_READ);
//if (SPIFFS.exists(caCertFile))
// f = SPIFFS.open(caCertFile, FILE_READ);
}
else if (storageType == esp_mail_file_storage_type::esp_mail_file_storage_type_sd)
{

View File

@ -37,7 +37,7 @@
#include <Arduino.h>
#include <WiFiClient.h>
#include <FS.h>
#include <SPIFFS.h>
//#include <SPIFFS.h>
#include <SD.h>
#include "ESP_Mail_WCS32.h"

View File

@ -53,6 +53,9 @@ extern "C"
#include <c_types.h>
#include <coredecls.h>
//#define Optimistic_yield(A) optimistic_yield(A);ESP.wdtFeed();
#define Optimistic_yield(A) optimistic_yield(A)
#if !CORE_MOCK
// The BearSSL thunks in use for now
@ -315,7 +318,7 @@ namespace ESP_Mail
// Ensure we yield if we need multiple fragments to avoid WDT
if (sent_bytes)
{
optimistic_yield(1000);
Optimistic_yield(1000);
}
// Get BearSSL to a state where we can send
@ -519,7 +522,7 @@ namespace ESP_Mail
for (int no_work = 0; blocking || no_work < 2;)
{
optimistic_yield(100);
Optimistic_yield(100);
if (loopTimeout)
{
@ -662,7 +665,7 @@ namespace ESP_Mail
{
_handshake_done = true;
}
optimistic_yield(1000);
Optimistic_yield(1000);
}
return _handshake_done;
}
@ -2047,7 +2050,7 @@ namespace ESP_Mail
if (!result)
{
optimistic_yield(100);
Optimistic_yield(100);
}
return result;
}

View File

@ -1,11 +1,22 @@
#ifdef USE_ESP32MAIL
#include <ESP_Mail_Client.h>
#ifdef ESP8266
#ifndef SEND_MAIL32_MINRAM
#undef SEND_MAIL32_MINRAM
#define SEND_MAIL32_MINRAM 13*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
char *attachments[MAX_ATTCHMENTS];
@ -13,6 +24,9 @@ uint8_t num_attachments;
void script_send_email_body(void(*func)(char *));
String html_content = "";
SMTP_Message *email_mptr;
SMTPSession *smtp;
//SMTPSession smtp;
void smtpCallback(SMTP_Status status);
//#define DEBUG_EMAIL_PORT
@ -30,17 +44,22 @@ uint16_t SendMail(char *buffer) {
uint16_t status = 0;
uint16_t blen;
char *endcmd;
SMTPSession smtp;
ESP_Mail_Session session;
SMTP_Message message;
email_mptr = &message;
smtp = new SMTPSession();
if (!smtp) {
return 4;
}
// return if not enough memory
uint32_t mem = ESP.getFreeHeap();
//AddLog(LOG_LEVEL_INFO, PSTR("heap: %d"),mem);
if (mem < SEND_MAIL32_MINRAM) {
// return 4;
return 4;
}
#ifdef DEBUG_EMAIL_PORT
AddLog(LOG_LEVEL_INFO, PSTR("heap: %d"),mem);
#endif
while (*buffer==' ') buffer++;
@ -154,15 +173,14 @@ uint16_t SendMail(char *buffer) {
#endif
//smtp.debug(true);
smtp.debug(false);
//smtp->debug(true);
smtp->debug(false);
// smtp->callback(smtpCallback);
message.clearRecipients();
message.clearCc();
message.clearBcc();
//smtp.callback(smtpCallback);
session.server.host_name = mserv;
session.server.port = port;
session.login.email = user;
@ -201,17 +219,18 @@ 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());
if (!MailClient.sendMail(smtp, &message, true)) {
Serial.println("Error sending Email, " + smtp->errorReason());
}
exit:
if (smtp) delete smtp;
for (uint32_t cnt = 0; cnt < MAX_ATTCHMENTS; cnt++) {
if (attachments[cnt]) {
free(attachments[cnt]);
@ -322,4 +341,35 @@ void attach_Data(char *name, uint8_t *buff, uint32_t len) {
email_mptr->resetAttachItem(att);
}
/* Callback function to get the Email sending status */
void smtpCallback(SMTP_Status status)
{
/* Print the current status */
Serial.println(status.info());
/* Print the sending result */
if (status.success())
{
Serial.println("----------------");
Serial.printf("Message sent success: %d\n", status.completedCount());
Serial.printf("Message sent failled: %d\n", status.failedCount());
Serial.println("----------------\n");
struct tm dt;
for (size_t i = 0; i < smtp->sendingResult.size(); i++)
{
/* Get the result item */
SMTP_Result result = smtp->sendingResult.getItem(i);
localtime_r(&result.timesstamp, &dt);
Serial.printf("Message No: %d\n", i + 1);
Serial.printf("Status: %s\n", result.completed ? "success" : "failed");
Serial.printf("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
Serial.printf("Recipient: %s\n", result.recipients);
Serial.printf("Subject: %s\n", result.subject);
}
Serial.println("----------------\n");
}
}
#endif // USE_ESP32MAIL

View File

@ -2924,7 +2924,7 @@ const char kWebCommands[] PROGMEM = "|" // No prefix
#ifdef USE_EMULATION
D_CMND_EMULATION "|"
#endif
#ifdef USE_SENDMAIL
#if defined(USE_SENDMAIL) || defined(USE_ESP32MAIL)
D_CMND_SENDMAIL "|"
#endif
D_CMND_WEBSERVER "|" D_CMND_WEBPASSWORD "|" D_CMND_WEBLOG "|" D_CMND_WEBREFRESH "|" D_CMND_WEBSEND "|" D_CMND_WEBCOLOR "|"
@ -2934,7 +2934,7 @@ void (* const WebCommand[])(void) PROGMEM = {
#ifdef USE_EMULATION
&CmndEmulation,
#endif
#ifdef USE_SENDMAIL
#if defined(USE_SENDMAIL) || defined(USE_ESP32MAIL)
&CmndSendmail,
#endif
&CmndWebServer, &CmndWebPassword, &CmndWeblog, &CmndWebRefresh, &CmndWebSend, &CmndWebColor,
@ -2966,7 +2966,7 @@ void CmndEmulation(void)
}
#endif // USE_EMULATION
#ifdef USE_SENDMAIL
#if defined(USE_SENDMAIL) || defined(USE_ESP32MAIL)
void CmndSendmail(void)
{
if (XdrvMailbox.data_len > 0) {

View File

@ -4325,7 +4325,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
ResponseAppend_P(PSTR("%s"), tmp);
goto next_line;
}
#ifdef USE_SENDMAIL
#if defined(USE_SENDMAIL) || defined(USE_ESP32MAIL)
else if (!strncmp(lp, "mail", 4)) {
lp+=5;
char tmp[256];
@ -4680,10 +4680,9 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
uint8_t script_xsns_index = 0;
void ScripterEvery100ms(void) {
if (Settings.rule_enabled && (TasmotaGlobal.uptime > 4)) {
if (bitRead(Settings.rule_enabled, 0) && (TasmotaGlobal.uptime > 4)) {
ResponseClear();
uint16_t script_tele_period_save = TasmotaGlobal.tele_period;
TasmotaGlobal.tele_period = 2;
@ -4695,7 +4694,7 @@ void ScripterEvery100ms(void) {
Run_Scripter(">T", 2, TasmotaGlobal.mqtt_data);
}
}
if (Settings.rule_enabled) {
if (bitRead(Settings.rule_enabled, 0)) {
if (glob_script_mem.fast_script == 99) Run_Scripter(">F", 2, 0);
}
}
@ -7161,7 +7160,7 @@ nextwebline:
#endif //USE_SCRIPT_WEB_DISPLAY
#ifdef USE_SENDMAIL
#if defined(USE_SENDMAIL) || defined(USE_ESP32MAIL)
void script_send_email_body(void(*func)(char *)) {
uint8_t msect = Run_Scripter(">m", -2, 0);