// 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.
/** 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 "smtp.office365.com"
#define SMTP_PORT esp_mail_smtp_port_587 // port 465 is not available for Outlook.com
/* Declare the message class */ SMTP_Message message;
/* Set the message headers */ message.sender.name = F("Bsgbsg7's ESP Box"); message.sender.email = AUTHOR_EMAIL; message.subject = F("FIRE!"); message.addRecipient(F("Master"), RECIPIENT_EMAIL);
String textMsg = "Hope you have a good day"; message.text.content = textMsg; /** 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;
/* Connect to the server */ 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());
// to clear sending result log // smtp.sendingResult.clear(); }
voidloop() { }
/* Callback function to get the Email sending status */ voidsmtpCallback(SMTP_Status status) { /* Print the current status */ Serial.println(status.info());
/* Print the sending result */ if (status.success()) { // ESP_MAIL_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("----------------"); ESP_MAIL_PRINTF("Message sent success: %d\n", status.completedCount()); ESP_MAIL_PRINTF("Message sent failed: %d\n", status.failedCount()); Serial.println("----------------\n");
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) ESP_MAIL_PRINTF("Message No: %d\n", i + 1); ESP_MAIL_PRINTF("Status: %s\n", result.completed ? "success" : "failed"); ESP_MAIL_PRINTF("Date/Time: %s\n", MailClient.Time.getDateTimeString(result.timestamp, "%B %d, %Y %H:%M:%S").c_str()); ESP_MAIL_PRINTF("Recipient: %s\n", result.recipients.c_str()); ESP_MAIL_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(); } }