Below is my ide code that i reached, i wanted to send an email started from specified date that i choose every month, every 3 months and every 6 months, its like a reminder for a preventive maintenance for an industrial machine, the problem is that i recieve all the mail all in once , infinity of mails , i checked the loop's condition and seemed fine , i didn't know where's the problem from
#include <WiFi.h>
#include <ESP_Mail_Client.h>
#include <time.h>
// Wi-Fi credentials
const char* ssid = "Orange-BFAA";
const char* password = "topnetbnh";
// Email credentials
const char* smtp_server = "smtp.gmail.com";
const int smtp_port = 465; // Use 587 for TLS
const char* email_sender_account = "";
const char* email_sender_password = ""; // Use App Password if 2FA is enabled
const char* email_recipient = "";
// Time intervals (in seconds)
const unsigned long one_month = 30 * 24 * 60 * 60;
const unsigned long three_months = 3 * one_month;
const unsigned long six_months = 6 * one_month;
// Time tracking
time_t start_time;
time_t last_one_month_email = 0;
time_t last_three_months_email = 0;
time_t last_six_months_email = 0;
// SMTP session and message
SMTPSession smtp;
ESP_Mail_Session session;
// Time zone offset for Tunisia (UTC+1)
const int timeZoneOffset = 3600; // in seconds
// Starting date and time (example: 1st May 2024, 12:00:00)
int start_year = 2024;
int start_month = 5;
int start_day = 1;
int start_hour = 12;
int start_minute = 0;
int start_second = 0;
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Set up time with NTP
configTime(timeZoneOffset, 0, "pool.ntp.org", "time.nist.gov");
if (!waitForSync()) {
Serial.println("Failed to sync time with NTP server. Setting time manually.");
setStartTime();
} else {
Serial.println("Time synchronized with NTP server.");
}
// Set SMTP server and login credentials
smtp.callback(smtpCallback);
session.server.host_name = smtp_server;
session.server.port = smtp_port;
session.login.email = email_sender_account;
session.login.password = email_sender_password;
session.login.user_domain = "";
}
void loop() {
time_t current_time;
time(¤t_time);
if ((current_time - start_time) >= one_month && (current_time - last_one_month_email) >= one_month) {
sendEmail("One Month Reminder", "This is your monthly reminder email.");
last_one_month_email = current_time;
}
if ((current_time - start_time) >= three_months && (current_time - last_three_months_email) >= three_months) {
sendEmail("Three Months Reminder", "This is your three-monthly reminder email.");
last_three_months_email = current_time;
}
if ((current_time - start_time) >= six_months && (current_time - last_six_months_email) >= six_months) {
sendEmail("Six Months Reminder", "This is your six-monthly reminder email.");
last_six_months_email = current_time;
}
delay(1000); // Run the loop every second
}
bool waitForSync() {
time_t now;
time(&now);
for (int i = 0; i < 30 && now < 8 * 3600 * 2; i++) {
delay(500);
time(&now);
}
return now > 8 * 3600 * 2;
}
void setStartTime() {
struct tm start_tm = {0};
start_tm.tm_year = start_year - 1900; // years since 1900
start_tm.tm_mon = start_month - 1; // months since January
start_tm.tm_mday = start_day;
start_tm.tm_hour = start_hour;
start_tm.tm_min = start_minute;
start_tm.tm_sec = start_second;
start_time = mktime(&start_tm);
}
void sendEmail(const char* subject, const char* message_body) {
SMTP_Message message;
message.sender.name = "ESP32";
message.sender.email = email_sender_account;
message.subject = subject;
message.addRecipient("Recipient", email_recipient);
message.text.content = message_body;
message.text.charSet = "utf-8";
message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
if (!smtp.connected()) {
smtp.connect(&session);
}
if (!MailClient.sendMail(&smtp, &message)) {
Serial.println("Error sending email: " + smtp.errorReason());
} else {
Serial.println("Email sent successfully");
}
// Clear all data from the message to prepare for the next one
message.text.content = "";
message.subject = "";
message.clearRecipients();
}
// Callback function to get the email sending status
void smtpCallback(SMTP_Status status) {
Serial.println(status.info());
if (status.success()) {
Serial.println("----------------");
Serial.println("Message sent successfully");
Serial.println("----------------");
}
}