/** * Created by K. Suwatchai (Mobizt) * * Email: suwatchai@outlook.com * * Github: https://github.com/mobizt/ESP-Mail-Client * * Copyright (c) 2023 mobizt */ // This example showes how the device time was resume after device woke up from sleep. /** Note for library update from v2.x.x to v3.x.x. * * Struct data names changed * * "ESP_Mail_Session" changes to "Session_Config" * "IMAP_Config" changes to "IMAP_Data" * * Changes in the examples * * ESP_Mail_Session session; * to * Session_Config config; * * IMAP_Config config; * to * IMAP_Data imap_data; */ #include #if defined(ESP32) #include #else // Other Client defined here // To use custom Client, define ENABLE_CUSTOM_CLIENT in src/ESP_Mail_FS.h. // See the example Custom_Client.ino for how to use. #endif #include #define WIFI_SSID "" #define WIFI_PASSWORD "" /** For Gmail, to send Email via port 465 (SSL), less secure app option * should be enabled in the account settings. https://myaccount.google.com/lesssecureapps?pli=1 * * Some Gmail user still not able to sign in using account password even above option was set up, * for this case, use "App Password" to sign in instead. * About Gmail "App Password", go to https://support.google.com/accounts/answer/185833?hl=en * * For Yahoo mail, log in to your yahoo mail in web browser and generate app password by go to * https://login.yahoo.com/account/security/app-passwords/add/confirm?src=noSrc * * To use Gmai and Yahoo's App Password to sign in, define the AUTHOR_PASSWORD with your App Password * and AUTHOR_EMAIL with your account email. */ /** The smtp host name e.g. smtp.gmail.com for GMail or smtp.office365.com for Outlook or smtp.mail.yahoo.com */ #define SMTP_HOST "" /** The smtp port e.g. * 25 or esp_mail_smtp_port_25 * 465 or esp_mail_smtp_port_465 * 587 or esp_mail_smtp_port_587 */ #define SMTP_PORT esp_mail_smtp_port_587 // port 465 is not available for Outlook.com /* The log in credentials */ #define AUTHOR_EMAIL "" #define AUTHOR_PASSWORD "" /* Recipient email address */ #define RECIPIENT_EMAIL "" /* Declare the global used SMTPSession object for SMTP transport */ SMTPSession smtp; /* Callback function to get the Email sending status */ void smtpCallback(SMTP_Status status); const char rootCACert[] PROGMEM = "-----BEGIN CERTIFICATE-----\n" "-----END CERTIFICATE-----\n"; void sendEmail() { /* Declare the Session_Config for user defined session credentials */ Session_Config config; /* Set the session config */ config.server.host_name = SMTP_HOST; config.server.port = SMTP_PORT; config.login.email = AUTHOR_EMAIL; config.login.password = AUTHOR_PASSWORD; config.login.user_domain = F("127.0.0.1"); /* Set the NTP config time For times east of the Prime Meridian use 0-12 For times west of the Prime Meridian add 12 to the offset. Ex. American/Denver GMT would be -6. 6 + 12 = 18 See https://en.wikipedia.org/wiki/Time_zone for a list of the GMT/UTC timezone offsets */ config.time.ntp_server = F("pool.ntp.org,time.nist.gov"); config.time.gmt_offset = 0; config.time.day_light_offset = 0; config.time.timezone_env_string = "JST-9"; // for Tokyo // See the timezone environment string list from https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv /* Declare the message class */ SMTP_Message message; /* Set the message headers */ message.sender.name = F("ESP Mail"); // This witll be used with 'MAIL FROM' command and 'From' header field. message.sender.email = AUTHOR_EMAIL; // This witll be used with 'From' header field. message.subject = F("Test sending plain text Email"); message.addRecipient(F("Someone"), RECIPIENT_EMAIL); // This will be used with RCPT TO command and 'To' header field. String textMsg = "This is simple plain text message"; message.text.content = textMsg; /** If the message to send is a large string, to reduce the memory used from internal copying while sending, * you can assign string to message.text.blob by cast your string to uint8_t array like this * * String myBigString = "..... ......"; * message.text.blob.data = (uint8_t *)myBigString.c_str(); * message.text.blob.size = myBigString.length(); * * or assign string to message.text.nonCopyContent, like this * * message.text.nonCopyContent = myBigString.c_str(); * * Only base64 encoding is supported for content transfer encoding in this case. */ /** The Plain text message character set e.g. * us-ascii * utf-8 * utf-7 * The default value is utf-8 */ message.text.charSet = F("us-ascii"); /** The content transfer encoding e.g. * enc_7bit or "7bit" (not encoded) * enc_qp or "quoted-printable" (encoded) * enc_base64 or "base64" (encoded) * enc_binary or "binary" (not encoded) * enc_8bit or "8bit" (not encoded) * The default value is "7bit" */ message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit; // If this is a reply message // message.in_reply_to = ""; // message.references = " "; /** The message priority * esp_mail_smtp_priority_high or 1 * esp_mail_smtp_priority_normal or 3 * esp_mail_smtp_priority_low or 5 * The default value is esp_mail_smtp_priority_low */ message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_low; // message.response.reply_to = "someone@somemail.com"; // message.response.return_path = "someone@somemail.com"; /** The Delivery Status Notifications e.g. * esp_mail_smtp_notify_never * esp_mail_smtp_notify_success * esp_mail_smtp_notify_failure * esp_mail_smtp_notify_delay * The default value is esp_mail_smtp_notify_never */ // 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(F("Message-ID: ")); // For Root CA certificate verification (ESP8266 and ESP32 only) // config.certificate.cert_data = rootCACert; // or // config.certificate.cert_file = "/path/to/der/file"; // config.certificate.cert_file_storage_type = esp_mail_file_storage_type_flash; // esp_mail_file_storage_type_sd // config.certificate.verify = true; // The WiFiNINA firmware the Root CA certification can be added via the option in Firmware update tool in Arduino IDE /* Connect to server with the session config */ // Library will be trying to sync the time with NTP server if time is never sync or set. // This is 10 seconds blocking process. // If time reading was timed out, the error "NTP server time reading timed out" will show via debug and callback function. // You can manually sync time by yourself with NTP library or calling configTime in ESP32 and ESP8266. // Time can be set manually with provided timestamp to function smtp.setSystemTime. /* Connect to the server */ if (!smtp.connect(&config)) { MailClient.printf("Connection error, Status Code: %d, Error Code: %d, Reason: %s", smtp.statusCode(), smtp.errorCode(), smtp.errorReason().c_str()); return; } /* Start sending Email and close the session */ if (!MailClient.sendMail(&smtp, &message)) MailClient.printf("Error, Status Code: %d, Error Code: %d, Reason: %s", smtp.statusCode(), smtp.errorCode(), smtp.errorReason().c_str()); // to clear sending result log // smtp.sendingResult.clear(); MailClient.printf("Free Heap: %d\n", MailClient.getFreeHeap()); } 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 * 0 for no debugging * 1 for basic level debugging * * Debug port can be changed via ESP_MAIL_DEFAULT_DEBUG_PORT in ESP_Mail_FS.h */ smtp.debug(1); /* Set the callback function to get the sending results */ smtp.callback(smtpCallback); sendEmail(); // Sleep for 60 seconds Serial.println("Going to sleep for 60 seconds..."); #if defined(ESP32) esp_sleep_enable_timer_wakeup(60 * 1000000); esp_deep_sleep_start(); #endif } void loop() { } /* 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()) { // MailClient.printf used in the examples is for format printing via debug Serial port // that works for all supported Arduino platform SDKs e.g. AVR, SAMD, ESP32 and ESP8266. // In ESP8266 and ESP32, you can use Serial.printf directly. Serial.println("----------------"); MailClient.printf("Message sent success: %d\n", status.completedCount()); MailClient.printf("Message sent failed: %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); // In case, ESP32, ESP8266 and SAMD device, the timestamp get from result.timestamp should be valid if // your device time was synched with NTP server. // Other devices may show invalid timestamp as the device time was not set i.e. it will show Jan 1, 1970. // You can call smtp.setSystemTime(xxx) to set device time manually. Where xxx is timestamp (seconds since Jan 1, 1970) MailClient.printf("Message No: %d\n", i + 1); MailClient.printf("Status: %s\n", result.completed ? "success" : "failed"); MailClient.printf("Date/Time: %s\n", MailClient.Time.getDateTimeString(result.timestamp, "%B %d, %Y %H:%M:%S").c_str()); MailClient.printf("Recipient: %s\n", result.recipients.c_str()); MailClient.printf("Subject: %s\n", result.subject.c_str()); } Serial.println("----------------\n"); // You need to clear sending result as the memory usage will grow up. smtp.sendingResult.clear(); } }