{"id":103424,"date":"2023-06-25T08:43:43","date_gmt":"2023-06-25T08:43:43","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=103424"},"modified":"2025-04-12T18:35:42","modified_gmt":"2025-04-12T18:35:42","slug":"esp8266-nodemcu-send-email-smtp-server-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-send-email-smtp-server-arduino\/","title":{"rendered":"ESP8266 NodeMCU Send Emails using an SMTP Server: HTML, Text, and Attachments (Arduino)"},"content":{"rendered":"\n<p>In this tutorial, you&#8217;ll learn how to send emails with the ESP8266 NodeMCU board using an SMTP server. We&#8217;ll show how to send an email with raw text, HTML text, and how to send attachments like images and files (<em>.txt<\/em>). We&#8217;ll program the ESP8266 NodeMCU board with the Arduino core.<\/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\/04\/ESP8266-Send-Emails-SMTP-Server-Text-Attachments.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU Send Emails using an SMTP Server HTML Text and Attachments Arduino IDE\" class=\"wp-image-103475\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-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\/04\/ESP8266-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\/04\/ESP8266-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\/04\/ESP8266-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 12 April 2025<\/em><\/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=\"#raw\">ESP8266 Send Email (HTML and raw text)<\/a><\/li>\n\n\n\n<li><a href=\"#attachments\">ESP8266 Send Email with Attachments (image and .txt file)<\/a><\/li>\n<\/ul>\n\n\n\n<p>We have a similar tutorial for the ESP32 board: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-send-email-smtp-server-arduino-ide\/\">ESP32 Send Emails using an SMTP Server: HTML, Text, and Attachments (Arduino IDE)<\/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 ESP8266, 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 ESP8266 NodeMCU board, 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 ESP8266 to send and receive emails with or without attachments via SMTP and IMAP servers. If you like this library and you&#8217;ll use it in your projects, consider supporting the developer&#8217;s work\u2014<a href=\"https:\/\/github.com\/mobizt\/ESP-Mail-Client\" target=\"_blank\" rel=\"noreferrer noopener\">see the library GitHub page<\/a>.<\/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 (<em>.png<\/em>) and a text (<em>.txt<\/em>) file. The files to be sent via email can be saved in the ESP8266 Filesystem (SPIFFS or LittleFS) or in 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>&gt; <strong>Include Library<\/strong> &gt; <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<h3 class=\"wp-block-heading\">Create a Sender Email (New Account)<\/h3>\n\n\n\n<p>We recommend creating a new email account to send the emails to your main personal email address.\u00a0<strong>Do not use your main personal email to send emails via ESP8266<\/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>Create a new email account for sending emails with the ESP8266. If you want to use a Gmail account,\u00a0<a href=\"https:\/\/www.google.com\/gmail\/about\/\" target=\"_blank\" rel=\"noreferrer noopener\">go to this link<\/a>\u00a0to create a new one.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><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 ESP8266 can 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 navigation panel, select&nbsp;<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&nbsp;<strong>App Passwords<\/strong>.<\/li>\n\n\n\n<li>Open the&nbsp;<strong>App Passwords<\/strong>&nbsp;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 account create app password to send email with esp32\" 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 ESP. Then, click on Create. It will pop-up a window with a password that you\u2019ll use with the ESP32 or ESP8266 to send emails. Save that password (even though it says you won\u2019t need to remember it) because you\u2019ll 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 to use with the ESP\" 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\u2019ll use with the ESP8266 to send emails. Save that password (even though it says you won\u2019t need to remember it) because you\u2019ll 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 Google Account\" 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 ESP8266 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=\"Gmail app password created for ESP32 send emails\" 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<\/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 ESP8266.<\/p>\n\n\n\n<p>In your Arduino IDE, you can go to <strong>File <\/strong>&gt; <strong>Examples <\/strong>&gt; <strong>ESP-Mail-Client<\/strong> and experiment with the examples provided\u2014you need to insert your email details (sender and recipient accounts), the SMTP server settings, and your SSID and password.<\/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=\"raw\">Send an Email with HTML or Raw Text with ESP8266<\/h2>\n\n\n\n<p>The following code sends an email via SMTP Server with HTML or raw text. As an example, the ESP8266 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 and set the sender email, SMTP Server details, recipient, and message.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\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 sign in 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 *\/\nmessage.sender.name = \"ESP\";\nmessage.sender.email = AUTHOR_EMAIL;\nmessage.subject = \"ESP Test Email\";\nmessage.addRecipient(\"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 ESP8266. After uploading, open the Serial Monitor at a baud rate of 115200.<\/p>\n\n\n\n<p>If everything goes 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=\"709\" height=\"370\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/ESP8266-send-email-success-serial-monitor.png?resize=709%2C370&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU Send Email Message Successfully Serial Monitor\" class=\"wp-image-168894\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/ESP8266-send-email-success-serial-monitor.png?w=709&amp;quality=100&amp;strip=all&amp;ssl=1 709w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/ESP8266-send-email-success-serial-monitor.png?resize=300%2C157&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 709px) 100vw, 709px\" \/><\/figure><\/div>\n\n\n<p>Check your email account. You should have received an email from your ESP8266 board.<\/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=\"886\" height=\"61\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/Send-Email-ESP-Gmail-Inbox.png?resize=886%2C61&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Email received from ESP8266 Board Gmail Account\" class=\"wp-image-104497\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/Send-Email-ESP-Gmail-Inbox.png?w=886&amp;quality=100&amp;strip=all&amp;ssl=1 886w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/Send-Email-ESP-Gmail-Inbox.png?resize=300%2C21&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/Send-Email-ESP-Gmail-Inbox.png?resize=768%2C53&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 886px) 100vw, 886px\" \/><\/figure><\/div>\n\n\n<p>If you set the option to send a message with HTML text, this is how 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=\"524\" height=\"356\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-Test-Email-Gmail-HTML-Text.png?resize=524%2C356&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 SMTP Server Send Email with Body HTML text format\" class=\"wp-image-103456\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-Test-Email-Gmail-HTML-Text.png?w=524&amp;quality=100&amp;strip=all&amp;ssl=1 524w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-Test-Email-Gmail-HTML-Text.png?resize=300%2C204&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 524px) 100vw, 524px\" \/><\/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=\"391\" height=\"180\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/ESP-email-message-raw-text-inbox.png?resize=391%2C180&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 SMTP Server Send Email with Body raw text format\" class=\"wp-image-104498\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/ESP-email-message-raw-text-inbox.png?w=391&amp;quality=100&amp;strip=all&amp;ssl=1 391w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/06\/ESP-email-message-raw-text-inbox.png?resize=300%2C138&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 391px) 100vw, 391px\" \/><\/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=\"attachments\">Send Attachments via Email with ESP8266 (Arduino IDE)<\/h2>\n\n\n\n<p>This section will show you how to send attachments in your emails sent by the ESP8266. 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 other applications.<\/p>\n\n\n\n<p>You should save the files you want to send in the ESP8266 filesystem (SPIFFS or LittleFS). The default filesystem used by the library is LittleFS. You can also send attachments saved on a microSD card, but we&#8217;ll not cover this subject in this tutorial.<\/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, you should save them in the ESP8266 filesystem. The library uses LittleFS by default. To upload files to the LittleFS using Arduino IDE, you need to install a Filesystem Uploader Plugin. Read the following tutorial to learn how to install and upload files to the ESP8266 filesystem:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-ide-2-install-esp8266-littlefs\/\" title=\"\">Install ESP8266 <strong>LittleFS <\/strong>Filesystem Uploader in Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<p>If you&#8217;re using VS Code + PlatformIO, follow the next tutorial to learn how to upload files to LittleFS:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-vs-code-platformio-littlefs\/\">ESP8266 NodeMCU with VS Code and PlatformIO: Upload Files to Filesystem (LittleFS)<\/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><em>data<\/em><\/strong>. Move a <em>.png<\/em> file and <em>.txt<\/em> file to your <strong><em>data<\/em> <\/strong>folder.<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/ESP8266_Email_Attachments\/ESP8266_Email_Attachments.zip\" target=\"_blank\" rel=\"noreferrer noopener\">Alternatively, you can click here to download the project folder.<\/a><\/strong><\/p>\n\n\n\n<p><strong>Note: <\/strong> with the default code, your files must be named <em>image.png<\/em> and <em>text_file.txt<\/em> or 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\/ESP8266\/ESP8266_Email_Attachments\/ESP8266_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=\"Upload the files to filesystem to send as an email attachment\" 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\/ESP8266\/ESP8266_Email_Attachments\/ESP8266_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><em>data <\/em><\/strong>folder, you can upload them to your board using Arduino IDE. Press [<strong>Ctrl<\/strong>] + [<strong>Shift<\/strong>] + [<strong>P<\/strong>] to open the command palette, and click on &#8216;<strong>Upload Little FS to Pico\/ESP8266<\/strong>\/<strong>ESP32<\/strong>&#8216;.<\/p>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> If you don&#8217;t have that option, it means you didn&#8217;t install the LittleFS Uploader Plugin. Make sure to follow this tutorial to install it: <a href=\"https:\/\/randomnerdtutorials.com\/arduino-ide-2-install-esp8266-littlefs\/\" title=\"\">Arduino IDE 2: Install ESP8266 NodeMCU LittleFS Uploader (Upload Files to the Filesystem)<\/a>.<\/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=\"673\" height=\"586\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/upload-to-filesystem-arduino-2-esp32-esp8266-pico.png?resize=673%2C586&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Upload LittleFS to Pico\/ESP8266\/ESP32\" class=\"wp-image-154922\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/upload-to-filesystem-arduino-2-esp32-esp8266-pico.png?w=673&amp;quality=100&amp;strip=all&amp;ssl=1 673w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/upload-to-filesystem-arduino-2-esp32-esp8266-pico.png?resize=300%2C261&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntcred\"><strong>Important: <\/strong> ensure the Serial Monitor is closed. Otherwise, the upload will fail.<\/p>\n\n\n\n<p>After a few seconds, you should get the message &#8220;<strong>Completed upload.<\/strong> &#8220;. The files were successfully uploaded to the ESP8266 filesystem.<\/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=\"844\" height=\"374\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/LittleFS-Upload-Completed-ESP8266-Arduino-IDE.png?resize=844%2C374&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"LittleFS Upload Complete to ESP8266 Arduino IDE\" class=\"wp-image-168896\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/LittleFS-Upload-Completed-ESP8266-Arduino-IDE.png?w=844&amp;quality=100&amp;strip=all&amp;ssl=1 844w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/LittleFS-Upload-Completed-ESP8266-Arduino-IDE.png?resize=300%2C133&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/LittleFS-Upload-Completed-ESP8266-Arduino-IDE.png?resize=768%2C340&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 844px) 100vw, 844px\" \/><\/figure><\/div>\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,  insert your network credentials, sender email settings, and 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 ESP8266 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 after adding the previous 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>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. If everything goes as expected, 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=\"688\" height=\"403\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/ESP8266-Send-Emails-With-Attachments-Serial-Monitor-Success.png?resize=688%2C403&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 send emails with attachments success - Serial Monitor\" class=\"wp-image-168897\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/ESP8266-Send-Emails-With-Attachments-Serial-Monitor-Success.png?w=688&amp;quality=100&amp;strip=all&amp;ssl=1 688w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/ESP8266-Send-Emails-With-Attachments-Serial-Monitor-Success.png?resize=300%2C176&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 688px) 100vw, 688px\" \/><\/figure><\/div>\n\n\n<p>If you go to your inbox, you should get the message with the 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 ESP8266 NodeMCU Gmail Account Arduino IDE\" 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 learned how to send emails with the ESP8266 NodeMCU board using an SMTP Server. You&#8217;ve learned how to send HTM text, raw text, and attachments.<\/p>\n\n\n\n<p>Now, the idea is to modify the code and include it in your projects. For example, it can be useful to send a <em>.txt<\/em> file with the sensor readings of the last hour, notifications when motion is detected, and much more.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this tutorial interesting. We have a similar guide for the ESP32:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-send-email-smtp-server-arduino-ide\/\">ESP32 Send Emails using an SMTP Server: HTML, Text and Attachments (Arduino IDE)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP8266 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266 (eBook)<\/a><\/strong><\/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 (2nd Edition)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">More ESP8266 NodeMCU Projects and Tutorials\u2026<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to send emails with the ESP8266 NodeMCU board using an SMTP server. We&#8217;ll show how to send an email with raw text, HTML text, &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP8266 NodeMCU Send Emails using an SMTP Server: HTML, Text, and Attachments (Arduino)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-send-email-smtp-server-arduino\/#more-103424\" aria-label=\"Read more about ESP8266 NodeMCU Send Emails using an SMTP Server: HTML, Text, and Attachments (Arduino)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":103475,"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":[265,214,246,300,240,264],"tags":[],"class_list":["post-103424","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266-project","category-esp8266","category-esp8266-arduino-ide","category-0-esp8266","category-esp8266-projects","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-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\/103424","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=103424"}],"version-history":[{"count":27,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/103424\/revisions"}],"predecessor-version":[{"id":168903,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/103424\/revisions\/168903"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/103475"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=103424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=103424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=103424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}