Send email with ethernet shield

Hello folks.

Sorry, if this have been covered before, but I can´t find a solution for my problem. Oh, and I`m a newbie to this, so also sorry for that.

I need to monitor a power supply and wish to receive an email when there is a failure on one of the supplies.
I have already made an simple webserver that works fine, but now, I also would like to add the email notification.

I have tryed to add the code for ESP8266, that I have been using in another project, but cant get it to work. Im using smtp2go.com.

Can anybody please take a look at the code, that I`m using for the webserver, and hopefully give me a hint about where to go?

#include <SPI.h>
#include <Ethernet.h>

#define input_voltage3 A0
#define input_voltage4 A1
#define input_voltage1 A2
#define input_voltage2 A3

const float mvpc1 = 6.55;
const float mvpc2 = 6.55;
const float mvpc3 = 6.65;
const float mvpc4 = 6.65;


float counts = 0;
float mv = 0;

float multiplier = 3 ;

float output1 = 0;
float output2 = 0;
float output3 = 0;
float output4 = 0;

int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;

/******************** ETHERNET SETTINGS ********************/

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x85, 0xD9 };   //physical mac address
byte ip[] = { 192, 168, 1, 185 };                   // ip in lan
byte subnet[] = { 255, 255, 255, 0 };              //subnet mask
byte gateway[] = { 192, 168, 1, 1 };              // default gateway
EthernetServer server(80);                       //server port

void setup()
{

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

  Ethernet.begin(mac, ip, gateway, subnet);  // initialize Ethernet device
  server.begin();                                // start to listen for clients

}

void loop()
{
  EthernetClient client = server.available();    // look for the client


  client.println("<!DOCTYPE html>");      //web page is made using html
  client.println("<html>");
  client.println("<head>");
  client.println("<title>Text on webpage</title>");
  client.println("<meta http-equiv=\"refresh\" content=\"5\">");


  client.println("</head>");
  client.println("<body>");
  client.println(F("<h1>Unit: 5 and 12 volt PSU unit</h1>"));
  client.println(F("<h1>Text on webpage</h1>"));
  client.println(F("<h1>Text on webpage)</h1>"));
  client.println(F("<hr />"));
  client.println(F("<h2>Text on webpage</h2>"));
  client.println(F("<h2>MONITORING: Redundant Powersupply (5V & 12V)</h2>"));
  client.println(F("<hr />"));


  counts = analogRead(input_voltage1);

  mv = counts * mvpc1;

  output1 = (mv * multiplier) / 1000;
  client.println("CURRENT OUTPUT VOLTAGE: PSU 1 5Volt: " + String(output1));
  //  Serial.println("Output1-5 VOLTAGE: " + String(output1));
  client.println(F("<hr />"));


  if (output1 < 4.5)
  {
    client.println(F("<h1><font color ='red' >PSU-1 5Volt FAULTY!!!</h1>"));
    client.println(F("<h2><font color ='black' >Text on webpage</h2>"));
    client.println(F("<h2><font color ='black' >If no error indication on PSU-2, the unit is still alive :-)</h2>"));
    client.println(F("<h2><font color ='black' >Text on webpage</h2>"));
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
  }

  else
  {
    client.println(F("<h1><font color ='green' >PSU-1: 5Volt OK</h1>"));
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
  }

  delay(300);


  counts = analogRead(input_voltage2);

  mv = counts * mvpc2;

  output2 = (mv * multiplier) / 1000;
  client.println("CURRENT OUTPUT VOLTAGE: PSU 2 5Volt: " + String(output2));
  //  Serial.println("Output2-5 VOLTAGE: " + String(output2));
  client.println(F("<hr />"));



  if (output2 < 4.5)
  {
    client.println(F("<h1><font color ='red' >PSU-2 5Volt DEFEKT!</h1>"));
    client.println(F("<h2><font color ='black' >Text on webpage</h2>"));
    client.println(F("<h2><font color ='black' >If no error indication on PSU-1, the unit is still alive :-)</h2>"));
    client.println(F("<h2><font color ='black' >Text on webpage</h2>"));
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, LOW);
  }

  else
  {
    client.println(F("<h1><font color ='green' >PSU-2: 5Volt OK</h1>"));
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, HIGH);
  }

  delay(300);

  counts = analogRead(input_voltage3);

  mv = counts * mvpc3;

  output3 = (mv * multiplier) / 1000;
  client.println("CURRENT OUTPUT VOLTAGE: PSU 1 12Volt: " + String(output3));
  //  Serial.println("Output3-12 VOLTAGE: " + String(output3));
  client.println(F("<hr />"));



  if (output3 < 11.5)
  {
    client.println(F("<h1><font color ='red' >PSU-1 12Volt DEFEKT!</h1>"));
    client.println(F("<h2><font color ='black' >Text on webpage</h2>"));
    client.println(F("<h2><font color ='black' >If no error indication on PSU-2, the unit is still alive :-)</h2>"));
    client.println(F("<h2><font color ='black' >Text on webpage</h2>"));
  }

  else
  {
    client.println(F("<h1><font color ='green' >PSU-1: 12Volt OK</h1>"));
  }

  delay(300);


  counts = analogRead(input_voltage4);

  mv = counts * mvpc4;

  output4 = (mv * multiplier) / 1000;
  client.println("CURRENT OUTPUT VOLTAGE: PSU 2 12 Volt: " + String(output4));
  //  Serial.println("Output4-12 VOLTAGE: " + String(output4));
  client.println(F("<hr />"));



  if (output4 < 11.5)
  {
    client.println(F("<h1><font color ='red' >Text on webpage</h1>"));
    client.println(F("<h2><font color ='black' >Text on webpage</h2>"));
    client.println(F("<h2><font color ='black' >If no error indication on PSU-1, the unit is still alive :-)</h2>"));
    client.println(F("<h2><font color ='black' >Text on webpage</h2>"));
  }

  else
  {
    client.println(F("<h1><font color ='green' >PSU-2: 12Volt OK</h1>"));
  }

  delay(300);



  client.println("</body>");
  client.println("</html>");
  delay(300);         // giving time to receive the data
  client.stop();

}

I would like to get the email notification, when LED1 go LOW or when output1 is below 4.5:

 if (output1 < 4.5)
  {
    client.println(F("<h1><font color ='red' >PSU-1 5Volt FAULTY!!!</h1>"));
    client.println(F("<h2><font color ='black' >Text on webpage</h2>"));
    client.println(F("<h2><font color ='black' >If no error indication on PSU-2, the unit is still alive :-)</h2>"));
    client.println(F("<h2><font color ='black' >Text on webpage</h2>"));
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
  }

  else
  {
    client.println(F("<h1><font color ='green' >PSU-1: 5Volt OK</h1>"));
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
  }

show your code for the esp8266. it should work if you replace WiFiClient with EthernetClient

Sorry, yes you are right!

Here is the code I have added:

int sensorOutput1Value = 0;  
int val = LOW;



/******************** ETHERNET SETTINGS ********************/

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x85, 0xD9 };   //physical mac address
byte ip[] = { 192, 168, 1, 185 };                   // ip in lan
byte subnet[] = { 255, 255, 255, 0 };              //subnet mask
byte gateway[] = { 192, 168, 1, 1 };              // default gateway
EthernetServer server(80);                       //server port

char EMAIL_FROM[] = "************";
char EMAIL_PASSWORD[] =  "****************";
char EMAIL_TO[] = "*************";
char SUBJECT[]  = "***************";
char EMAIL_CONTENT[] = "**************************";

// We'll need your EMAIL_FROM and its EMAIL_PASSWORD base64 encoded, you can use https://www.base64encode.org/
#define EMAIL_FROM_BASE64 "*******************"
#define EMAIL_PASSWORD_BASE64 "****************"

#define HOST     "mail.smtp2go.com"     // Find/Google your email provider's SMTP outgoing server name for unencrypted email

#define PORT     2525                     // Find/Google your email provider's SMTP outgoing port for unencrypted email

int count = 0; // we'll use this int to keep track of which command we need to send next
bool send_flag = false; // we'll use this flag to know when to send the email commands

void setup()
{

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

  Ethernet.begin(mac, ip, gateway, subnet);  // initialize Ethernet device
  server.begin();                                // start to listen for clients

}

Here I have added send_flag = true; in the loop:

  if (output1 < 4.5)
  {
    client.println(F("<h1><font color ='red' >PSU-1 5Volt FAULTY!!!</h1>"));
    client.println(F("<h2><font color ='black' >A.S.A.P. Replace the PSU-1 with RS partno.: 644-7067</h2>"));
    client.println(F("<h2><font color ='black' >If no error indication on PSU-2, the unit is still alive :-)</h2>"));
    client.println(F("<h2><font color ='black' >**********************</h2>"));
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    send_flag = true;
  }

In order to send an email, you will need a SMTP client library that works with ESP8266. Unrelated suggestion: I would recommend you to verify that the EthernetClient is actually valid (use client.connected()) before writing to it.

Snapjack:
Sorry, yes you are right!

Here is the code I have added:

I don't see any code for sending e-mail in your snippets

@Danois90, Mega is the board in this topic

Juraj:
@Danois90, Mega is the board in this topic

I (don't) see.. :wink:

Thanks guys

I have tried different codes from the ESP8266 code in the loop, but with no luck. :confused:
Since I´m not an expert in programming, could you please give me some examples?

Here is an example.

Hi

Thank you so fare for your assistance!
I have managed to get it to work now, but there are still a small problem.

I only want to send the email once, when there is a fault on the power supply output.
Can you please help with that one?

I have tryed with break and client.stop commands, but that didn´t work.
if (output1 < 4.5)
{
if(sendEmail()) Serial.println(F(“Email sent”));
else Serial.println(F(“Email failed”));
delay(300);
client.stop();
}

if (output1 < 4.5)
      {
      if(sendEmail()) Serial.println(F("Email sent"));
      else Serial.println(F("Email failed"));
      delay(300); 
      client.stop();
  }

  byte inChar;
 
  inChar = Serial.read();
 
  if(inChar == 'e')
  {
      if(sendEmail()) Serial.println(F("Email sent"));
      else Serial.println(F("Email failed"));
  }
}
 
byte sendEmail()

{
  byte thisByte = 0;
  byte respCode;
 
  if(client.connect(server,port) == 1) {
    Serial.println(F("connected"));
  } else {
    Serial.println(F("connection failed"));
    return 0;
  }
 
  if(!eRcv()) return 0;
 
  Serial.println(F("Sending hello"));
// replace 1.2.3.4 with your Arduino's ip
  client.println("EHLO 192, 168, 1, 185");
  if(!eRcv()) return 0;
 
  Serial.println(F("Sending auth login"));
  client.println("auth login");
  if(!eRcv()) return 0;
 
  Serial.println(F("Sending User"));
// Change to your base64 encoded user
  client.println("QUxBUk1fVmluZHVlQG9lcmlzLmRr");
 
  if(!eRcv()) return 0;
 
  Serial.println(F("Sending Password"));
// change to your base64 encoded password
  client.println("QmV0YWNhbTExMg==");
 
  if(!eRcv()) return 0;
 
// change to your email address (sender)
  Serial.println(F("Sending From"));
  client.println("MAIL From: <alarm_vindue@oeris.dk>");
  if(!eRcv()) return 0;
 
// change to recipient address
  Serial.println(F("Sending To"));
  client.println("RCPT To: <jeor112@gmail.com>");
  if(!eRcv()) return 0;
 
  Serial.println(F("Sending DATA"));
  client.println("DATA");
  if(!eRcv()) return 0;
 
  Serial.println(F("Sending email"));
 
// change to recipient address
  client.println("To: You <jeor112@gmail.com>");
 
// change to your address
  client.println("From: Me <alarm_vindue@oeris.dk>");
 
  client.println("Subject: Arduino email test\r\n");
 
  client.println("This is from my Arduino!");
 
  client.println(".");
 
  if(!eRcv()) return 0;
 
  Serial.println(F("Sending QUIT"));
  client.println("QUIT");
  if(!eRcv()) return 0;
 
  client.stop();
 
  Serial.println(F("disconnected"));
 
  return 1;
}
 
byte eRcv()
{
  byte respCode;
  byte thisByte;
  int loopCount = 0;
 
  while(!client.available()) {
    delay(1);
    loopCount++;
 
    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
      client.stop();
      Serial.println(F("\r\nTimeout"));
      return 0;
    }
  }
 
  respCode = client.peek();
 
  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }
 
  if(respCode >= '4')
  {
    efail();
    return 0;  
  }
 
  return 1;
}
 
 
void efail()
{
  byte thisByte = 0;
  int loopCount = 0;
 
  client.println(F("QUIT"));
 
  while(!client.available()) {
    delay(1);
    loopCount++;
 
    // if nothing received for 10 seconds, timeout
    if(loopCount > 10000) {
      client.stop();
      Serial.println(F("\r\nTimeout"));
      return;
    }
  }
 
  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }
 
  client.stop();
 
  Serial.println(F("disconnected"));
}

Thanks in advance!

Can you please help with that one?

You need to define a boolean variable, and set it to the proper value when the error occurs, and reset it at some undefined (that is, you haven't defined when) point.

Only send the email if the value is true (or false). After sending the email, set the value to false (or true, if that makes more sense).

bool needToSendEmail = true;
if(errorHappened())
{
   if(needToSendEmail)
     sendEmail();
   needToSendEmail = false;
}

Hi PaulS

Thank you, but I´m not sure if I understand you correctly.

I have inserted your suggestion, but it still keep sending emails.

I write this before void.setup:

float counts = 0;
float mv = 0;

float multiplier = 3 ;

float output1 = 0;
float output2 = 0;
float output3 = 0;
float output4 = 0;

int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;


byte byteRead;
int count = 0; 
bool send_flag = true; 
boolean do_next();
bool needToSendEmail = true;

and this in the void.loop:

// ########## EMAIL ##########

    if(output1 < 4.5)
    {
      if (needToSendEmail)
      sendEmail();
      needToSendEmail = false;
    }

 
  byte inChar;
 
  inChar = Serial.read();
 
  if(inChar == 'e')
  {
      if(sendEmail()) Serial.println(F("Email sent"));
      else Serial.println(F("Email failed"));
  }
}
 
byte sendEmail()

It’s probably not that easy… :confused:

Why do you call sendEmail() in two places in loop()? Why does one of them depend on not having sent an email already, while the other one does not?

This block needs some curly brackets:

if (needToSendEmail)
sendEmail();
needToSendEmail = false;

Or even better, you could modify sendMail:

byte sendMail()
{
  if (!needToSendMail) return;

  //Rest of sendMail's code here

  if (sendMailSuccess) needToSendMail = false;
}

Then you can call sendMail() all you want but it will only send a mail once - until needToSendMail is reset to true (should probably happen when the voltage is good again).

Hi

What do you meen

This block needs some curly brackets:

It look like this:

// ########## EMAIL ##########

if (output1 < 4.5)

{
if (needToSendEmail)
sendEmail();
needToSendEmail = true;
}

I have tried with byte sendEmail(), but no luck with that.

Would it make sense for you, if I post the full code?

Well, here it is anyway ;D

#include <SPI.h>
#include <Ethernet.h>

#define input_voltage3 A0
#define input_voltage4 A1
#define input_voltage1 A2
#define input_voltage2 A3

const float mvpc1 = 6.55;
const float mvpc2 = 6.55;
const float mvpc3 = 6.65;
const float mvpc4 = 6.65;


float counts = 0;
float mv = 0;

float multiplier = 3 ;

float output1 = 0;
float output2 = 0;
float output3 = 0;
float output4 = 0;

int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;

int val = LOW;
byte byteRead;
int count = 0; 
bool send_flag = true; 
boolean do_next();
bool needToSendEmail = true;


// this must be unique
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x85, 0xD9 };
// change network settings to yours
IPAddress ip( 192, 168, 1, 185 );
IPAddress gateway( 192, 168, 1, 1 );
IPAddress subnet( 255, 255, 255, 0 );

char server[] = "mail.smtp2go.com";
int port = 2525;

EthernetClient client;
EthernetServer Server(80);                       //server port

void setup()
{
  Serial.begin(115200);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  delay(2000);
  Serial.println(F("Ready. Press 'e' to send."));

}

void loop() {

  EthernetClient client = Server.available();    
  // send a standard http response header


  client.println("<!DOCTYPE html>");      //web page is made using html
  client.println("<html>");
  client.println("<head>");
  client.println("<title>PSU_DCAR_Watchout_Converters</title>");
  client.println("<meta http-equiv=\"refresh\" content=\"5\">");


  client.println("</head>");
  client.println("<body>");
  client.println(F("<h1>Unit: 5 and 12 volt PSU unit</h1>"));
  client.println(F("<h1>Location: D-CAR / RACK G1</h1>"));
  client.println(F("<h1>Affected devices: ************</h1>"));
  client.println(F("<hr />"));
  client.println(F("<h2>PSU unit IP Adress: ************</h2>"));
  client.println(F("<h2>MONITORING: Redundant Powersupply (5V & 12V)</h2>"));
  client.println(F("<hr />"));


  counts = analogRead(input_voltage1);

  mv = counts * mvpc1;

  output1 = (mv * multiplier) / 1000;
  client.println("CURRENT OUTPUT VOLTAGE: PSU 1 5Volt: " + String(output1));
  //  Serial.println("Output1-5 VOLTAGE: " + String(output1));
  client.println(F("<hr />"));



  if (output1 < 4.5)
  {
    client.println(F("<h1><font color ='red' >PSU-1 5Volt FAULTY!!!</h1>"));
    client.println(F("<h2><font color ='black' >***************</h2>"));
    client.println(F("<h2><font color ='black' >If no error indication on PSU-2, the unit is still alive :-)</h2>"));
    client.println(F("<h2><font color ='black' >***************</h2>"));
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    Serial.println("PSU1 5V dead");


  }




  else
  {
    client.println(F("<h1><font color ='green' >PSU-1: 5Volt OK</h1>"));
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
  }


  delay(300);


  counts = analogRead(input_voltage2);

  mv = counts * mvpc2;

  output2 = (mv * multiplier) / 1000;
  client.println("CURRENT OUTPUT VOLTAGE: PSU 2 5Volt: " + String(output2));
  //  Serial.println("Output2-5 VOLTAGE: " + String(output2));
  client.println(F("<hr />"));



  if (output2 < 4.5)
  {
    client.println(F("<h1><font color ='red' >PSU-2 5Volt Faulty!</h1>"));
    client.println(F("<h2><font color ='black' >***************</h2>"));
    client.println(F("<h2><font color ='black' >If no error indication on PSU-1, the unit is still alive :-)</h2>"));
    client.println(F("<h2><font color ='black' >************************</h2>"));
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, LOW);
  }

  else
  {
    client.println(F("<h1><font color ='green' >PSU-2: 5Volt OK</h1>"));
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, HIGH);
  }

  delay(300);

  counts = analogRead(input_voltage3);

  mv = counts * mvpc3;

  output3 = (mv * multiplier) / 1000;
  client.println("CURRENT OUTPUT VOLTAGE: PSU 1 12Volt: " + String(output3));
  //  Serial.println("Output3-12 VOLTAGE: " + String(output3));
  client.println(F("<hr />"));



  if (output3 < 11.5)
  {
    client.println(F("<h1><font color ='red' >PSU-1 12Volt Faulty!</h1>"));
    client.println(F("<h2><font color ='black' >********************</h2>"));
    client.println(F("<h2><font color ='black' >If no error indication on PSU-2, the unit is still alive :-)</h2>"));
    client.println(F("<h2><font color ='black' >*****************</h2>"));
  }

  else
  {
    client.println(F("<h1><font color ='green' >PSU-1: 12Volt OK</h1>"));
  }

  delay(300);


  counts = analogRead(input_voltage4);

  mv = counts * mvpc4;

  output4 = (mv * multiplier) / 1000;
  client.println("CURRENT OUTPUT VOLTAGE: PSU 2 12 Volt: " + String(output4));
  //  Serial.println("Output4-12 VOLTAGE: " + String(output4));
  client.println(F("<hr />"));



  if (output4 < 11.5)
  {
    client.println(F("<h1><font color ='red' >PSU-2 12Volt Faulty!</h1>"));
    client.println(F("<h2><font color ='black' >******************</h2>"));
    client.println(F("<h2><font color ='black' >If no error indication on PSU-1, the unit is still alive :-)</h2>"));
    client.println(F("<h2><font color ='black' >*************************</h2>"));
  }

  else
  {
    client.println(F("<h1><font color ='green' >PSU-2: 12Volt OK</h1>"));
  }

  delay(300);



  client.println("</body>");
  client.println("</html>");
  delay(300);         // giving time to receive the data


  client.stop();



  // ########## EMAIL ##########

  if (output1 < 4.5)

  {
    if (needToSendEmail)
      sendEmail();
    needToSendEmail = true;
  }


  byte inChar;

  inChar = Serial.read();

  if (inChar == 'e')
  {
    if (sendEmail()) Serial.println(F("Email sent"));
    else Serial.println(F("Email failed"));
  }
}

byte sendEmail()

{
  byte thisByte = 0;
  byte respCode;

  if (client.connect(server, port) == 1) {
    Serial.println(F("connected"));
  } else {
    Serial.println(F("connection failed"));
    return 0;
  }

  if (!eRcv()) return 0;

  Serial.println(F("Sending hello"));
  // replace 1.2.3.4 with your Arduino's ip
  client.println("EHLO 192, 168, 1, 185");
  if (!eRcv()) return 0;

  Serial.println(F("Sending auth login"));
  client.println("auth login");
  if (!eRcv()) return 0;

  Serial.println(F("Sending User"));
  // Change to your base64 encoded user
  client.println("*******************************");

  if (!eRcv()) return 0;

  Serial.println(F("Sending Password"));
  // change to your base64 encoded password
  client.println("**********************");

  if (!eRcv()) return 0;

  // change to your email address (sender)
  Serial.println(F("Sending From"));
  client.println("MAIL From: <************@************>");
  if (!eRcv()) return 0;

  // change to recipient address
  Serial.println(F("Sending To"));
  client.println("RCPT To: <************@****************>");
  if (!eRcv()) return 0;

  Serial.println(F("Sending DATA"));
  client.println("DATA");
  if (!eRcv()) return 0;

  Serial.println(F("Sending email"));

  // change to recipient address
  client.println("To: You <************@******************>");

  // change to your address
  client.println("From: Me <***********@*************>");

  client.println("Subject: Arduino email test\r\n");

  client.println("This is from my Arduino!");

  client.println(".");

  if (!eRcv()) return 0;

  Serial.println(F("Sending QUIT"));
  client.println("QUIT");
  if (!eRcv()) return 0;

  client.stop();

  Serial.println(F("disconnected"));

  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;
  int loopCount = 0;

  while (!client.available()) {
    delay(1);
    loopCount++;

    // if nothing received for 10 seconds, timeout
    if (loopCount > 10000) {
      client.stop();
      Serial.println(F("\r\nTimeout"));
      return 0;
    }
  }

  respCode = client.peek();

  while (client.available())
  {
    thisByte = client.read();
    Serial.write(thisByte);
  }

  if (respCode >= '4')
  {
    efail();
    return 0;
  }

  return 1;
}


void efail()
{
  byte thisByte = 0;
  int loopCount = 0;

  client.println(F("QUIT"));

  while (!client.available()) {
    delay(1);
    loopCount++;

    // if nothing received for 10 seconds, timeout
    if (loopCount > 10000) {
      client.stop();
      Serial.println(F("\r\nTimeout"));
      return;
    }
  }

  while (client.available())
  {
    thisByte = client.read();
    Serial.write(thisByte);
  }

  client.stop();

  Serial.println(F("disconnected"));
}

It's still wrong, and you are probably still receiving numerous mails for the same event. The backets should have been:

if (needToSendEmail)
{
  sendEmail();
  needToSendEmail = false; //Should only be set to false is sendMail succeeds
}

But I would rather opt for the method where sendMail is modified, since this would cause less code repetition.

The easy way

I test the various provider with the library

Here the documentation and how to send attachments

https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/

Bye

Look at your other “if” statements in your program. Do they have the braces { } around the outside of the if like you have written…

  {
    if (needToSendEmail)
      sendEmail();
    needToSendEmail = true;
  }

Or perhaps somewhere else…

  if (output1 < 4.5)
  {
    client.println(F("<h1><font color ='red' >PSU-1 5Volt FAULTY!!!</h1>"));
    client.println(F("<h2><font color ='black' >***************</h2>"));
    client.println(F("<h2><font color ='black' >If no error indication on PSU-2, the unit is still alive :-)</h2>"));
    client.println(F("<h2><font color ='black' >***************</h2>"));
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    Serial.println("PSU1 5V dead");
  }

What do you surmise perhaps is the purpose of the braces { } is to indicate?

Also, you need to set needToSendEmail FALSE after you have sent it. Don’t you see how doing this will stop you sending an e-Mail for a second time?

Then, somewhere else in your code, when you detect a transition INTO an error condition you need to set needToSendEmail TRUE.