Update sendemail_ESP32.ino

This commit is contained in:
gemu2015 2021-02-18 16:57:43 +01:00
parent 8b4703c095
commit 96b3e9172e
1 changed files with 15 additions and 10 deletions

View File

@ -24,7 +24,8 @@ uint8_t num_attachments;
void script_send_email_body(void(*func)(char *)); void script_send_email_body(void(*func)(char *));
String html_content = ""; String html_content = "";
SMTP_Message *email_mptr; SMTP_Message *email_mptr;
SMTPSession smtp; SMTPSession *smtp;
//SMTPSession smtp;
void smtpCallback(SMTP_Status status); void smtpCallback(SMTP_Status status);
//#define DEBUG_EMAIL_PORT //#define DEBUG_EMAIL_PORT
@ -46,7 +47,10 @@ uint16_t SendMail(char *buffer) {
ESP_Mail_Session session; ESP_Mail_Session session;
SMTP_Message message; SMTP_Message message;
email_mptr = &message; email_mptr = &message;
smtp = new SMTPSession();
if (!smtp) {
return 4;
}
// return if not enough memory // return if not enough memory
uint32_t mem = ESP.getFreeHeap(); uint32_t mem = ESP.getFreeHeap();
//AddLog(LOG_LEVEL_INFO, PSTR("heap: %d"),mem); //AddLog(LOG_LEVEL_INFO, PSTR("heap: %d"),mem);
@ -169,9 +173,9 @@ uint16_t SendMail(char *buffer) {
#endif #endif
//smtp.debug(true); //smtp->debug(true);
smtp.debug(false); smtp->debug(false);
// smtp.callback(smtpCallback); // smtp->callback(smtpCallback);
message.clearRecipients(); message.clearRecipients();
message.clearCc(); message.clearCc();
@ -215,17 +219,18 @@ uint16_t SendMail(char *buffer) {
/* Connect to server with the session config */ /* Connect to server with the session config */
delay(0); delay(0);
if (!smtp.connect(&session)) if (!smtp->connect(&session))
goto exit; goto exit;
/* Start sending the Email and close the session */ /* Start sending the Email and close the session */
delay(0); delay(0);
if (!MailClient.sendMail(&smtp, &message, true)) { if (!MailClient.sendMail(smtp, &message, true)) {
Serial.println("Error sending Email, " + smtp.errorReason()); Serial.println("Error sending Email, " + smtp->errorReason());
} }
exit: exit:
if (smtp) delete smtp;
for (uint32_t cnt = 0; cnt < MAX_ATTCHMENTS; cnt++) { for (uint32_t cnt = 0; cnt < MAX_ATTCHMENTS; cnt++) {
if (attachments[cnt]) { if (attachments[cnt]) {
free(attachments[cnt]); free(attachments[cnt]);
@ -351,10 +356,10 @@ if (status.success())
Serial.println("----------------\n"); Serial.println("----------------\n");
struct tm dt; struct tm dt;
for (size_t i = 0; i < smtp.sendingResult.size(); i++) for (size_t i = 0; i < smtp->sendingResult.size(); i++)
{ {
/* Get the result item */ /* Get the result item */
SMTP_Result result = smtp.sendingResult.getItem(i); SMTP_Result result = smtp->sendingResult.getItem(i);
localtime_r(&result.timesstamp, &dt); localtime_r(&result.timesstamp, &dt);
Serial.printf("Message No: %d\n", i + 1); Serial.printf("Message No: %d\n", i + 1);