Looks like commenting out time server seems to fix the second GPIO2 push button issue. Error: Error, Status Code: 554, Error Code: -109, Reason: send body failed. I do get a SMTP 406 Error, but email is still sent. also the below code fixed SMTP Send eMail on Boot.
#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <ESP_Mail_Client.h>
#define WIFI_SSID "REPLACE_WITH_YOUR_SSID"
#define WIFI_PASSWORD "REPLACE_WITH_YOUR_PASSWORD"
#define SMTP_HOST "smtp.mail.com"
#define SMTP_PORT 587
/* The sign in credentials */
#define AUTHOR_EMAIL “YOUR_EMAIL@XXXX.com”
#define AUTHOR_PASSWORD "YOUR_EMAIL_APP_PASS"
/* Recipient's email*/
#define RECIPIENT_EMAIL “RECIPIENTE_EMAIL@XXXX.com”
SMTPSession smtp;
void smtpCallback(SMTP_Status status);
RTC_DATA_ATTR int bootCount = 0;
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
void sendEmail() {
Serial.println("Sending Email...");
smtp.debug(1);
smtp.callback(smtpCallback);
Session_Config config;
config.server.host_name = SMTP_HOST;
config.server.port = SMTP_PORT;
config.login.email = AUTHOR_EMAIL;
config.login.password = AUTHOR_PASSWORD;
config.login.user_domain = "";
//config.time.ntp_server = F("pool.ntp.org,time.nist.gov");
//config.time.gmt_offset = -6;
//config.time.day_light_offset = 0;
SMTP_Message message;
message.sender.name = F("ESP");
message.sender.email = AUTHOR_EMAIL;
message.subject = F("ESP Test Email");
message.addRecipient(F("SMTP"), RECIPIENT_EMAIL);
String textMsg = "Hello World! - Sent from ESP board";
message.text.content = textMsg.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(&config)) {
Serial.println("Connection error");
return;
}
if (!MailClient.sendMail(&smtp, &message)) {
Serial.println("Error sending email");
}
}
void setup() {
Serial.begin(115200);
Serial.println();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("\nConnected to Wi-Fi.");
print_wakeup_reason();
// Check if GPIO2 is HIGH (button pressed) before sending email
if (digitalRead(2) == HIGH) {
sendEmail();
}
// Enable external wake-up on GPIO2, high level trigger
esp_sleep_enable_ext0_wakeup(GPIO_NUM_2, 1);
Serial.println("Going to sleep now");
esp_deep_sleep_start();
}
void loop() {
// Not used due to deep sleep
}
void smtpCallback(SMTP_Status status) {
Serial.println(status.info());
if (status.success()) {
Serial.println("Email sent successfully");
} else {
Serial.print("Error Code: ");
Serial.println(smtp.errorCode());
}
}