{"id":93585,"date":"2024-06-14T16:24:54","date_gmt":"2024-06-14T16:24:54","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=93585"},"modified":"2024-06-14T16:24:55","modified_gmt":"2024-06-14T16:24:55","slug":"esp32-send-email-smtp-server-arduino-ide","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-send-email-smtp-server-arduino-ide\/","title":{"rendered":"ESP32 Send Emails using an SMTP Server: HTML, Text, and Attachments (Arduino IDE)"},"content":{"rendered":"\n<p>Learn how to send emails with the ESP32 using an SMTP Server. We&#8217;ll show you how to send a simple email with HTML or raw text and how to send attachments like images and files (<em>.txt<\/em>). The <a rel=\"noreferrer noopener\" aria-label=\"ESP32 board (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32 board<\/a> will be programmed using Arduino IDE.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/ESP32-Send-Emails-SMTP-Server-Text-Attachments.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Send Emails using an SMTP Server: HTML, Text and Attachments (Arduino IDE)\" class=\"wp-image-104421\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/ESP32-Send-Emails-SMTP-Server-Text-Attachments.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/ESP32-Send-Emails-SMTP-Server-Text-Attachments.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/ESP32-Send-Emails-SMTP-Server-Text-Attachments.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/ESP32-Send-Emails-SMTP-Server-Text-Attachments.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgray\"><em>Updated 14 June 202<\/em>4<\/p>\n\n\n\n<p>In this tutorial, we cover the following topics:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#send-email\">ESP32 Send Email (HTML and raw text)<\/a><\/li>\n\n\n\n<li><a href=\"#attachment\">ESP32 Send Emails with Attachments (image and .txt file)<\/a><\/li>\n<\/ul>\n\n\n\n<p>We have a similar tutorial for the ESP8266 board: <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-send-email-smtp-server-arduino\/\">ESP8266 NodeMCU Send Emails using an SMTP Server: HTML, Text, and Attachments (Arduino)<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing SMTP Servers<\/h2>\n\n\n\n<p><strong>SMTP <\/strong>means <em>Simple Mail Transfer Protocol<\/em> and it is an internet standard for email transmission. To send emails using an ESP32, you need to connect it to an SMTP Server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ESP Mail Client Library<\/h3>\n\n\n\n<p>To send emails with the <a aria-label=\"ESP32 (opens in a new tab)\" rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a>, we\u2019ll use the <a href=\"https:\/\/github.com\/mobizt\/ESP-Mail-Client\" target=\"_blank\" rel=\"noreferrer noopener\">ESP-Mail-Client library<\/a>. This library allows the ESP32 to send and receive emails with or without attachments via SMTP and IMAP servers. <\/p>\n\n\n\n<p>In this tutorial, we\u2019ll use SMTP to send an email with and without attachments. As an example, we&#8217;ll send an image (.png) and a text (.txt) file. The files sent via email can be saved in the ESP32 Filesystem (SPIFFS, LittleFS, or FatFs) or a microSD card (not covered in this tutorial).<\/p>\n\n\n\n<p><strong>Installing the ESP-Mail-Client Library<\/strong><\/p>\n\n\n\n<p>Before proceeding with this tutorial, you need to install the <a href=\"https:\/\/github.com\/mobizt\/ESP-Mail-Client\" target=\"_blank\" rel=\"noreferrer noopener\">ESP-Mail-Client library<\/a>.<\/p>\n\n\n\n<p>Go to <strong>Sketch <\/strong>> <strong>Include Library<\/strong> > <strong>Manage Libraries<\/strong> and search for <strong>ESP Mail Client<\/strong>. Install the <em>ESP Mail Client library by Mobizt<\/em>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"835\" height=\"586\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/install-esp-mail-client.png?resize=835%2C586&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install ESP Mail Client Library Arduino IDE\" class=\"wp-image-159016\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/install-esp-mail-client.png?w=835&amp;quality=100&amp;strip=all&amp;ssl=1 835w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/install-esp-mail-client.png?resize=300%2C211&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/install-esp-mail-client.png?resize=768%2C539&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 835px) 100vw, 835px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Sender Email (New Account)<\/h2>\n\n\n\n<p>We recommend creating a new email account to send the emails to your main personal email address. <strong>Do not use your main personal email to send emails via ESP32<\/strong>. If something goes wrong in your code or if by mistake you make too many requests, you can be banned or have your account temporarily disabled.<\/p>\n\n\n\n<p>We&#8217;ll use a newly created Gmail.com account to send the emails, but you can use any other email provider. The receiver email can be your personal email without any problem.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create a Sender Email Account<\/h3>\n\n\n\n<p>Create a new email account for sending emails with the ESP32. If you want to use a Gmail account, <a href=\"https:\/\/www.google.com\/gmail\/about\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">go to this link<\/a> to create a new one.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"940\" height=\"602\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/GMAIL-create-a-new-account.png?resize=940%2C602&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Gmail Create a new account\" class=\"wp-image-93593\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/GMAIL-create-a-new-account.png?w=940&amp;quality=100&amp;strip=all&amp;ssl=1 940w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/GMAIL-create-a-new-account.png?resize=300%2C192&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/GMAIL-create-a-new-account.png?resize=768%2C492&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 940px) 100vw, 940px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Create an App Password<\/h3>\n\n\n\n<p>You need to create an app password so that the ESP32 is able to send emails using your Gmail account. An App Password is a 16-digit passcode that gives a less secure app or device permission to access your Google Account. <a href=\"https:\/\/support.google.com\/accounts\/answer\/185833\" target=\"_blank\" rel=\"noreferrer noopener\">Learn more about sign-in with app passwords here<\/a>.<\/p>\n\n\n\n<p>An app password can only be used with accounts that have <strong><a href=\"https:\/\/support.google.com\/accounts\/answer\/185839\">2-step verification turned on<\/a><\/strong>. <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open your&nbsp;<a href=\"https:\/\/myaccount.google.com\/\">Google Account<\/a>.<\/li>\n\n\n\n<li>In the search panel, select\u00a0<strong>Security<\/strong>.<\/li>\n\n\n\n<li>Under \u201cSigning in to Google,\u201d select&nbsp;<strong>2-Step Verification<\/strong>&nbsp;&gt; <strong>Get started<\/strong>.<\/li>\n\n\n\n<li>Follow the on-screen steps.<\/li>\n<\/ol>\n\n\n\n<p>After enabling 2-step verification, you can create an app password.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open your&nbsp;<a href=\"https:\/\/myaccount.google.com\/\">Google Account<\/a>.<\/li>\n\n\n\n<li>In the search panel, search for <strong>App Passwords<\/strong>.<\/li>\n\n\n\n<li>Open the <strong>App Passwords<\/strong> menu.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"491\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/app-password-menu.png?resize=750%2C491&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Google - Create app password\" class=\"wp-image-159018\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/app-password-menu.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/app-password-menu.png?resize=300%2C196&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<ol start=\"4\"><li>Give a name to the app password, for example <em>ESP.<\/em> Then, click on <strong>Create<\/strong>. It will pop-up a window with a password that you&#8217;ll use with the ESP32 or ESP8266 to send emails. Save that password (even though it says you won&#8217;t need to remember it) because you&#8217;ll need it later.<\/li><\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"664\" height=\"587\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/create-app-password.png?resize=664%2C587&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Create App password gmail\" class=\"wp-image-159019\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/create-app-password.png?w=664&amp;quality=100&amp;strip=all&amp;ssl=1 664w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/create-app-password.png?resize=300%2C265&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 664px) 100vw, 664px\" \/><\/figure><\/div>\n\n\n<p>It will pop-up a window with a password that you&#8217;ll use with the ESP32 or ESP8266 to send emails. Save that password (even though it says you won&#8217;t need to remember it) because you&#8217;ll need it later.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"544\" height=\"453\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/generated-app-password.png?resize=544%2C453&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Generated app password Gmail\" class=\"wp-image-159020\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/generated-app-password.png?w=544&amp;quality=100&amp;strip=all&amp;ssl=1 544w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/generated-app-password.png?resize=300%2C250&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 544px) 100vw, 544px\" \/><\/figure><\/div>\n\n\n<p>Now, you should have an app password that you&#8217;ll use on the ESP code to send the emails.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"652\" height=\"282\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/app-password-created.png?resize=652%2C282&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"App password created ESP32\" class=\"wp-image-159021\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/app-password-created.png?w=652&amp;quality=100&amp;strip=all&amp;ssl=1 652w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/app-password-created.png?resize=300%2C130&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><\/figure><\/div>\n\n\n<p>If you&#8217;re using another email provider, check how to create an app password. You should be able to find the instructions with a quick google search &#8220;<strong>your_email_provider<\/strong> + create app password&#8221;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Gmail SMTP Server Settings<\/h3>\n\n\n\n<p>If you&#8217;re using a Gmail account, these are the SMTP Server details:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SMTP Server: <strong>smtp.gmail.com<\/strong><\/li>\n\n\n\n<li>SMTP username: Complete Gmail address<\/li>\n\n\n\n<li>SMTP password: Your Gmail password<\/li>\n\n\n\n<li>SMTP port (TLS): <strong>587<\/strong><\/li>\n\n\n\n<li>SMTP port (SSL): <strong>465<\/strong><\/li>\n\n\n\n<li>SMTP TLS\/SSL required: <strong>yes<\/strong><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Outlook SMTP Server Settings<\/h3>\n\n\n\n<p>For Outlook accounts, these are the SMTP Server settings:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SMTP Server: <strong>smtp.office365.com<\/strong><\/li>\n\n\n\n<li>SMTP Username: Complete Outlook email address<\/li>\n\n\n\n<li>SMTP Password: Your Outlook password<\/li>\n\n\n\n<li>SMTP Port: <strong>587<\/strong><\/li>\n\n\n\n<li>SMTP TLS\/SSL Required: <strong>Yes<\/strong><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Live or Hotmail SMTP Server Settings<\/h3>\n\n\n\n<p>For Live or Hotmail accounts, these are the SMTP Server settings:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SMTP Server: <strong>smtp.live.com<\/strong><\/li>\n\n\n\n<li>SMTP Username: Complete Live\/Hotmail email address<\/li>\n\n\n\n<li>SMTP Password: Your Windows Live Hotmail password<\/li>\n\n\n\n<li>SMTP Port: <strong>587<\/strong><\/li>\n\n\n\n<li>SMTP TLS\/SSL Required: <strong>Yes<\/strong><\/li>\n<\/ul>\n\n\n\n<p>If you&#8217;re using another email provider, you need to search for its SMTP Server settings. Now, you have everything ready to start sending emails with your ESP32.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"send-email\">Send an Email with HTML or Raw Text with ESP32 (Arduino IDE)<\/h2>\n\n\n\n<p>The following code sends an email via SMTP Server with HTML or raw text. For demonstration purposes, the ESP32 sends an email once when it boots. Then, you should be able to modify the code and integrate it into your own projects.<\/p>\n\n\n\n<p>Don&#8217;t upload the code yet, you need to make some modifications to make it work for you.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos\n  Complete project details at:\n   - ESP32: https:\/\/RandomNerdTutorials.com\/esp32-send-email-smtp-server-arduino-ide\/\n   - ESP8266: https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-send-email-smtp-server-arduino\/\n  \n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n  Example adapted from: https:\/\/github.com\/mobizt\/ESP-Mail-Client\n*\/\n\n#include &lt;Arduino.h&gt;\n#if defined(ESP32)\n  #include &lt;WiFi.h&gt;\n#elif defined(ESP8266)\n  #include &lt;ESP8266WiFi.h&gt;\n#endif\n#include &lt;ESP_Mail_Client.h&gt;\n\n#define WIFI_SSID &quot;REPLACE_WITH_YOUR_SSID&quot;\n#define WIFI_PASSWORD &quot;REPLACE_WITH_YOUR_PASSWORD&quot;\n\n\/** The smtp host name e.g. smtp.gmail.com for GMail or smtp.office365.com for Outlook or smtp.mail.yahoo.com *\/\n#define SMTP_HOST &quot;smtp.gmail.com&quot;\n#define SMTP_PORT 465\n\n\/* The sign in credentials *\/\n#define AUTHOR_EMAIL &quot;YOUR_EMAIL@XXXX.com&quot;\n#define AUTHOR_PASSWORD &quot;YOUR_EMAIL_APP_PASS&quot;\n\n\/* Recipient's email*\/\n#define RECIPIENT_EMAIL &quot;RECIPIENTE_EMAIL@XXXX.com&quot;\n\n\/* Declare the global used SMTPSession object for SMTP transport *\/\nSMTPSession smtp;\n\n\/* Callback function to get the Email sending status *\/\nvoid smtpCallback(SMTP_Status status);\n\nvoid setup(){\n  Serial.begin(115200);\n  Serial.println();\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\n  Serial.print(&quot;Connecting to Wi-Fi&quot;);\n  while (WiFi.status() != WL_CONNECTED){\n    Serial.print(&quot;.&quot;);\n    delay(300);\n  }\n  Serial.println();\n  Serial.print(&quot;Connected with IP: &quot;);\n  Serial.println(WiFi.localIP());\n  Serial.println();\n\n  \/*  Set the network reconnection option *\/\n  MailClient.networkReconnect(true);\n\n  \/** Enable the debug via Serial port\n   * 0 for no debugging\n   * 1 for basic level debugging\n   *\n   * Debug port can be changed via ESP_MAIL_DEFAULT_DEBUG_PORT in ESP_Mail_FS.h\n   *\/\n  smtp.debug(1);\n\n  \/* Set the callback function to get the sending results *\/\n  smtp.callback(smtpCallback);\n\n  \/* Declare the Session_Config for user defined session credentials *\/\n  Session_Config config;\n\n  \/* Set the session config *\/\n  config.server.host_name = SMTP_HOST;\n  config.server.port = SMTP_PORT;\n  config.login.email = AUTHOR_EMAIL;\n  config.login.password = AUTHOR_PASSWORD;\n  config.login.user_domain = &quot;&quot;;\n\n  \/*\n  Set the NTP config time\n  For times east of the Prime Meridian use 0-12\n  For times west of the Prime Meridian add 12 to the offset.\n  Ex. American\/Denver GMT would be -6. 6 + 12 = 18\n  See https:\/\/en.wikipedia.org\/wiki\/Time_zone for a list of the GMT\/UTC timezone offsets\n  *\/\n  config.time.ntp_server = F(&quot;pool.ntp.org,time.nist.gov&quot;);\n  config.time.gmt_offset = 3;\n  config.time.day_light_offset = 0;\n\n  \/* Declare the message class *\/\n  SMTP_Message message;\n\n  \/* Set the message headers *\/\n  message.sender.name = F(&quot;ESP&quot;);\n  message.sender.email = AUTHOR_EMAIL;\n  message.subject = F(&quot;ESP Test Email&quot;);\n  message.addRecipient(F(&quot;Sara&quot;), RECIPIENT_EMAIL);\n    \n  \/*Send HTML message*\/\n  \/*String htmlMsg = &quot;&lt;div style=\\&quot;color:#2f4468;\\&quot;&gt;&lt;h1&gt;Hello World!&lt;\/h1&gt;&lt;p&gt;- Sent from ESP board&lt;\/p&gt;&lt;\/div&gt;&quot;;\n  message.html.content = htmlMsg.c_str();\n  message.html.content = htmlMsg.c_str();\n  message.text.charSet = &quot;us-ascii&quot;;\n  message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit;*\/\n\n   \n  \/\/Send raw text message\n  String textMsg = &quot;Hello World! - Sent from ESP board&quot;;\n  message.text.content = textMsg.c_str();\n  message.text.charSet = &quot;us-ascii&quot;;\n  message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;\n  \n  message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_low;\n  message.response.notify = esp_mail_smtp_notify_success | esp_mail_smtp_notify_failure | esp_mail_smtp_notify_delay;\n\n\n  \/* Connect to the server *\/\n  if (!smtp.connect(&amp;config)){\n    ESP_MAIL_PRINTF(&quot;Connection error, Status Code: %d, Error Code: %d, Reason: %s&quot;, smtp.statusCode(), smtp.errorCode(), smtp.errorReason().c_str());\n    return;\n  }\n\n  if (!smtp.isLoggedIn()){\n    Serial.println(&quot;\\nNot yet logged in.&quot;);\n  }\n  else{\n    if (smtp.isAuthenticated())\n      Serial.println(&quot;\\nSuccessfully logged in.&quot;);\n    else\n      Serial.println(&quot;\\nConnected with no Auth.&quot;);\n  }\n\n  \/* Start sending Email and close the session *\/\n  if (!MailClient.sendMail(&amp;smtp, &amp;message))\n    ESP_MAIL_PRINTF(&quot;Error, Status Code: %d, Error Code: %d, Reason: %s&quot;, smtp.statusCode(), smtp.errorCode(), smtp.errorReason().c_str());\n\n}\n\nvoid loop(){\n}\n\n\/* Callback function to get the Email sending status *\/\nvoid smtpCallback(SMTP_Status status){\n  \/* Print the current status *\/\n  Serial.println(status.info());\n\n  \/* Print the sending result *\/\n  if (status.success()){\n    \/\/ ESP_MAIL_PRINTF used in the examples is for format printing via debug Serial port\n    \/\/ that works for all supported Arduino platform SDKs e.g. AVR, SAMD, ESP32 and ESP8266.\n    \/\/ In ESP8266 and ESP32, you can use Serial.printf directly.\n\n    Serial.println(&quot;----------------&quot;);\n    ESP_MAIL_PRINTF(&quot;Message sent success: %d\\n&quot;, status.completedCount());\n    ESP_MAIL_PRINTF(&quot;Message sent failed: %d\\n&quot;, status.failedCount());\n    Serial.println(&quot;----------------\\n&quot;);\n\n    for (size_t i = 0; i &lt; smtp.sendingResult.size(); i++)\n    {\n      \/* Get the result item *\/\n      SMTP_Result result = smtp.sendingResult.getItem(i);\n\n      \/\/ In case, ESP32, ESP8266 and SAMD device, the timestamp get from result.timestamp should be valid if\n      \/\/ your device time was synched with NTP server.\n      \/\/ Other devices may show invalid timestamp as the device time was not set i.e. it will show Jan 1, 1970.\n      \/\/ You can call smtp.setSystemTime(xxx) to set device time manually. Where xxx is timestamp (seconds since Jan 1, 1970)\n      \n      ESP_MAIL_PRINTF(&quot;Message No: %d\\n&quot;, i + 1);\n      ESP_MAIL_PRINTF(&quot;Status: %s\\n&quot;, result.completed ? &quot;success&quot; : &quot;failed&quot;);\n      ESP_MAIL_PRINTF(&quot;Date\/Time: %s\\n&quot;, MailClient.Time.getDateTimeString(result.timestamp, &quot;%B %d, %Y %H:%M:%S&quot;).c_str());\n      ESP_MAIL_PRINTF(&quot;Recipient: %s\\n&quot;, result.recipients.c_str());\n      ESP_MAIL_PRINTF(&quot;Subject: %s\\n&quot;, result.subject.c_str());\n    }\n    Serial.println(&quot;----------------\\n&quot;);\n\n    \/\/ You need to clear sending result as the memory usage will grow up.\n    smtp.sendingResult.clear();\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_Email_Simple.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>You need to insert your network credentials as well as set the sender email, SMTP Server details, recipient, and message.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>This code is adapted from an example provided by the library. The example is well commented so that you understand what each line of code does. Let&#8217;s just take a look at the relevant parts that you need or may need to change.<\/p>\n\n\n\n<p>First, insert your network credentials in the following lines:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define WIFI_SSID \"REPLACE_WITH_YOUR_SSID\"\n#define WIFI_PASSWORD \"REPLACE_WITH_YOUR_PASSWORD\"<\/code><\/pre>\n\n\n\n<p>Insert your SMTP server settings. If you&#8217;re using a Gmail account to send the emails, these are the settings:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define SMTP_HOST \"smtp.gmail.com\"\n#define SMTP_PORT 465<\/code><\/pre>\n\n\n\n<p>Insert the sender email login credentials (complete email and app password you created previously).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define AUTHOR_EMAIL \"YOUR_EMAIL@XXXX.com\"\n#define AUTHOR_PASSWORD \"YOUR_EMAIL_PASS\"<\/code><\/pre>\n\n\n\n<p>Insert the recipient email:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define RECIPIENT_EMAIL \"RECIPIENTE_EMAIL@XXXX.com\"<\/code><\/pre>\n\n\n\n<p>You may need to adjust the <span class=\"rnthl rntliteral\">gmt_offset<\/span> variable depending on your location so that the email is timestamped with the right time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>config.time.ntp_server = F(\"pool.ntp.org,time.nist.gov\");\nconfig.time.gmt_offset = 3;\nconfig.time.day_light_offset = 0;<\/code><\/pre>\n\n\n\n<p>Set the message headers in the following lines in the <span class=\"rnthl rntliteral\">setup()<\/span>\u2014sender name, sender email, email subject, and the recipient name and email:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>  \/* Set the message headers *\/\n  message.sender.name = F(\"ESP\");\n  message.sender.email = AUTHOR_EMAIL;\n  message.subject = F(\"ESP Test Email\");\n  message.addRecipient(F(\"Sara\"), RECIPIENT_EMAIL);<\/code><\/pre>\n\n\n\n<p>In the following lines, set the content of the message (raw text) in the <span class=\"rnthl rntliteral\">textMsg<\/span> variable:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/Send raw text message\nString textMsg = \"Hello World - Sent from ESP board\";\nmessage.text.content = textMsg.c_str();\nmessage.text.charSet = \"us-ascii\";\nmessage.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;<\/code><\/pre>\n\n\n\n<p>If you want to send HTML text instead, uncomment the following lines\u2014 you should insert your HTML text in the <span class=\"rnthl rntliteral\">htmlMsg<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/*Send HTML message*\/\n\/*String htmlMsg = \"&lt;div style=\\\"color:#2f4468;\\\"&gt;&lt;h1&gt;Hello World!&lt;\/h1&gt;&lt;p&gt;- Sent from ESP board&lt;\/p&gt;&lt;\/div&gt;\";\nmessage.html.content = htmlMsg.c_str();\nmessage.html.content = htmlMsg.c_str();\nmessage.text.charSet = \"us-ascii\";\nmessage.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit;*\/<\/code><\/pre>\n\n\n\n<p>Finally, the following lines send the message:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (!MailClient.sendMail(&amp;smtp, &amp;message))\n  Serial.println(\"Error sending Email, \" + smtp.errorReason());<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>Upload the code to your ESP32. After uploading, open the Serial Monitor at a baud rate of 115200. Press the ESP32 Reset button.<\/p>\n\n\n\n<p>If everything went as expected you should get a similar message in the Serial Monitor.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"300\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/esp32-esp8266-send-email.png?resize=696%2C300&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP send email SMTP server - serial monitor\" class=\"wp-image-159023\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/esp32-esp8266-send-email.png?w=696&amp;quality=100&amp;strip=all&amp;ssl=1 696w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/esp32-esp8266-send-email.png?resize=300%2C129&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><\/figure><\/div>\n\n\n<p>Check your email account. You should have received an email from your ESP32 board.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"907\" height=\"229\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/ESP32-SMTP-Server-Receive-Email-Test.png?resize=907%2C229&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 SMTP Server Receive Email Test\" class=\"wp-image-93587\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/ESP32-SMTP-Server-Receive-Email-Test.png?w=907&amp;quality=100&amp;strip=all&amp;ssl=1 907w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/ESP32-SMTP-Server-Receive-Email-Test.png?resize=300%2C76&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/ESP32-SMTP-Server-Receive-Email-Test.png?resize=768%2C194&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 907px) 100vw, 907px\" \/><\/figure><\/div>\n\n\n<p>If you set the option to send a message with HTML text, this is what the message looks like:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"327\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/ESP32-SMTP-Server-Send-Email-with-Body-Text-format-HTML.png?resize=601%2C327&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 SMTP Server Send Email with Body Text format HTML\" class=\"wp-image-93588\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/ESP32-SMTP-Server-Send-Email-with-Body-Text-format-HTML.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/ESP32-SMTP-Server-Send-Email-with-Body-Text-format-HTML.png?resize=300%2C163&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n<p>If you&#8217;ve enabled the raw text message, this is the email that you should receive.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"570\" height=\"280\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/ESP32-SMTP-Server-Send-Email-with-Body-Text-only-format.png?resize=570%2C280&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 SMTP Server Send Email with Body Text only format\" class=\"wp-image-93589\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/ESP32-SMTP-Server-Send-Email-with-Body-Text-only-format.png?w=570&amp;quality=100&amp;strip=all&amp;ssl=1 570w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/02\/ESP32-SMTP-Server-Send-Email-with-Body-Text-only-format.png?resize=300%2C147&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 570px) 100vw, 570px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"attachment\">Send Attachments via Email with ESP32 (Arduino IDE)<\/h2>\n\n\n\n<p>In this section, we&#8217;ll show you how to send attachments in your emails sent by the ESP32. We&#8217;ll show you how to send <em>.txt <\/em>files or pictures. This can be useful to send a <em>.txt<\/em> file with sensor readings from the past few hours or to send a <a href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-take-photo-save-microsd-card\/\">photo captured by an ESP32-CAM<\/a>. <\/p>\n\n\n\n<p>For this tutorial, the files to be sent should be saved on the ESP32 filesystem (LittleFS).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Upload files to LittleFS<\/h3>\n\n\n\n<p>To send files via email, these should be saved on the ESP32 filesystem or on a microSD card. We&#8217;ll upload a picture and a .txt file to the ESP32 LittleFS filesystem using the <a href=\"https:\/\/randomnerdtutorials.com\/arduino-ide-2-install-esp32-littlefs\/\" title=\"\">ESP32 Filesystem Uploader plugin for Arduino IDE<\/a>. Follow the next tutorial to install the plugin if you don&#8217;t have it installed yet:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-ide-2-install-esp32-littlefs\/\">Arduino IDE 2: Install ESP32 LittleFS Uploader (Upload Files to the Filesystem)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Create a new Arduino sketch and save it. Go to <strong>Sketch <\/strong>&gt; <strong>Show Sketch<\/strong> folder. Inside the Arduino sketch folder, create a folder called <strong>data<\/strong>. Move a .jpg file and .txt file to your <strong>data <\/strong>folder.<\/p>\n\n\n\n<p><strong>Alternatively, you can <a href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_Email_Attachments\/ESP_Email_Attachments.zip\" target=\"_blank\" rel=\"noreferrer noopener\">click here to download the project folder<\/a>.<\/strong><\/p>\n\n\n\n<p><strong>Note: <\/strong> with the default code,<em> your files must be named image.png<\/em> and <em>text_file.txt<\/em>. Alternatively, you can modify the code to import files with a different name.<\/p>\n\n\n\n<p>We&#8217;ll be sending these <strong><a href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_Email_Attachments\/ESP_Email_Attachments.zip\" target=\"_blank\" rel=\"noreferrer noopener\">files<\/a><\/strong>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"544\" height=\"249\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/Files-to-send-via-email-ESP-Mail-Client.png?resize=544%2C249&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Send emails with attachments ESP32 ESP8266 Text file and image\" class=\"wp-image-103461\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/Files-to-send-via-email-ESP-Mail-Client.png?w=544&amp;quality=100&amp;strip=all&amp;ssl=1 544w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/Files-to-send-via-email-ESP-Mail-Client.png?resize=300%2C137&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 544px) 100vw, 544px\" \/><\/figure><\/div>\n\n\n<p>Your folder structure should look as follows (<strong><a href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_Email_Attachments\/ESP_Email_Attachments.zip\" target=\"_blank\" rel=\"noreferrer noopener\">download project folder<\/a><\/strong>):<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"531\" height=\"350\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/Send-Email-Attachments-Folder-Structure-filesystem_organizing-files.png?resize=531%2C350&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Send email attachments folder structure filesystem organizing files\" class=\"wp-image-103463\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/Send-Email-Attachments-Folder-Structure-filesystem_organizing-files.png?w=531&amp;quality=100&amp;strip=all&amp;ssl=1 531w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/Send-Email-Attachments-Folder-Structure-filesystem_organizing-files.png?resize=300%2C198&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 531px) 100vw, 531px\" \/><\/figure><\/div>\n\n\n<p>After moving the files to the <strong>data <\/strong>folder, you need to upload them to your board. In the Arduino IDE, press [<strong>Ctrl<\/strong>] + [<strong>Shift<\/strong>] + [<strong>P<\/strong>] on Windows or [<strong>\u2318<\/strong>] + [<strong>Shift<\/strong>] + [<strong>P<\/strong>] on MacOS to open the command palette. Search for the\u00a0<strong>Upload LittleFS to Pico\/ESP8266\/ESP32<\/strong>\u00a0command and click on it.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"744\" height=\"401\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/upload-files-little-fs-esp32-arduino-ide.png?resize=744%2C401&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Upload LittleFS to Pico ESP8266 ESP32 Arduino IDE\" class=\"wp-image-158893\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/upload-files-little-fs-esp32-arduino-ide.png?w=744&amp;quality=100&amp;strip=all&amp;ssl=1 744w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/upload-files-little-fs-esp32-arduino-ide.png?resize=300%2C162&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 744px) 100vw, 744px\" \/><\/figure><\/div>\n\n\n<p>You should get a success message on the debugging window. If the files were successfully uploaded, move on to the next section. <\/p>\n\n\n\n<p class=\"rntbox rntcred\"><strong>Note: <\/strong> if you start seeing many dots &#8230;.____&#8230;..____ being printed on the debugging window, you need to hold the ESP32 on-board BOOT button for the files to be uploaded.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n\n<p>The following code sends an email with a <em>.txt<\/em> file and a picture attached. Before uploading the code, make sure you insert your sender email settings and your recipient email.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\r\n  Rui Santos\r\n  Complete project details at:\r\n   - ESP32: https:\/\/RandomNerdTutorials.com\/esp32-send-email-smtp-server-arduino-ide\/\r\n   - ESP8266: https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-send-email-smtp-server-arduino\/\r\n   \r\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\r\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\r\n  Example adapted K. Suwatchai (Mobizt): https:\/\/github.com\/mobizt\/ESP-Mail-Client Copyright (c) 2021 mobizt\r\n*\/\r\n\r\n\/\/ To use send Email for Gmail to port 465 (SSL), less secure app option should be enabled. https:\/\/myaccount.google.com\/lesssecureapps?pli=1\r\n\r\n#include &lt;Arduino.h&gt;\r\n#if defined(ESP32)\r\n  #include &lt;WiFi.h&gt;\r\n#elif defined(ESP8266)\r\n  #include &lt;ESP8266WiFi.h&gt;\r\n#endif\r\n#include &lt;ESP_Mail_Client.h&gt;\r\n\r\n#define WIFI_SSID &quot;REPLACE_WITH_YOUR_SSID&quot;\r\n#define WIFI_PASSWORD &quot;REPLACE_WITH_YOUR_PASSWORD&quot;\r\n\r\n#define SMTP_HOST &quot;smtp.gmail.com&quot;\r\n\r\n\/** The smtp port e.g. \r\n * 25  or esp_mail_smtp_port_25\r\n * 465 or esp_mail_smtp_port_465\r\n * 587 or esp_mail_smtp_port_587\r\n*\/\r\n#define SMTP_PORT 465\r\n\r\n\/* The sign in credentials *\/\r\n#define AUTHOR_EMAIL &quot;YOUR_EMAIL@XXXX.com&quot;\r\n#define AUTHOR_PASSWORD &quot;YOUR_EMAIL_APP_PASS&quot;\r\n\r\n\/* Recipient's email*\/\r\n#define RECIPIENT_EMAIL &quot;RECIPIENTE_EMAIL@XXXX.com&quot;\r\n\r\n\/* The SMTP Session object used for Email sending *\/\r\nSMTPSession smtp;\r\n\r\n\/* Callback function to get the Email sending status *\/\r\nvoid smtpCallback(SMTP_Status status);\r\n\r\nvoid setup(){\r\n  Serial.begin(115200);\r\n  Serial.println();\r\n  Serial.print(&quot;Connecting to AP&quot;);\r\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\r\n  while (WiFi.status() != WL_CONNECTED){\r\n    Serial.print(&quot;.&quot;);\r\n    delay(200);\r\n  }\r\n  Serial.println(&quot;&quot;);\r\n  Serial.println(&quot;WiFi connected.&quot;);\r\n  Serial.println(&quot;IP address: &quot;);\r\n  Serial.println(WiFi.localIP());\r\n  Serial.println();\r\n\r\n  \/\/ Init filesystem\r\n  ESP_MAIL_DEFAULT_FLASH_FS.begin();\r\n\r\n  \/** Enable the debug via Serial port\r\n   * none debug or 0\r\n   * basic debug or 1\r\n  *\/\r\n  smtp.debug(1);\r\n\r\n  \/* Set the callback function to get the sending results *\/\r\n  smtp.callback(smtpCallback);\r\n\r\n  \/* Declare the Session_Config for user defined session credentials *\/\r\n  Session_Config config;\r\n\r\n  \/* Set the session config *\/\r\n  config.server.host_name = SMTP_HOST;\r\n  config.server.port = SMTP_PORT;\r\n  config.login.email = AUTHOR_EMAIL;\r\n  config.login.password = AUTHOR_PASSWORD;\r\n  config.login.user_domain = &quot;mydomain.net&quot;;\r\n  \r\n  \/*\r\n  Set the NTP config time\r\n  For times east of the Prime Meridian use 0-12\r\n  For times west of the Prime Meridian add 12 to the offset.\r\n  Ex. American\/Denver GMT would be -6. 6 + 12 = 18\r\n  See https:\/\/en.wikipedia.org\/wiki\/Time_zone for a list of the GMT\/UTC timezone offsets\r\n  *\/\r\n  config.time.ntp_server = F(&quot;pool.ntp.org,time.nist.gov&quot;);\r\n  config.time.gmt_offset = 3;\r\n  config.time.day_light_offset = 0;\r\n\r\n  \/* Declare the message class *\/\r\n  SMTP_Message message;\r\n\r\n  \/* Enable the chunked data transfer with pipelining for large message if server supported *\/\r\n  message.enable.chunking = true;\r\n\r\n  \/* Set the message headers *\/\r\n  message.sender.name = &quot;ESP Mail&quot;;\r\n  message.sender.email = AUTHOR_EMAIL;\r\n\r\n  message.subject = F(&quot;Test sending Email with attachments and inline images from Flash&quot;);\r\n  message.addRecipient(F(&quot;Sara&quot;), RECIPIENT_EMAIL);\r\n\r\n  \/** Two alternative content versions are sending in this example e.g. plain text and html *\/\r\n  String htmlMsg = &quot;This message contains attachments: image and text file.&quot;;\r\n  message.html.content = htmlMsg.c_str();\r\n  message.html.charSet = &quot;utf-8&quot;;\r\n  message.html.transfer_encoding = Content_Transfer_Encoding::enc_qp;\r\n\r\n  message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_normal;\r\n  message.response.notify = esp_mail_smtp_notify_success | esp_mail_smtp_notify_failure | esp_mail_smtp_notify_delay;\r\n\r\n  \/* The attachment data item *\/\r\n  SMTP_Attachment att;\r\n\r\n  \/** Set the attachment info e.g. \r\n   * file name, MIME type, file path, file storage type,\r\n   * transfer encoding and content encoding\r\n  *\/\r\n  att.descr.filename = &quot;image.png&quot;;\r\n  att.descr.mime = &quot;image\/png&quot;; \/\/binary data\r\n  att.file.path = &quot;\/image.png&quot;;\r\n  att.file.storage_type = esp_mail_file_storage_type_flash;\r\n  att.descr.transfer_encoding = Content_Transfer_Encoding::enc_base64;\r\n\r\n  \/* Add attachment to the message *\/\r\n  message.addAttachment(att);\r\n\r\n  message.resetAttachItem(att);\r\n  att.descr.filename = &quot;text_file.txt&quot;;\r\n  att.descr.mime = &quot;text\/plain&quot;;\r\n  att.file.path = &quot;\/text_file.txt&quot;;\r\n  att.file.storage_type = esp_mail_file_storage_type_flash;\r\n  att.descr.transfer_encoding = Content_Transfer_Encoding::enc_base64;\r\n\r\n  \/* Add attachment to the message *\/\r\n  message.addAttachment(att);\r\n\r\n  \/* Connect to server with the session config *\/\r\n  if (!smtp.connect(&amp;config)){\r\n    ESP_MAIL_PRINTF(&quot;Connection error, Status Code: %d, Error Code: %d, Reason: %s&quot;, smtp.statusCode(), smtp.errorCode(), smtp.errorReason().c_str());\r\n    return;\r\n  }\r\n\r\n  if (!smtp.isLoggedIn()){\r\n    Serial.println(&quot;\\nNot yet logged in.&quot;);\r\n  }\r\n  else{\r\n    if (smtp.isAuthenticated())\r\n      Serial.println(&quot;\\nSuccessfully logged in.&quot;);\r\n    else\r\n      Serial.println(&quot;\\nConnected with no Auth.&quot;);\r\n  }\r\n\r\n  \/* Start sending the Email and close the session *\/\r\n  if (!MailClient.sendMail(&amp;smtp, &amp;message, true))\r\n    Serial.println(&quot;Error sending Email, &quot; + smtp.errorReason());\r\n}\r\n\r\nvoid loop(){\r\n}\r\n\r\n\/* Callback function to get the Email sending status *\/\r\nvoid smtpCallback(SMTP_Status status){\r\n  \/* Print the current status *\/\r\n  Serial.println(status.info());\r\n\r\n  \/* Print the sending result *\/\r\n  if (status.success()){\r\n    Serial.println(&quot;----------------&quot;);\r\n    ESP_MAIL_PRINTF(&quot;Message sent success: %d\\n&quot;, status.completedCount());\r\n    ESP_MAIL_PRINTF(&quot;Message sent failled: %d\\n&quot;, status.failedCount());\r\n    Serial.println(&quot;----------------\\n&quot;);\r\n    struct tm dt;\r\n\r\n    for (size_t i = 0; i &lt; smtp.sendingResult.size(); i++){\r\n      \/* Get the result item *\/\r\n      SMTP_Result result = smtp.sendingResult.getItem(i);\r\n      time_t ts = (time_t)result.timestamp;\r\n      localtime_r(&amp;ts, &amp;dt);\r\n\r\n      ESP_MAIL_PRINTF(&quot;Message No: %d\\n&quot;, i + 1);\r\n      ESP_MAIL_PRINTF(&quot;Status: %s\\n&quot;, result.completed ? &quot;success&quot; : &quot;failed&quot;);\r\n      ESP_MAIL_PRINTF(&quot;Date\/Time: %d\/%d\/%d %d:%d:%d\\n&quot;, dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);\r\n      ESP_MAIL_PRINTF(&quot;Recipient: %s\\n&quot;, result.recipients.c_str());\r\n      ESP_MAIL_PRINTF(&quot;Subject: %s\\n&quot;, result.subject.c_str());\r\n    }\r\n    Serial.println(&quot;----------------\\n&quot;);\r\n    \r\n    \/\/ You need to clear sending result as the memory usage will grow up.\r\n    smtp.sendingResult.clear();\r\n  }\r\n}\r\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_Email_Attachments\/ESP_Email_Attachments.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the code works<\/h3>\n\n\n\n<p>This code is very similar to the previous one, so we&#8217;ll just take a look at the relevant parts to send attachments.<\/p>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, you initialize the filesystem using the ESP Mail Client library method. The default filesystem set in the library for the ESP32 is LittleFS (you can change the default in the library file <em>ESP_Mail_FS.h<\/em>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Init filesystem\nESP_MAIL_DEFAULT_FLASH_FS.begin();<\/code><\/pre>\n\n\n\n<p>You need to create an attachment as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/* The attachment data item *\/\nSMTP_Attachment att;<\/code><\/pre>\n\n\n\n<p>Then, add the attachment details: filename, MIME type, file path, file storage type, and transfer encoding. In the following lines, we&#8217;re sending the image file.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>att.descr.filename = \"image.png\";\natt.descr.mime = \"image\/png\"; \natt.file.path = \"\/image.png\";\natt.file.storage_type = esp_mail_file_storage_type_flash;\natt.descr.transfer_encoding = Content_Transfer_Encoding::enc_base64;<\/code><\/pre>\n\n\n\n<p>Finally, add the attachment to the message:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>message.addAttachment(att);<\/code><\/pre>\n\n\n\n<p>If you want to send more attachments, you need to call the following line before adding the next attachment:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>message.resetAttachItem(att);<\/code><\/pre>\n\n\n\n<p>Then, enter the details of the other attachment (text file):<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>att.descr.filename = \"text_file.txt\";\natt.descr.mime = \"text\/plain\";\natt.file.path = \"\/text_file.txt\";\natt.file.storage_type = esp_mail_file_storage_type_flash;\natt.descr.transfer_encoding = Content_Transfer_Encoding::enc_base64;<\/code><\/pre>\n\n\n\n<p>And add this attachment to the message:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>message.addAttachment(att);<\/code><\/pre>\n\n\n\n<p>Finally, you just need to send the message as you did with the previous example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (!MailClient.sendMail(&amp;smtp, &amp;message, true))\n  Serial.println(\"Error sending Email, \" + smtp.errorReason());<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>After uploading the code, open the Serial Monitor at a baud rate of 115200 and press the on-board EN\/RESET button. If everything goes smoothly, you should get a similar message on the Serial Monitor.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"407\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP-send-email-attachments-demonstration.png?resize=666%2C407&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP Send Email Message Successfully Serial Monitor\" class=\"wp-image-159025\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP-send-email-attachments-demonstration.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP-send-email-attachments-demonstration.png?resize=300%2C183&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n<p>Check the recipient&#8217;s email address. You should have a new email with two attachments.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"390\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP-email-attachments-image-file.png?resize=750%2C390&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Send Email with Attachments ESP32 and ESP8266\" class=\"wp-image-103465\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP-email-attachments-image-file.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP-email-attachments-image-file.png?resize=300%2C156&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you&#8217;ve learned how to send emails with the ESP32 using an SMTP Server. For this method to work, the ESP32 should have access to the internet.<\/p>\n\n\n\n<p>If you don&#8217;t want to use an SMTP Server, you can also write a <a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-send-email-notification\/\">PHP script to send email notifications with the ESP32 or ESP8266 board<\/a>. <\/p>\n\n\n\n<p>You&#8217;ve learned how to send a simple email with text and with attachments. When using attachments, these should be saved on the ESP32 filesystem (LittleFS) or on a microSD card (not covered in this tutorial).<\/p>\n\n\n\n<p>The examples presented show how to send a single email when the ESP32 boots. The idea is to modify the code and include it in your own projects. For example, it can be useful to send a .txt file with the sensor readings, send a photo captured with the ESP32-CAM, use deep sleep to wake up your board every hour and send an email with data, etc.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this tutorial interesting.<\/p>\n\n\n\n<p>To learn more about the ESP32, take a look at our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\">Build Web Servers with ESP32 and ESP8266 eBook<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">More ESP32 resources&#8230;<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to send emails with the ESP32 using an SMTP Server. We&#8217;ll show you how to send a simple email with HTML or raw text and how to send &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 Send Emails using an SMTP Server: HTML, Text, and Attachments (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-send-email-smtp-server-arduino-ide\/#more-93585\" aria-label=\"Read more about ESP32 Send Emails using an SMTP Server: HTML, Text, and Attachments (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":104421,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,276,277,299,264],"tags":[],"class_list":["post-93585","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/ESP32-Send-Emails-SMTP-Server-Text-Attachments.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/93585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=93585"}],"version-history":[{"count":28,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/93585\/revisions"}],"predecessor-version":[{"id":159026,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/93585\/revisions\/159026"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/104421"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=93585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=93585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=93585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}