//send_email.ino
#include <Arduino.h>
#include <ESP8266WiFi.h>
//---include these lines for sending email----
#include <ESP_Mail_Client.h>
String my_nickname="My ESP8266-01";
String my_email="YourYahooEmail@yahoo.com";
String my_password="YourYahooAppPassword";
//---- done with email include lines ----
//--------------------
void setup(){
//--------------------
  Serial.begin(115200);
  Serial.println();
  connect_to_router("RouterSSID", "RouterPassword");
  //Include these 4 lines to send the email:
  String recipient="recipientemail";
  String subj="This is the subject line";
  String email_body="This is the body of the email";
send_email(my_nickname,my_email,my_password,recipient,subj,email_body);
}
//--------------------
void send_email(String nickname,String from,String password,String to,String subj,String msg){
//--------------------
  SMTPSession smtp;
  MailClient.networkReconnect(true);
  smtp.debug(1);
  Session_Config config;
  config.server.host_name = "smtp.mail.yahoo.com";
  config.server.port = 465;
  config.login.email = from;
  config.login.password = password;
  config.login.user_domain = "yahoo.com";
  config.time.ntp_server = "pool.ntp.org,time.nist.gov";
  SMTP_Message message;
  message.sender.name = nickname;
  message.sender.email = from;
  message.subject = subj;
  message.addRecipient("Someone",to);
  message.text.content = msg;
  message.text.charSet = F("us-ascii");
  message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
  //high,normal,low
  message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_high;
  message.addHeader("Message-ID: <"+from+">");
  if (!smtp.connect(&config)){
    ESP_MAIL_PRINTF("Connection error, Status Code: %d, Error Code: %d, Reason: %s", smtp.statusCode(), smtp.errorCode(), smtp.errorReason().c_str());
    return;
  }
  if (!smtp.isLoggedIn()) {
    Serial.println("\nNot yet logged in.");
  }
  else
  {
    if (smtp.isAuthenticated())
      Serial.println("\nSuccessfully logged in.");
    else
      Serial.println("\nConnected with no Auth.");
  }

  /* Start sending Email and close the session */
  if (!MailClient.sendMail(&smtp, &message))
    ESP_MAIL_PRINTF("Error, Status Code: %d, Error Code: %d, Reason: %s", smtp.statusCode(), smtp.errorCode(), smtp.errorReason().c_str());
  }
//--------------------
void connect_to_router(String ssid,String password){
//--------------------  
  Serial.print("Connecting to WiFi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");
  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();
}
//--------------------
void loop(){}
//-------------------- 

