Skip to content

Commit

Permalink
Fix NTP time out issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Aug 28, 2023
1 parent c72aa81 commit 2521e06
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 186 deletions.
17 changes: 9 additions & 8 deletions examples/SMTP/Set_Time/Set_Time.ino
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ void setup()
Serial.println(WiFi.localIP());
Serial.println();


Serial.print("Waiting for NTP server time reading");

#if defined(ESP8266) || defined(ESP32) && !defined(ARDUINO_NANO_RP2040_CONNECT)
Expand Down Expand Up @@ -173,11 +172,11 @@ void setup()
/**
* Once the system time (using smtp.setSystemTime) or device time was set before calling smtp.connect, the following config will
* not take effect when NTP time is enabled.
*
*
* config.time.ntp_server
* config.time.gmt_offset
* config.time.day_light_offset
*
*
* To reset the reference time and use config.time instead, call smtp.setSystemTime(0) whenever you want.
*/

Expand All @@ -186,15 +185,17 @@ void setup()
/* Set the message headers */
message.sender.name = F("ESP Mail");
message.sender.email = AUTHOR_EMAIL;
message.subject = F("Test sending plain text Email");
message.addRecipient(F("Someone"), RECIPIENT_EMAIL);

String textMsg = "This is simple plain text message";
message.text.content = textMsg;
/* The time format of timestamp to inject into subject or content as using in strftime C++ function */
message.timestamp.tag = "#esp_mail_current_time";

message.text.charSet = F("us-ascii");
/* The tag that will be replaced with current timestamp */
message.timestamp.format = "%B %d, %Y %H:%M:%S";

message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
message.subject = F("Test sending plain text Email (#esp_mail_current_time)");

message.text.content = "This is simple plain text message\n\nSent #esp_mail_current_time";

if (!smtp.connect(&config))
{
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ESP Mail Client",
"version": "3.4.7",
"version": "3.4.8",
"keywords": "communication, email, imap, smtp, esp32, esp8266, samd, arduino",
"description": "Arduino E-Mail Client Library to send, read and get incoming email notification for ESP32, ESP8266 and SAMD21 devices. The library also supported other Arduino Devices using Clients interfaces e.g. WiFiClient, EthernetClient, and GSMClient.",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=ESP Mail Client

version=3.4.7
version=3.4.8

author=Mobizt

Expand Down
42 changes: 17 additions & 25 deletions src/ESP_Mail_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30407)
#if !VALID_VERSION_CHECK(30408)
#error "Mixed versions compilation."
#endif

/**
* Mail Client Arduino Library for Arduino devices.
*
* Created August 27, 2023
* Created August 28, 2023
*
* This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers.
*
Expand Down Expand Up @@ -503,12 +503,12 @@ void ESP_Mail_Client::getTimezone(const char *TZ_file, MB_String &out)
#endif
}

void ESP_Mail_Client::setTime(const char *TZ_Var, const char *TZ_file, bool wait)
void ESP_Mail_Client::setTime(const char *TZ_Var, const char *TZ_file, bool wait, bool debugProgress)
{

_clockReady = Time.clockReady();
timeStatus = Time.timeReady();

if (!_clockReady)
if (!timeStatus)
{

#if defined(ENABLE_IMAP) || defined(ENABLE_SMTP)
Expand All @@ -517,15 +517,7 @@ void ESP_Mail_Client::setTime(const char *TZ_Var, const char *TZ_file, bool wait

if (WiFI_CONNECTED)
{
Time.setClock();
if (wait)
{
unsigned long waitMs = millis();
while (!Time.clockReady() && millis() - waitMs < 10000)
{
yield_impl();
}
}
Time.readNTPTime(wait ? 10000 : 0, debugProgress);
}
else
{
Expand All @@ -544,7 +536,7 @@ void ESP_Mail_Client::setTime(const char *TZ_Var, const char *TZ_file, bool wait
#endif
}

_clockReady = Time.clockReady();
timeStatus = Time.timeReady();
}

void ESP_Mail_Client::getSetTimezoneEnv(const char *TZ_file, const char *TZ_Var)
Expand Down Expand Up @@ -1347,11 +1339,10 @@ bool ESP_Mail_Client::prepareTime(Session_Config *session_config, T sessionPtr)

if (session_config->time.ntp_server.length() > 0 || timeShouldBeValid)
{

if (session_config->time.ntp_server.length() > 0)
Time.begin(session_config->time.gmt_offset, session_config->time.day_light_offset, session_config->time.ntp_server.c_str());

if (!Time.clockReady())
Time.begin(session_config->time.gmt_offset, session_config->time.day_light_offset, session_config->time.ntp_server.c_str());

if (!Time.timeReady())
{
if (sessionPtr->client.type() == esp_mail_client_type_external_gsm_client)
{
Expand All @@ -1363,16 +1354,17 @@ bool ESP_Mail_Client::prepareTime(Session_Config *session_config, T sessionPtr)
int sec = 0;
float timezone = 0;
if (sessionPtr->client.gprsGetTime(year, month, day, hour, min, sec, timezone))
Time.setTimestamp(Time.getTimestamp(year, month, day, hour, min, sec));
Time.setTimestamp(Time.getTimestamp(year, month, day, hour, min, sec), timezone);
}
else
{
#if defined(ENABLE_NTP_TIME)
#if !defined(SILENT_MODE)
if (sessionPtr->_debug && !isResponseCB<T>(sessionPtr))
esp_mail_debug_print_tag(esp_mail_dbg_str_21 /* "Reading time from NTP server" */, esp_mail_debug_tag_type_client, true);
esp_mail_debug_print_tag(esp_mail_dbg_str_21 /* "Reading time from NTP server" */, esp_mail_debug_tag_type_client, false);
#endif
setTime(session_config->time.timezone_env_string.c_str(), session_config->time.timezone_file.c_str(), true);

setTime(session_config->time.timezone_env_string.c_str(), session_config->time.timezone_file.c_str(), true, sessionPtr->_debug && !isResponseCB<T>(sessionPtr));
#endif
}
}
Expand All @@ -1381,11 +1373,11 @@ bool ESP_Mail_Client::prepareTime(Session_Config *session_config, T sessionPtr)
#endif

#if defined(ESP32)
if (Time.clockReady() && !timezoneEnvSet)
if (Time.timeReady() && !timezoneEnvSet)
getSetTimezoneEnv(session_config->time.timezone_file.c_str(), session_config->time.timezone_env_string.c_str());
#endif

if (Time.clockReady())
if (Time.timeReady())
return true;
else if (WiFI_CONNECTED && timeShouldBeValid)
{
Expand Down Expand Up @@ -1438,7 +1430,7 @@ void ESP_Mail_Client::setSecure(ESP_Mail_TCPClient &client, Session_Config *sess
{
if (session_config->certificate.cert_file.length() > 0 || session_config->certificate.cert_data != NULL || session_config->cert_ptr > 0)
{
client.setClockReady(_clockReady);
client.setClockReady(timeStatus);
}

if (session_config->certificate.cert_file.length() == 0)
Expand Down
11 changes: 7 additions & 4 deletions src/ESP_Mail_Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#define ESP_MAIL_CLIENT_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30407)
#if !VALID_VERSION_CHECK(30408)
#error "Mixed versions compilation."
#endif

/**
* Mail Client Arduino Library for Arduino devices.
*
* Created August 27, 2023
* Created August 28, 2023
*
* This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers.
*
Expand Down Expand Up @@ -733,6 +733,9 @@ class SMTP_Message

/* The field that contains the parent's references (if any) and followed by the parent's message ID (if any) of the message to which this one is a reply */
MB_String references;

/* The timestamp value to replace in text */
esp_mail_timestamp_value_t timestamp;

private:
friend class ESP_Mail_Client;
Expand Down Expand Up @@ -1071,7 +1074,7 @@ class ESP_Mail_Client
friend class IMAPSession;

MB_FS *mbfs = nullptr;
bool _clockReady = false;
bool timeStatus = false;
time_t ts = 0;
bool networkAutoReconnect = true;
volatile bool networkStatus = false;
Expand Down Expand Up @@ -1214,7 +1217,7 @@ class ESP_Mail_Client

// Set or sync device system time with NTP server
// Do not modify or remove
void setTime(const char *TZ_Var, const char *TZ_file, bool wait);
void setTime(const char *TZ_Var, const char *TZ_file, bool wait, bool debugProgress);

// Set the device time zone via TZ environment variable
void setTimezone(const char *TZ_Var, const char *TZ_file);
Expand Down
4 changes: 2 additions & 2 deletions src/ESP_Mail_Client_Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#ifndef ESP_MAIL_VERSION

#define ESP_MAIL_VERSION "3.4.7"
#define ESP_MAIL_VERSION_NUM 30407
#define ESP_MAIL_VERSION "3.4.8"
#define ESP_MAIL_VERSION_NUM 30408

/* The inconsistent file version checking to prevent mixed versions compilation. */
#define VALID_VERSION_CHECK(ver) (ver == ESP_MAIL_VERSION_NUM)
Expand Down
10 changes: 9 additions & 1 deletion src/ESP_Mail_Const.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define ESP_MAIL_CONST_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30407)
#if !VALID_VERSION_CHECK(30408)
#error "Mixed versions compilation."
#endif

Expand Down Expand Up @@ -835,6 +835,14 @@ struct esp_mail_smtp_command_t
char text[9];
};

struct esp_mail_timestamp_value_t
{
/* The time format of timestamp to inject into subject or content as using in strftime C++ function */
MB_String format;
/* The tag that will be replaced with current timestamp */
MB_String tag;
};

/** The SMTP commands per stansards.
* The arrangement is related to esp_mail_smtp_command_types enum.
* Do not modify or remove.
Expand Down
2 changes: 1 addition & 1 deletion src/ESP_Mail_Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define ESP_MAIL_ERROR_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30407)
#if !VALID_VERSION_CHECK(30408)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/ESP_Mail_FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define ESP_MAIL_CONFIG_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30407)
#if !VALID_VERSION_CHECK(30408)
#error "Mixed versions compilation."
#endif

Expand Down
7 changes: 3 additions & 4 deletions src/ESP_Mail_IMAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#define ESP_MAIL_IMAP_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30407)
#if !VALID_VERSION_CHECK(30408)
#error "Mixed versions compilation."
#endif

/**
* 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 August 27, 2023
* Created August 28, 2023
*
* This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers.
*
Expand Down Expand Up @@ -5343,8 +5343,7 @@ void IMAPSession::mimeDataStreamCallback(MIMEDataStreamCallback mimeDataStreamCa

void IMAPSession::setSystemTime(time_t ts, float gmtOffset)
{
MailClient.Time.TZ = gmtOffset;
MailClient.Time.setTimestamp(ts);
MailClient.Time.setTimestamp(ts, gmtOffset);
}

void IMAPSession::keepAlive(int tcpKeepIdleSeconds, int tcpKeepIntervalSeconds, int tcpKeepCount)
Expand Down
17 changes: 13 additions & 4 deletions src/ESP_Mail_SMTP.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#define ESP_MAIL_SMTP_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30407)
#if !VALID_VERSION_CHECK(30408)
#error "Mixed versions compilation."
#endif

/**
* 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 August 27, 2023
* Created August 28, 2023
*
* This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers.
*
Expand Down Expand Up @@ -319,6 +319,10 @@ bool ESP_Mail_Client::addSendingResult(SMTPSession *smtp, SMTP_Message *msg, boo
status.completed = result;
status.timestamp = smtp->ts;
status.subject = msg->subject.c_str();

if (msg->timestamp.tag.length() && msg->timestamp.format.length())
status.subject.replaceAll(msg->timestamp.tag, Time.getDateTimeString(Time.getCurrentTimestamp(), msg->timestamp.format.c_str()));

status.recipients = msg->_rcp[0].email.c_str();
smtp->sendingResult.add(&status);

Expand Down Expand Up @@ -716,6 +720,9 @@ bool ESP_Mail_Client::sendContent(SMTPSession *smtp, SMTP_Message *msg, bool clo
MB_String s;
appendHeaderField(s, rfc822_headers[esp_mail_rfc822_header_field_subject].text, msg->subject.c_str(), false, true);

if (msg->timestamp.tag.length() && msg->timestamp.format.length())
s.replaceAll(msg->timestamp.tag, Time.getDateTimeString(Time.getCurrentTimestamp(), msg->timestamp.format.c_str()));

// Construct the 'Date' header field.
// The 'Date' header field should be valid and should be included in the message headers to
// prevent the 'spam' or 'junk' message considered by mail server.
Expand Down Expand Up @@ -2207,6 +2214,9 @@ void ESP_Mail_Client::encodingText(SMTPSession *smtp, SMTP_Message *msg, uint8_t
content += s;
s.clear();
}

if (msg->timestamp.tag.length() && msg->timestamp.format.length())
content.replaceAll(msg->timestamp.tag, Time.getDateTimeString(Time.getCurrentTimestamp(), msg->timestamp.format.c_str()));
}

void ESP_Mail_Client::encodeQP(const char *buf, char *out)
Expand Down Expand Up @@ -3533,8 +3543,7 @@ SMTP_Status SMTPSession::status()

void SMTPSession::setSystemTime(time_t ts, float gmtOffset)
{
MailClient.Time.TZ = gmtOffset;
MailClient.Time.setTimestamp(ts);
MailClient.Time.setTimestamp(ts, gmtOffset);
}

void SMTPSession::keepAlive(int tcpKeepIdleSeconds, int tcpKeepIntervalSeconds, int tcpKeepCount)
Expand Down
2 changes: 1 addition & 1 deletion src/ESP_Mail_TCPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#define ESP_MAIL_TCPCLIENT_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30407)
#if !VALID_VERSION_CHECK(30408)
#error "Mixed versions compilation."
#endif

Expand Down
Loading

0 comments on commit 2521e06

Please sign in to comment.