diff --git a/src/ESP_Mail_Client.cpp b/src/ESP_Mail_Client.cpp index 358daadd..76218af5 100644 --- a/src/ESP_Mail_Client.cpp +++ b/src/ESP_Mail_Client.cpp @@ -9,7 +9,7 @@ /** * Mail Client Arduino Library for Espressif's ESP32 and ESP8266, Raspberry Pi RP2040 Pico, and SAMD21 with u-blox NINA-W102 WiFi/Bluetooth module * - * Created April 15, 2023 + * Created April 16, 2023 * * This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers. * @@ -1453,18 +1453,55 @@ String ESP_Mail_Client::errorReason(bool isSMTP, int errorCode, const char *msg) #if defined(ENABLE_ERROR_STRING) - // if there is server reply (IMAP), return the reply instead + // If there is server meanningful response (msg) is available, return it instead if (strlen(msg) > 0) return msg; // The error code enums were defined in ESP_Mail_Error.h and MB_FS.h. - // Not all error enums in ESP_Mail_Error.h was used - // then errorCode will match all cases of currently implemented enums. switch (errorCode) { + + case TCP_CLIENT_ERROR_CONNECTION_REFUSED: + ret = esp_mail_error_network_str_7; /* "connection refused" */ + break; + case TCP_CLIENT_ERROR_SEND_DATA_FAILED: + ret = esp_mail_error_network_str_8; /* "data sending failed" */ + break; + case TCP_CLIENT_ERROR_NOT_INITIALIZED: + ret = esp_mail_error_client_str_1; /* "client and/or necessary callback functions are not yet assigned" */ + break; + case TCP_CLIENT_ERROR_NOT_CONNECTED: + ret = esp_mail_error_network_str_4; /* "not connected" */ + break; + + case MAIL_CLIENT_ERROR_CONNECTION_CLOSED: + ret = esp_mail_error_network_str_6; /* "connection closed" */ + break; + case MAIL_CLIENT_ERROR_READ_TIMEOUT: + ret = esp_mail_error_network_str_3; /* "session timed out" */ + break; + case MAIL_CLIENT_ERROR_SSL_TLS_STRUCTURE_SETUP: + ret = esp_mail_error_ssl_str_1; /* "fail to set up the SSL/TLS structure" */ + break; + case MAIL_CLIENT_ERROR_OUT_OF_MEMORY: + ret = esp_mail_error_mem_str_8; /* "out of memory" */ + break; + case MAIL_CLIENT_ERROR_CUSTOM_CLIENT_DISABLED: + ret = esp_mail_error_client_str_2; /* "custom Client is not yet enabled" */ + break; + case MAIL_CLIENT_ERROR_NTP_TIME_SYNC_TIMED_OUT: + ret = esp_mail_error_network_str_1; /* "NTP server time synching timed out" */ + break; case MAIL_CLIENT_ERROR_SESSION_CONFIG_WAS_NOT_ASSIGNED: ret = esp_mail_error_session_str_1; /* "the Session_Config object was not assigned" */ break; + case MAIL_CLIENT_ERROR_TIME_WAS_NOT_SET: + ret = esp_mail_error_time_str_1; /* "library or device time was not set" */ + break; + case MAIL_CLIENT_ERROR_NOT_YET_LOGIN: + ret = esp_mail_error_auth_str_3; /* "not yet log in" */ + break; + #if defined(ENABLE_SMTP) case SMTP_STATUS_SERVER_CONNECT_FAILED: ret = esp_mail_error_network_str_2; /* "unable to connect to server" */ @@ -1508,8 +1545,11 @@ String ESP_Mail_Client::errorReason(bool isSMTP, int errorCode, const char *msg) case SMTP_STATUS_NO_SUPPORTED_AUTH: ret = esp_mail_error_auth_str_1; /* "the provided SASL authentication mechanism is not support" */ break; - case IMAP_STATUS_SMTP_SESSION_WAS_NOT_ASSIGNED: - ret = esp_mail_error_session_str_2; /* "the SMTPSession object was not assigned" */ + case SMTP_STATUS_SEND_CUSTOM_COMMAND_FAILED: + ret = esp_mail_error_smtp_str_10; /* "send custom command failed" */ + break; + case SMTP_STATUS_UNDEFINED: + ret = esp_mail_error_smtp_str_11; /* "undefined error" */ break; #endif @@ -1517,14 +1557,23 @@ String ESP_Mail_Client::errorReason(bool isSMTP, int errorCode, const char *msg) case IMAP_STATUS_SERVER_CONNECT_FAILED: ret = esp_mail_error_network_str_2; /* "unable to connect to server" */ break; - case IMAP_STATUS_NO_MESSAGE: - ret = esp_mail_error_imap_str_5; /* "some of the requested messages no longer exist" */ + case IMAP_STATUS_IMAP_RESPONSE_FAILED: + ret = esp_mail_error_imap_str_18; /* "server replied NO or BAD response" */ break; - case IMAP_STATUS_CHANGEDSINC_MODSEQ_TEST_FAILED: - ret = esp_mail_error_imap_str_14; /* "no message changed since (assigned) modsec" */ + case IMAP_STATUS_AUTHENTICATE_FAILED: + ret = esp_mail_error_imap_str_19; /* "authenticate failed" */ break; - case IMAP_STATUS_MODSEQ_WAS_NOT_SUPPORTED: - ret = esp_mail_error_imap_str_15; /* "CONDSTORE was not supported or modsec was not supported for selected mailbox" */ + case IMAP_STATUS_BAD_COMMAND: + ret = esp_mail_error_imap_str_17; /* "could not parse command" */ + break; + case IMAP_STATUS_STORE_FAILED: + ret = esp_mail_error_imap_str_20; /* "flags or keywords store failed" */ + break; + case IMAP_STATUS_SERVER_OAUTH2_LOGIN_DISABLED: + ret = esp_mail_error_imap_str_21; /* "server is not support OAuth2 login" */ + break; + case IMAP_STATUS_NO_MESSAGE: + ret = esp_mail_error_imap_str_5; /* "some of the requested messages no longer exist" */ break; case IMAP_STATUS_ERROR_DOWNLAD_TIMEOUT: ret = esp_mail_error_network_str_5; /* "connection timeout" */ @@ -1538,12 +1587,12 @@ String ESP_Mail_Client::errorReason(bool isSMTP, int errorCode, const char *msg) case IMAP_STATUS_LIST_MAILBOXS_FAILED: ret = esp_mail_error_imap_str_1; /* "fail to list the mailboxes" */ break; - case IMAP_STATUS_NO_SUPPORTED_AUTH: - ret = esp_mail_error_auth_str_1; /* "the provided SASL authentication mechanism is not support" */ - break; case IMAP_STATUS_CHECK_CAPABILITIES_FAILED: ret = esp_mail_error_imap_str_2; /* "fail to check the capabilities" */ break; + case IMAP_STATUS_NO_SUPPORTED_AUTH: + ret = esp_mail_error_auth_str_1; /* "the provided SASL authentication mechanism is not support" */ + break; case IMAP_STATUS_NO_MAILBOX_FOLDER_OPENED: ret = esp_mail_error_imap_str_5; /* "no mailbox opened" */ break; @@ -1556,56 +1605,23 @@ String ESP_Mail_Client::errorReason(bool isSMTP, int errorCode, const char *msg) case IMAP_STATUS_FIRMWARE_UPDATE_END_FAILED: ret = esp_mail_error_imap_str_8; /* "firmware update finalize failed" */ break; - case IMAP_STATUS_IMAP_SESSION_WAS_NOT_ASSIGNED: - ret = esp_mail_error_session_str_3; /* "the IMAPSession object was not assigned" */ + case IMAP_STATUS_CHANGEDSINC_MODSEQ_TEST_FAILED: + ret = esp_mail_error_imap_str_14; /* "no message changed since (assigned) modsec" */ break; - case IMAP_STATUS_BAD_COMMAND: - ret = esp_mail_error_imap_str_17; /* "could not parse command" */ + case IMAP_STATUS_MODSEQ_WAS_NOT_SUPPORTED: + ret = esp_mail_error_imap_str_15; /* "CONDSTORE was not supported or modsec was not supported for selected mailbox" */ break; -#endif - case MAIL_CLIENT_ERROR_CONNECTION_CLOSED: - ret = esp_mail_error_network_str_6; /* "connection closed" */ - break; - case MAIL_CLIENT_ERROR_READ_TIMEOUT: - ret = esp_mail_error_network_str_3; /* "session timed out" */ - break; - case MAIL_CLIENT_ERROR_SSL_TLS_STRUCTURE_SETUP: - ret = esp_mail_error_ssl_str_1; /* "fail to set up the SSL/TLS structure" */ - break; - case MAIL_CLIENT_ERROR_OUT_OF_MEMORY: - ret = esp_mail_error_mem_str_8; /* "out of memory" */ - break; - case TCP_CLIENT_ERROR_SEND_DATA_FAILED: - ret = esp_mail_error_network_str_8; /* "data sending failed" */ - break; - case TCP_CLIENT_ERROR_CONNECTION_REFUSED: - ret = esp_mail_error_network_str_7; /* "connection refused" */ - break; - case TCP_CLIENT_ERROR_NOT_INITIALIZED: - ret = esp_mail_error_client_str_1; /* "client and/or necessary callback functions are not yet assigned" */ - break; - case TCP_CLIENT_ERROR_NOT_CONNECTED: - ret = esp_mail_error_network_str_4; /* "not connected" */ - break; - case MAIL_CLIENT_ERROR_NTP_TIME_SYNC_TIMED_OUT: - ret = esp_mail_error_network_str_1; /* "NTP server time synching timed out" */ - break; - case MAIL_CLIENT_ERROR_TIME_WAS_NOT_SET: - ret = esp_mail_error_time_str_1; /* "library or device time was not set" */ - break; - case MAIL_CLIENT_ERROR_CUSTOM_CLIENT_DISABLED: - ret = esp_mail_error_client_str_2; /* "custom Client is not yet enabled" */ - break; - case MAIL_CLIENT_ERROR_NOT_YET_LOGIN: - ret = esp_mail_error_auth_str_3; /* "not yet log in" */ - break; +#endif #if defined(MBFS_FLASH_FS) || defined(MBFS_SD_FS) case MB_FS_ERROR_FILE_IO_ERROR: ret = esp_mail_error_mem_str_7; /* "file I/O error" */ break; + case MB_FS_ERROR_FILE_NOT_FOUND: + ret = esp_mail_error_mem_str_6; /* "file not found." */ + break; case MB_FS_ERROR_FLASH_STORAGE_IS_NOT_READY: ret = esp_mail_error_mem_str_1; /* "flash Storage is not ready." */ break; @@ -1615,13 +1631,9 @@ String ESP_Mail_Client::errorReason(bool isSMTP, int errorCode, const char *msg) case MB_FS_ERROR_FILE_STILL_OPENED: ret = esp_mail_error_mem_str_5; /* "file is still opened." */ break; - case MB_FS_ERROR_FILE_NOT_FOUND: - ret = esp_mail_error_mem_str_6; /* "file not found." */ - break; #endif default: - // Not possible case break; } diff --git a/src/ESP_Mail_Const.h b/src/ESP_Mail_Const.h index 8e4a9773..f85c312d 100644 --- a/src/ESP_Mail_Const.h +++ b/src/ESP_Mail_Const.h @@ -3126,8 +3126,6 @@ static const char esp_mail_error_auth_str_3[] PROGMEM = "not yet log in"; #if defined(ENABLE_ERROR_STRING) static const char esp_mail_error_session_str_1[] PROGMEM = "the Session_Config object was not assigned"; -static const char esp_mail_error_session_str_2[] PROGMEM = "the SMTPSession object was not assigned"; -static const char esp_mail_error_session_str_3[] PROGMEM = "the IMAPSession object was not assigned"; #endif ///////////////////////// @@ -3148,6 +3146,8 @@ static const char esp_mail_error_smtp_str_5[] PROGMEM = "send body failed"; static const char esp_mail_error_smtp_str_7[] PROGMEM = "sender Email address is not valid"; static const char esp_mail_error_smtp_str_8[] PROGMEM = "some of the recipient Email address is not valid"; static const char esp_mail_error_smtp_str_9[] PROGMEM = "set recipient failed"; +static const char esp_mail_error_smtp_str_10[] PROGMEM = "send custom command failed"; +static const char esp_mail_error_smtp_str_11[] PROGMEM = "undefined error"; #endif #endif @@ -3176,6 +3176,10 @@ static const char esp_mail_error_imap_str_13[] PROGMEM = "this feature was not s static const char esp_mail_error_imap_str_14[] PROGMEM = "no message changed since (assigned) modsec"; static const char esp_mail_error_imap_str_15[] PROGMEM = "CONDSTORE was not supported or modsec was not supported for selected mailbox"; static const char esp_mail_error_imap_str_17[] PROGMEM = "could not parse command"; +static const char esp_mail_error_imap_str_18[] PROGMEM = "server replied NO or BAD response"; +static const char esp_mail_error_imap_str_19[] PROGMEM = "authenticate failed"; +static const char esp_mail_error_imap_str_20[] PROGMEM = "flags or keywords store failed"; +static const char esp_mail_error_imap_str_21[] PROGMEM = "server is not support OAuth2 login"; #endif #endif @@ -3201,7 +3205,7 @@ static const char esp_mail_str_17[] PROGMEM = "cid:"; static const char esp_mail_str_18[] PROGMEM = "\r\n"; static const char esp_mail_str_19[] PROGMEM = "<"; static const char esp_mail_str_20[] PROGMEM = ">"; -// static const char esp_mail_str_21[] PROGMEM = ""; +static const char esp_mail_str_21[] PROGMEM = "(\"name\" \"ESP Mail Client\" \"version\" \"%s\")"; static const char esp_mail_str_22[] PROGMEM = "message/rfc822"; static const char esp_mail_str_23[] PROGMEM = "upload"; static const char esp_mail_str_24[] PROGMEM = "%"; @@ -3279,7 +3283,6 @@ static const char esp_mail_str_96[] PROGMEM = "Date/Time: %s"; static const char esp_mail_str_97[] PROGMEM = "Recipient: %s"; static const char esp_mail_str_98[] PROGMEM = "success"; static const char esp_mail_str_99[] PROGMEM = "failed"; -static const char esp_mail_str_100[] PROGMEM = "(\"name\" \"ESP Mail Client\" \"version\" \"%s\")"; #if defined(ENABLE_SMTP) static const char boundary_table[] PROGMEM = "=_abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; diff --git a/src/ESP_Mail_Error.h b/src/ESP_Mail_Error.h index d8c738d9..15687d45 100644 --- a/src/ESP_Mail_Error.h +++ b/src/ESP_Mail_Error.h @@ -1,5 +1,5 @@ /** - * Created April 15, 2023 + * Created April 16, 2023 */ #pragma once @@ -11,11 +11,10 @@ #error "Mixed versions compilation." #endif - -#define TCP_CLIENT_ERROR_CONNECTION_REFUSED (-1) -#define TCP_CLIENT_ERROR_SEND_DATA_FAILED (-2) -#define TCP_CLIENT_ERROR_NOT_INITIALIZED (-3) -#define TCP_CLIENT_ERROR_NOT_CONNECTED (-4) +#define TCP_CLIENT_ERROR_CONNECTION_REFUSED -1 +#define TCP_CLIENT_ERROR_SEND_DATA_FAILED -2 +#define TCP_CLIENT_ERROR_NOT_INITIALIZED -3 +#define TCP_CLIENT_ERROR_NOT_CONNECTED -4 #if defined(ENABLE_SMTP) @@ -35,16 +34,15 @@ #define SMTP_STATUS_NO_SUPPORTED_AUTH -113 #define SMTP_STATUS_SEND_CUSTOM_COMMAND_FAILED -114 #define SMTP_STATUS_UNDEFINED -115 -#define IMAP_STATUS_SMTP_SESSION_WAS_NOT_ASSIGNED -116 #endif #if defined(ENABLE_IMAP) #define IMAP_STATUS_SERVER_CONNECT_FAILED -200 #define IMAP_STATUS_IMAP_RESPONSE_FAILED -201 -#define IMAP_STATUS_LOGIN_FAILED -202 +#define IMAP_STATUS_AUTHENTICATE_FAILED -202 #define IMAP_STATUS_BAD_COMMAND -203 -#define IMAP_STATUS_PARSE_FLAG_FAILED -204 +#define IMAP_STATUS_STORE_FAILED -204 #define IMAP_STATUS_SERVER_OAUTH2_LOGIN_DISABLED -205 #define IMAP_STATUS_NO_MESSAGE -206 #define IMAP_STATUS_ERROR_DOWNLAD_TIMEOUT -207 @@ -57,22 +55,30 @@ #define IMAP_STATUS_FIRMWARE_UPDATE_INIT_FAILED -214 #define IMAP_STATUS_FIRMWARE_UPDATE_WRITE_FAILED -215 #define IMAP_STATUS_FIRMWARE_UPDATE_END_FAILED -216 -#define IMAP_STATUS_IMAP_SESSION_WAS_NOT_ASSIGNED -217 -#define IMAP_STATUS_CHANGEDSINC_MODSEQ_TEST_FAILED -218 -#define IMAP_STATUS_MODSEQ_WAS_NOT_SUPPORTED -219 +#define IMAP_STATUS_CHANGEDSINC_MODSEQ_TEST_FAILED -217 +#define IMAP_STATUS_MODSEQ_WAS_NOT_SUPPORTED -218 #endif +/** + * MB_FS.h + #define MB_FS_ERROR_FILE_IO_ERROR -300 + #define MB_FS_ERROR_FILE_NOT_FOUND -301 + #define MB_FS_ERROR_FLASH_STORAGE_IS_NOT_READY -302 + #define MB_FS_ERROR_SD_STORAGE_IS_NOT_READY -303 + #define MB_FS_ERROR_FILE_STILL_OPENED -304 +*/ + #if defined(ENABLE_SMTP) || defined(ENABLE_IMAP) -#define MAIL_CLIENT_ERROR_CONNECTION_CLOSED -28 -#define MAIL_CLIENT_ERROR_READ_TIMEOUT -29 -#define MAIL_CLIENT_ERROR_SSL_TLS_STRUCTURE_SETUP -32 -#define MAIL_CLIENT_ERROR_OUT_OF_MEMORY -33 -#define MAIL_CLIENT_ERROR_CUSTOM_CLIENT_DISABLED -34 -#define MAIL_CLIENT_ERROR_NTP_TIME_SYNC_TIMED_OUT -35 -#define MAIL_CLIENT_ERROR_SESSION_CONFIG_WAS_NOT_ASSIGNED -36 -#define MAIL_CLIENT_ERROR_TIME_WAS_NOT_SET -37 -#define MAIL_CLIENT_ERROR_NOT_YET_LOGIN -38 +#define MAIL_CLIENT_ERROR_CONNECTION_CLOSED -400 +#define MAIL_CLIENT_ERROR_READ_TIMEOUT -401 +#define MAIL_CLIENT_ERROR_SSL_TLS_STRUCTURE_SETUP -402 +#define MAIL_CLIENT_ERROR_OUT_OF_MEMORY -403 +#define MAIL_CLIENT_ERROR_CUSTOM_CLIENT_DISABLED -404 +#define MAIL_CLIENT_ERROR_NTP_TIME_SYNC_TIMED_OUT -405 +#define MAIL_CLIENT_ERROR_SESSION_CONFIG_WAS_NOT_ASSIGNED -406 +#define MAIL_CLIENT_ERROR_TIME_WAS_NOT_SET -407 +#define MAIL_CLIENT_ERROR_NOT_YET_LOGIN -408 #endif diff --git a/src/ESP_Mail_IMAP.h b/src/ESP_Mail_IMAP.h index 60dc09c0..d08af298 100644 --- a/src/ESP_Mail_IMAP.h +++ b/src/ESP_Mail_IMAP.h @@ -678,9 +678,7 @@ bool ESP_Mail_Client::readMail(IMAPSession *imap, bool closeSession) imap->_imap_cmd = esp_mail_imap_cmd_fetch_body_header; - int err = IMAP_STATUS_BAD_COMMAND; - if (imap->_headerOnly) - err = IMAP_STATUS_IMAP_RESPONSE_FAILED; + int err = imap->_headerOnly ? IMAP_STATUS_IMAP_RESPONSE_FAILED : IMAP_STATUS_BAD_COMMAND; if (!handleIMAPResponse(imap, err, closeSession)) { @@ -1311,7 +1309,7 @@ bool ESP_Mail_Client::imapAuth(IMAPSession *imap, bool &ssl) return false; imap->_imap_cmd = esp_mail_imap_cmd_sasl_auth_oauth; - if (!handleIMAPResponse(imap, IMAP_STATUS_LOGIN_FAILED, true)) + if (!handleIMAPResponse(imap, IMAP_STATUS_AUTHENTICATE_FAILED, true)) return false; cmd = getXOAUTH2String(imap->_session_cfg->login.email, imap->_session_cfg->login.accessToken); @@ -1321,7 +1319,7 @@ bool ESP_Mail_Client::imapAuth(IMAPSession *imap, bool &ssl) } imap->_imap_cmd = esp_mail_imap_cmd_sasl_auth_oauth; - if (!handleIMAPResponse(imap, IMAP_STATUS_LOGIN_FAILED, false)) + if (!handleIMAPResponse(imap, IMAP_STATUS_AUTHENTICATE_FAILED, false)) return false; } else if (sasl_auth_plain) @@ -1360,7 +1358,7 @@ bool ESP_Mail_Client::imapAuth(IMAPSession *imap, bool &ssl) return false; imap->_imap_cmd = esp_mail_imap_cmd_sasl_auth_plain; - if (!handleIMAPResponse(imap, IMAP_STATUS_LOGIN_FAILED, true)) + if (!handleIMAPResponse(imap, IMAP_STATUS_AUTHENTICATE_FAILED, true)) return false; cmd = encodeBase64Str(tmp, p); @@ -1372,7 +1370,7 @@ bool ESP_Mail_Client::imapAuth(IMAPSession *imap, bool &ssl) } imap->_imap_cmd = esp_mail_imap_cmd_sasl_auth_plain; - if (!handleIMAPResponse(imap, IMAP_STATUS_LOGIN_FAILED, true)) + if (!handleIMAPResponse(imap, IMAP_STATUS_AUTHENTICATE_FAILED, true)) return false; } else if (sasl_login) @@ -1389,7 +1387,7 @@ bool ESP_Mail_Client::imapAuth(IMAPSession *imap, bool &ssl) return false; imap->_imap_cmd = esp_mail_imap_cmd_sasl_login; - if (!handleIMAPResponse(imap, IMAP_STATUS_LOGIN_FAILED, true)) + if (!handleIMAPResponse(imap, IMAP_STATUS_AUTHENTICATE_FAILED, true)) return false; } @@ -1607,7 +1605,7 @@ bool ESP_Mail_Client::mSetFlag(IMAPSession *imap, MB_StringPtr sequenceSet, MB_S imap->_imap_cmd = esp_mail_imap_cmd_store; - if (!handleIMAPResponse(imap, IMAP_STATUS_PARSE_FLAG_FAILED, false)) + if (!handleIMAPResponse(imap, IMAP_STATUS_STORE_FAILED, false)) return false; if (closeSession) @@ -1627,7 +1625,7 @@ void ESP_Mail_Client::imapErrorCB(IMAPSession *imap, PGM_P info, bool prependCRL int ESP_Mail_Client::parseSearchResponse(IMAPSession *imap, esp_mail_imap_response_data &res, PGM_P tag, const char *key) { - int bufLen = res.chunkBufSize; + int bufLen = res.chunkBufSize; int ret = -1; char c = 0; int idx = 0; @@ -4876,7 +4874,7 @@ bool IMAPSession::id(IMAP_Identification *identification) { int bufLen = 50; char *buf = MailClient.allocMem(bufLen); - snprintf(buf, bufLen, pgm2Str(esp_mail_str_100 /* "(\"name\" \"ESP Mail Client\" \"version\" \"%s\")" */), ESP_MAIL_VERSION); + snprintf(buf, bufLen, pgm2Str(esp_mail_str_21 /* "(\"name\" \"ESP Mail Client\" \"version\" \"%s\")" */), ESP_MAIL_VERSION); cmd += buf; // release memory MailClient.freeMem(&buf);