I'm attempting to build a motion email alert using PIR and nodemcu. I have altered sketch provided by Rui Santos (ESP8266 NodeMCU Send Emails SMTP Server: HTML, Text, Attachments (Arduino) | Random Nerd Tutorials). The unaltered sketch compiles and works when I enter my router and gmail credentials. I took two lines of code that connects to the server and sends the email and placed them into a separate sendTextMsg() function that I call in the loop when the PIR triggers high. I am receiving error in the sendTextMsg() function at the line "if (!MailClient.sendMail(&smtp, &message))" of " 'message' is not declared in thus scope". The function appears to recognize the address of both session and smtp but not message? Any help would be appreciated. Thanks.
#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <ESP_Mail_Client.h>
#define WIFI_SSID "SSID"
#define WIFI_PASSWORD "PASSWORD"
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
//Email credentials
#define AUTHOR_EMAIL "email@gmail.com"
#define AUTHOR_PASSWORD "GMAILPASSWORD"
//Recipient's email
#define RECIPIENT_EMAIL "email@gmail.com"
//The SMTP Session object used for Email sending
SMTPSession smtp;
// Declare the session config data
ESP_Mail_Session session;
// Callback function to get the Email sending status
void smtpCallback(SMTP_Status status);
// Define Nodemcu pins
const int PIR = D1;
const int LED = D4;
void setup(){
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to AP");
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();
//Enable the debug via Serial port; none debug or 0, basic debug or 1
smtp.debug(1);
// Set the callback function to get the sending results
smtp.callback(smtpCallback);
// Declare the session config data
ESP_Mail_Session session;
// Set the session config
session.server.host_name = SMTP_HOST;
session.server.port = SMTP_PORT;
session.login.email = AUTHOR_EMAIL;
session.login.password = AUTHOR_PASSWORD;
session.login.user_domain = "";
// Declare the message class
SMTP_Message message;
//Set the message headers
message.sender.name = "ESP";
message.sender.email = AUTHOR_EMAIL;
message.subject = "ESP Test Email";
message.addRecipient("Name", RECIPIENT_EMAIL);
//Send raw text message
String textMsg = "Hello World! - Sent from ESP board";
message.text.content = textMsg.c_str();
message.text.charSet = "us-ascii";
message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_low;
message.response.notify = esp_mail_smtp_notify_success | esp_mail_smtp_notify_failure | esp_mail_smtp_notify_delay;
//Set the custom message header
message.addHeader("Message-ID: <abcde.fghij@gmail.com>");
// Pin ID
pinMode(PIR, INPUT); //D1 as INPUT
pinMode(LED, OUTPUT); //D4 as OUTPUT
/* Replaced as sendTextMsg function below
-------------------------------------------------------------
// Connect to server with the session config
if (!smtp.connect(&session))
return;*/
/* //Start sending Email and close the session
if (!MailClient.sendMail(&smtp, &message))
Serial.println("Error sending Email, " + smtp.errorReason());
------------------------------------------------------------*/
}
void loop(){
if (digitalRead(PIR) == HIGH){
sendTextMsg();
}
}
void sendTextMsg() {
//Connect to server with the session config
if (!smtp.connect(&session))return;
//Start sending Email and close the session
if (!MailClient.sendMail(&smtp, &message)) //error - "message not declared in this scope"
Serial.println("Error sending Email, " + smtp.errorReason());
}
// Callback function to get the Email sending status
void smtpCallback(SMTP_Status status){
/* Print the current status */
Serial.println(status.info());
// Print the sending result
if (status.success()){
Serial.println("----------------");
ESP_MAIL_PRINTF("Message sent success: %d\n", status.completedCount());
ESP_MAIL_PRINTF("Message sent failled: %d\n", status.failedCount());
Serial.println("----------------\n");
struct tm dt;
for (size_t i = 0; i < smtp.sendingResult.size(); i++){
/* Get the result item */
SMTP_Result result = smtp.sendingResult.getItem(i);
time_t ts = (time_t)result.timestamp;
localtime_r(&ts, &dt);
ESP_MAIL_PRINTF("Message No: %d\n", i + 1);
ESP_MAIL_PRINTF("Status: %s\n", result.completed ? "success" : "failed");
ESP_MAIL_PRINTF("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
ESP_MAIL_PRINTF("Recipient: %s\n", result.recipients);
ESP_MAIL_PRINTF("Subject: %s\n", result.subject);
}
Serial.println("----------------\n");
}
}