-
Hi, I have a program to send email every 5 mins. I have set this in my setup function. WifiWaitConnect("Setup: ");
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
//smtp.debug(1);
smtp.callback(smtpCallback);
session.server.host_name = smtpUrl;
session.server.port = smtpPort ;
session.login.email = emailFrom;
session.login.password = emailPass;
session.login.user_domain = "";
session.time.ntp_server = "pool.ntp.org,time.nist.gov";
session.time.gmt_offset = 0;
session.time.day_light_offset = 0;
session.time.timezone_env_string = "EST5EDT,M3.2.0,M11.1.0";
SendMail("Setup",F("Email Client Initialized ") + WiFi.localIP().toString() + ". \r\n\r\n" + StoredValues()); So the above function connects to wifi and set the email parms and send the mail. In the loop function i call sendmail function every like 5 mins. void loop()
{
SendMail("loop","Email from loop");
delay(300000);
} void SendMail(String Subject, String Body)
{
SMTP_Message message;
message.sender.name = "ESP8266";
message.sender.email = emailFrom;
message.subject = Subject ;
message.addRecipient("ESP8266Receiver", emailTo);
message.text.content = Body.c_str();
message.text.charSet = "us-ascii";
message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_low;
message.response.notify = esp_mail_smtp_notify_success | esp_mail_smtp_notify_failure | esp_mail_smtp_notify_delay;
if (!smtp.connect(&session))
{ LastSMTPEmailReasonCode = smtp.errorReason();
return;
}
if (!smtp.isLoggedIn())
LastSMTPEmailReasonCode = F("SMTP not logged in");
else
{
if(!smtp.isAuthenticated())
LastSMTPEmailReasonCode = F("SMTP Logged in, not authenticated.");
}
if (!MailClient.sendMail(&smtp, &message))
{
Serial.println("Error sending Email, " + smtp.errorReason());
}
Serial.println(String(EmailTimestamp));
} In the callback function I am getting the email sent timestamp and storing in variable. void smtpCallback(SMTP_Status status)
{
/* Print the current status */
//Serial.println(status.info()); // ENable to DEBUG SMTP
status.info();
/* Print the sending result */
if (status.success())
{
ESP_MAIL_PRINTF("Message sent success : %d failed : %d \n", status.completedCount(), status.failedCount());
for (size_t i = 0; i < smtp.sendingResult.size(); i++)
{
/* Get the result item */
SMTP_Result result = smtp.sendingResult.getItem(i);
strcpy(EmailTimestamp, MailClient.Time.getDateTimeString(result.timestamp, "%Y-%m-%d-%H.%M.%S %z").c_str());
}
smtp.sendingResult.clear();
}
} Now my problem is sometimes after wifi is connected the internet is not available then the first email sendmail in setup function fails with message Then after sometime when internet is available the sendmail function in loop() works fine. It sends emails. But the timestamp string in callback function comes in GMT time rather than local time. However when the internet is available during the 1st email thats being sent in the setup function then timestamp is in local time. I want to know is there a way to refresh the timezone setting so that timestamp is fetched in local time when internet becomes available? Both session and smtp are global variables. Will setting the below again inside loop function, refresh the timezone setting and subsequent emails sent after internet is available will be in local time? session.time.gmt_offset = 0; //EST 12 + ABS(offset)
session.time.day_light_offset = 0;
session.time.timezone_env_string = "EST5EDT,M3.2.0,M11.1.0"; |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 12 replies
-
If you use ESP32 and your device woke up from sleep which time zone environment will be lost, please update the library to v3.3.2 when it is available in Arduino Library Manager. |
Beta Was this translation helpful? Give feedback.
-
I tried adding this in Sendmail... but still it shows GMT time when internet becomes available. However when Internet is available from the beginning then timestamp is shown in local timezone. |
Beta Was this translation helpful? Give feedback.
-
After calling configTime, (you need to connect to internet) and wait until valid time becomes available. while (time(nullptr) < 1690559334)
{
delay(1);
} The value 1690559334 is current timestamp, or you can use any large number of seconds for a year like 60 * 60 * 24 * 365. |
Beta Was this translation helpful? Give feedback.
You can update to v3.3.5.
It gains 2.2k more free flash space when ENABLE_NTP_TIME was undefined.