ESP32 Mail Client issue

Hello all,

I am having issue while trying to send an email notification while using and esp32 and a deb-ounce switch.
The first part of the sketch(void setup) will work fine, sending me my email with the message showing up that my esp32 have just booted up as I am starting up the board, but the switch won't trigger the 2nd email to go out, also I am receiving random emails about every minute or so about something triggering the motion( never touches the switch)...
I did try different input for my switch, still the same, I also physically removed the switch, ran a jumper wire directly from the input that I was using to the 3.3V so it stay high, still receiving random message from the void loop...

I also remove completely the portion that should send the message from the void loop, leaving only the part that will turn on the LED when i press the switch, that LED turn on every single time that I press the switch and then turn off as expected,

Thank You for any help

Sergio

#include <Arduino.h>
#include <WiFi.h>
#include <ESP_Mail_Client.h>

#define WIFI_SSID "*******"
#define WIFI_PASSWORD "*******"

#define SMTP_server "smtp.gmail.com"
#define SMTP_Port 465


#define sender_email "*******@gmail.com"
#define sender_password "gvij fvik adad mbqx"

#define Recipient_email "*******@gmail.com"

#define LED 18
#define BUTTON 4

int button_state = 0;



SMTPSession smtp;

  void setup()
{
  pinMode(LED, OUTPUT);
   
  pinMode(BUTTON, INPUT_PULLUP);
   
  digitalWrite(LED, HIGH);
  
  Serial.begin(9600);
  Serial.println();
  Serial.print("Connecting...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED)
  {
  Serial.print(".");
  delay(200);
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println();
  smtp.debug(1);

  ESP_Mail_Session session;

  session.server.host_name = SMTP_server ;
  session.server.port = SMTP_Port;
  session.login.email = sender_email;
  session.login.password = sender_password;
  session.login.user_domain = "";

  /* Declare the message class */
  SMTP_Message message;

  message.sender.name = "ESP32";
  message.sender.email = sender_email;
  message.subject = "ESP32 BOOT ALERT";
  message.addRecipient("MYR2D2S",Recipient_email);

  //Send HTML message
  String htmlMsg = "<div style=\"color:#FF0000;\"><h1>YOUR MAIN ESP32WROOM SYSTEM HAVE JUST REBOOTED</h1><p>Sent by your ESP32WroomDevKit1</p></div>";
  message.html.content = htmlMsg.c_str();
  message.html.content = htmlMsg.c_str();
  message.text.charSet = "us-ascii";
  message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit; 

 /* 
  //Send simple text message
  String textMsg = "Hello Microcontrollerslab! This is a simple text sent from ESP board";
  message.text.content = textMsg.c_str();
  message.text.charSet = "us-ascii";
  message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit; */


if (!smtp.connect(&session))
  return;


if (!MailClient.sendMail(&smtp, &message))
  Serial.println("Error sending Email, " + smtp.errorReason());
 
}

void loop()
{
  button_state = digitalRead(BUTTON);

  if (button_state == LOW)
  
  digitalWrite(LED, LOW);
  
  ESP_Mail_Session session;


  session.server.host_name = SMTP_server ;
  session.server.port = SMTP_Port;
  session.login.email = sender_email;
  session.login.password = sender_password;
  session.login.user_domain = "";

  /* Declare the message class */
  SMTP_Message message;


  message.sender.name = "ESP32";
  message.sender.email = sender_email;
  message.subject = "ESP32 MOTION ALERT";
  message.addRecipient("MYR2D2S",Recipient_email);

  //Send HTML message
  String htmlMsg = "<div style=\"color:#FF0000;\"><h1>MOTION DETECTED IN THE MAIN LIVING ROOM AREA</h1><p>Sent by your ESP32WroomDevKit1</p></div>";
  message.html.content = htmlMsg.c_str();
  message.html.content = htmlMsg.c_str();
  message.text.charSet = "us-ascii";
  message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit; 

 /* 
  //Send simple text message
  String textMsg = "Hello Microcontrollerslab! This is a simple text sent from ESP board";
  message.text.content = textMsg.c_str();
  message.text.charSet = "us-ascii";
  message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit; */


  if (!smtp.connect(&session))
  return;


  if (!MailClient.sendMail(&smtp, &message))
  Serial.println("Error sending Email, " + smtp.errorReason());

   else                           
    digitalWrite(LED, HIGH); 
    
}





  if (button_state == LOW)
  
  digitalWrite(LED, LOW);
  ESP_Mail_Session session;

Which of the last 2 lines of code above should be executed if button_state is HIGH ?

  1. Neither
  2. Both
  3. Only the first
  4. Only the second

1 Neither, the LED output should stay HIGH, no other codes should run( the ones that keep sending me random email every minute)

Thanks

digitalWrite(LED, LOW);

will be executed only when button_state is LOW

ESP_Mail_Session session;

and all other lines of code in loop() will be executed whether button_state is LOW or HIGH. Is that what you intended ?

If you want a block of code to be dependant on a test then put it in curly brackets. Without the curly brackets only the first code line after the test is controlled by the result of the test. Subsequent code is executed regardless of the outcome of the test

OMG what a rookie error, now working as expected, thank you so much for that reminder

Hi - I am trying to use your code formy own project, what library versions are you using ? - The code isn't compiling for me using a NodeMCU in Arduino 1.8.19.

The original code related to code for an ESP32 not an ESP8266. Have you taken that into account ?

Please post your sketch, using code tags when you do

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.