help emailing different messages

Hi, this is my first post. I am a total noob, please be nice.

I am working on one of those projects where a plant emails you if it needs watering. Somehow I have managed so far to get the sensor sending info to the UNO, and connect to an smtp server with the ethernet shield. I want to get different messages depending on the needs of the plant. Example, "Water me urgently!", Water me.", "Thanks for watering me", "You over watered me".

So far what I have is: When a very low humidity level is detected, the sendmeEmail() function is called. This function simply sends me an email with the body saying "URGENT: Water me!". This is done with the line client.write("URGENT! Water me!\r\n");

But I need the message body to vary, and say the other conditions. How can I pass a specific condition to the sendmeEmail() function and have it email me different body messages depending on each condition? I'm sure it's not hard but my noobiness took over at this point, any help is appreciated. Cheers!

Here is the emailing function. I want the topic and body to be able be variable. How?

void sendmeEmail(){
  
Ethernet.begin(mac, ip, gateway, gateway, subnet);

{
if(sendEmail()) Serial.println("Email sent");
else Serial.println("Email failed");

}}

byte sendEmail()
{
  byte thisByte = 0;
  byte respCode;
  
  if (client.connect(server,2525)) {
    Serial.println("connected");
  } else {
    Serial.println("connection failed");
    return 0;
  }

  if(!eRcv()) return 0;

// change this ip to your public ip
 client.write("helo 1.2.3.4\r\n");

  if(!eRcv()) return 0;

// change this
  client.write("MAIL From: <xxx@gmail.com>\r\n");
  
  if(!eRcv()) return 0;

// change this  
  client.write("RCPT To: <xxx@gmail.com>\r\n");

  if(!eRcv()) return 0;

  client.write("DATA\r\n");

  if(!eRcv()) return 0;

//change this
  client.write("To: You <xxx@gmail.com>\r\n");

// change this
  client.write("From: Me <xxx@gmail.com>\r\n");
  
  client.write("Subject: Plant report: URGENT! Water me!\r\n");
  
  client.write("URGENT! Water me!\r\n");
 
  client.write(".\r\n");

  if(!eRcv()) return 0;

  client.write("QUIT\r\n");

  if(!eRcv()) return 0;

  client.stop();
  Serial.println("disconnected");
  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;
  
  while(!client.available()) delay(1);

  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;

  client.write("QUIT\r\n");

  while(!client.available()) delay(1);

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

  client.stop();
  Serial.println("disconnected");
}

You could add arguments to the sendEmail() function so that you can pass in the subject line and message body, and provide different values depending on the circumstances.

Thanks Peter. That's exactly what I need. I tried but my knowledge of programing is quite limited. I am not sure how I could pass these (strings?) to the function and have that text replaced by them. I've searched quite a bit on google but wasn't able to find out how to do that. Any leads? Cheers

byte sendEmail(char *subject, char *body)
{
    ... 
    // client.write("Subject: Plant report: URGENT! Water me!\r\n");
    client.write("Subject: ");
    client.write(subject);
    client.write("\r\n");
    ...
    // client.write("URGENT! Water me!\r\n");
    client.write(body);
    client.write("\r\n");
    client.write(".\r\n");
}

...

sendEmail("Plant report: URGENT! Water me!", "I'm very thirsty!");

...

sendEmail("Plant report: Thank you!", "Thanks for watering me.");

You get the idea.

Ha! I tried a few things similar to that but didn't write 'bite' at the beginning. I'll give that a try and tell you how it went. Thanks PeterH!

This is my code now PeterH, but it says "'subject' was not declared in this scope"

void sendmeEmail(char *subject, char *body)

{
  
Ethernet.begin(mac, ip, gateway, gateway, subnet);

{
if(sendEmail()) Serial.println("Email sent");
else Serial.println("Email failed");

}}

byte sendEmail()
{
  byte thisByte = 0;
  byte respCode;
  
  if (client.connect(server,2525)) {
    Serial.println("connected");
  } else {
    Serial.println("connection failed");
    return 0;
  }

  if(!eRcv()) return 0;

// change this ip to your public ip
 client.write("helo 1.2.3.4\r\n");

  if(!eRcv()) return 0;

// change this
  client.write("MAIL From: <xxx@gmail.com>\r\n");
  
  if(!eRcv()) return 0;

// change this  
  client.write("RCPT To: <xxx@gmail.com>\r\n");

  if(!eRcv()) return 0;

  client.write("DATA\r\n");

  if(!eRcv()) return 0;

//change this
  client.write("To: You <xxx@gmail.com>\r\n");

// change this
  client.write("From: Me <xxx@gmail.com>\r\n");
  
  //client.write("Subject: Plant report: URGENT! Water me!\r\n");
    client.write("Subject: ");
    client.write(subject);
    client.write("\r\n");
  
  //client.write("URGENT! Water me!\r\n");
    client.write(body);
    client.write("\r\n");
    client.write(".\r\n");
 
  client.write(".\r\n");

  if(!eRcv()) return 0;

  client.write("QUIT\r\n");

  if(!eRcv()) return 0;

  client.stop();
  Serial.println("disconnected");
  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;
  
  while(!client.available()) delay(1);

  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;

  client.write("QUIT\r\n");

  while(!client.available()) delay(1);

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

  client.stop();
  Serial.println("disconnected");
}

You haven't added the arguments to the sendEmail() function. You've added them instead to the sendmeEmail() function, which doesn't do anything with them so far.

I'm not sure why you have two functions for the same job, but the one that actually sends the email needs the extra arguments I showed you. If you want to wrap that with another function that does the same thing, you would need to add the arguments to that function too and then pass them through to the other function that actually sends the email. I don't see the point of having two functions though - why not just call sendEmail() directly? It also seems strange for sendmeEmail() to reinitialise the ethernet interface each time it is called. That should normally only be done once, during setup().

It works Peter!

I removed that second sendmeEmail() function, and also had the ethernet initialize only once now!
Had some trouble today it wouldn't talk to the smtp server. I'm not sure why it worked yesterday and no today but looking at some examples I changed HELO for EHLO and works perfectly now!

Thanks for the help and patience! Cheers!