{"id":98272,"date":"2023-09-19T12:45:56","date_gmt":"2023-09-19T12:45:56","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=98272"},"modified":"2024-06-16T14:58:23","modified_gmt":"2024-06-16T14:58:23","slug":"esp32-cam-send-photos-email","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-cam-send-photos-email\/","title":{"rendered":"ESP32-CAM: Take and Send Photos via Email using an SMTP Server"},"content":{"rendered":"\n<p>This tutorial shows how to send captured photos from the ESP32-CAM to your email account using an <a href=\"https:\/\/randomnerdtutorials.com\/esp32-send-email-smtp-server-arduino-ide\/\">SMTP Server<\/a>. We\u2019ll show you a simple example that takes a photo when the ESP32 boots and sends it as an attachment in an email. The last photo taken is temporarily saved in the ESP32 LittleFS filesystem.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/ESP32-CAM-Send-Email-1024x576.jpg?resize=1024%2C576&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32-CAM Take and Send Photos via Email using an SMTP Server Arduino IDE\" class=\"wp-image-136594\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/ESP32-CAM-Send-Email.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/ESP32-CAM-Send-Email.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/ESP32-CAM-Send-Email.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/ESP32-CAM-Send-Email.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgray\"><em>Updated 19 September 2023.<\/em><\/p>\n\n\n\n<p>To make this project work, the ESP32-CAM needs to be connected to a router with access to the internet \u2013 needs to be connected to your local network.<\/p>\n\n\n\n<p>This project is compatible with any <a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/esp32-camera-cam-boards-review-comparison\/\" target=\"_blank\">ESP32 camera board with the OV2640 camera<\/a>. You just need to make sure you use the right pin assignment for the board you&#8217;re using: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-camera-pin-gpios\/\">ESP32-CAM Camera Boards: Pin and GPIOs Assignment Guide<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ESP Mail Client Library<\/h2>\n\n\n\n<p>To send emails with the&nbsp;<a href=\"https:\/\/makeradvisor.com\/tools\/esp32-cam\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32-CAM<\/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 project, we&#8217;ll use <a href=\"https:\/\/randomnerdtutorials.com\/esp32-send-email-smtp-server-arduino-ide\/\">SMTP<\/a> to send an email with an attachment. The attachment is a photo taken with the ESP32-CAM.<\/p>\n\n\n\n<p>SMTP means Simple Mail Transfer Protocol and it is an internet standard for email transmission. To send emails through code, you need to know your SMTP server details. Each email provider has a different SMTP server.<\/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.&nbsp;<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\u2019ll 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,&nbsp;<a href=\"https:\/\/www.google.com\/gmail\/about\/\" target=\"_blank\" rel=\"noreferrer noopener\">go to this link<\/a>&nbsp;to 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 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 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\u00a0<strong>App Passwords<\/strong>.<\/li>\n\n\n\n<li>Open the\u00a0<strong>App Passwords<\/strong>\u00a0menu.<\/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 ESP32 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 ESP32 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 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\u2019re using a Gmail account, these are the SMTP Server details:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SMTP Server:&nbsp;<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):&nbsp;<strong>587<\/strong><\/li>\n\n\n\n<li>SMTP port (SSL):&nbsp;<strong>465<\/strong><\/li>\n\n\n\n<li>SMTP TLS\/SSL required:&nbsp;<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:&nbsp;<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:&nbsp;<strong>587<\/strong><\/li>\n\n\n\n<li>SMTP TLS\/SSL Required:&nbsp;<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:&nbsp;<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:&nbsp;<strong>587<\/strong><\/li>\n\n\n\n<li>SMTP TLS\/SSL Required:&nbsp;<strong>Yes<\/strong><\/li>\n<\/ul>\n\n\n\n<p>If you\u2019re using another email provider, you need to search for its SMTP Server settings. Now, you have everything ready to start sending emails with the ESP32-CAM.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code &#8211; ESP32-CAM Send Email<\/h2>\n\n\n\n<p>The following code takes a photo when the ESP32-CAM first boots and sends it to your email account. Before uploading the code, make sure you insert your sender email settings as well as your recipient email.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\r\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n  Complete instructions at https:\/\/RandomNerdTutorials.com\/esp32-cam-projects-ebook\/\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*********\/\r\n\r\n#include &quot;esp_camera.h&quot;\r\n#include &quot;SPI.h&quot;\r\n#include &quot;driver\/rtc_io.h&quot;\r\n#include &quot;soc\/rtc_cntl_reg.h&quot;  \/\/ Disable brownout problems\r\n#include &lt;ESP_Mail_Client.h&gt;\r\n#include &lt;FS.h&gt;\r\n#include &lt;WiFi.h&gt;\r\n\r\n\/\/ REPLACE WITH YOUR NETWORK CREDENTIALS\r\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\r\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\r\n\r\n\/\/ To send Email using Gmail use port 465 (SSL) and SMTP Server smtp.gmail.com\r\n\/\/ You need to create an email app password\r\n#define emailSenderAccount    &quot;SENDER_EMAIL@gmail.com&quot;\r\n#define emailSenderPassword   &quot;YOUR_EMAIL_APP_PASSWORD&quot;\r\n#define smtpServer            &quot;smtp.gmail.com&quot;\r\n#define smtpServerPort        465\r\n#define emailSubject          &quot;ESP32-CAM Photo Captured&quot;\r\n#define emailRecipient        &quot;YOUR_EMAIL_RECIPIENT@example.com&quot;\r\n\r\n#define CAMERA_MODEL_AI_THINKER\r\n\r\n#if defined(CAMERA_MODEL_AI_THINKER)\r\n  #define PWDN_GPIO_NUM     32\r\n  #define RESET_GPIO_NUM    -1\r\n  #define XCLK_GPIO_NUM      0\r\n  #define SIOD_GPIO_NUM     26\r\n  #define SIOC_GPIO_NUM     27\r\n  #define Y9_GPIO_NUM       35\r\n  #define Y8_GPIO_NUM       34\r\n  #define Y7_GPIO_NUM       39\r\n  #define Y6_GPIO_NUM       36\r\n  #define Y5_GPIO_NUM       21\r\n  #define Y4_GPIO_NUM       19\r\n  #define Y3_GPIO_NUM       18\r\n  #define Y2_GPIO_NUM        5\r\n  #define VSYNC_GPIO_NUM    25\r\n  #define HREF_GPIO_NUM     23\r\n  #define PCLK_GPIO_NUM     22\r\n#else\r\n  #error &quot;Camera model not selected&quot;\r\n#endif\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\n\/\/ Photo File Name to save in LittleFS\r\n#define FILE_PHOTO &quot;photo.jpg&quot;\r\n#define FILE_PHOTO_PATH &quot;\/photo.jpg&quot;\r\n\r\nvoid setup() {\r\n  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); \/\/disable brownout detector\r\n  \r\n  Serial.begin(115200);\r\n  Serial.println();\r\n\r\n  \/\/ Connect to Wi-Fi\r\n  WiFi.begin(ssid, password);\r\n  Serial.print(&quot;Connecting to WiFi...&quot;);\r\n  while (WiFi.status() != WL_CONNECTED) {\r\n    delay(500);\r\n    Serial.print(&quot;.&quot;);\r\n  }\r\n  Serial.println();\r\n  \r\n  \/\/ Print ESP32 Local IP Address\r\n  Serial.print(&quot;IP Address: http:\/\/&quot;);\r\n  Serial.println(WiFi.localIP());\r\n\r\n  \/\/ Init filesystem\r\n  ESP_MAIL_DEFAULT_FLASH_FS.begin();\r\n   \r\n  camera_config_t config;\r\n  config.ledc_channel = LEDC_CHANNEL_0;\r\n  config.ledc_timer = LEDC_TIMER_0;\r\n  config.pin_d0 = Y2_GPIO_NUM;\r\n  config.pin_d1 = Y3_GPIO_NUM;\r\n  config.pin_d2 = Y4_GPIO_NUM;\r\n  config.pin_d3 = Y5_GPIO_NUM;\r\n  config.pin_d4 = Y6_GPIO_NUM;\r\n  config.pin_d5 = Y7_GPIO_NUM;\r\n  config.pin_d6 = Y8_GPIO_NUM;\r\n  config.pin_d7 = Y9_GPIO_NUM;\r\n  config.pin_xclk = XCLK_GPIO_NUM;\r\n  config.pin_pclk = PCLK_GPIO_NUM;\r\n  config.pin_vsync = VSYNC_GPIO_NUM;\r\n  config.pin_href = HREF_GPIO_NUM;\r\n  config.pin_sccb_sda = SIOD_GPIO_NUM;\r\n  config.pin_sccb_scl = SIOC_GPIO_NUM;\r\n  config.pin_pwdn = PWDN_GPIO_NUM;\r\n  config.pin_reset = RESET_GPIO_NUM;\r\n  config.xclk_freq_hz = 20000000;\r\n  config.pixel_format = PIXFORMAT_JPEG;\r\n  config.grab_mode = CAMERA_GRAB_LATEST;\r\n  \r\n  if(psramFound()){\r\n    config.frame_size = FRAMESIZE_UXGA;\r\n    config.jpeg_quality = 10;\r\n    config.fb_count = 1;\r\n  } else {\r\n    config.frame_size = FRAMESIZE_SVGA;\r\n    config.jpeg_quality = 12;\r\n    config.fb_count = 1;\r\n  }\r\n\r\n  \/\/ Initialize camera\r\n  esp_err_t err = esp_camera_init(&amp;config);\r\n  if (err != ESP_OK) {\r\n    Serial.printf(&quot;Camera init failed with error 0x%x&quot;, err);\r\n    return;\r\n  }\r\n  \r\n  capturePhotoSaveLittleFS();\r\n  sendPhoto();\r\n}\r\n\r\nvoid loop() {\r\n\r\n}\r\n\r\n\/\/ Capture Photo and Save it to LittleFS\r\nvoid capturePhotoSaveLittleFS( void ) {\r\n  \/\/ Dispose first pictures because of bad quality\r\n  camera_fb_t* fb = NULL;\r\n  \/\/ Skip first 3 frames (increase\/decrease number as needed).\r\n  for (int i = 0; i &lt; 3; i++) {\r\n    fb = esp_camera_fb_get();\r\n    esp_camera_fb_return(fb);\r\n    fb = NULL;\r\n  }\r\n    \r\n  \/\/ Take a new photo\r\n  fb = NULL;  \r\n  fb = esp_camera_fb_get();  \r\n  if(!fb) {\r\n    Serial.println(&quot;Camera capture failed&quot;);\r\n    delay(1000);\r\n    ESP.restart();\r\n  }  \r\n\r\n  \/\/ Photo file name\r\n  Serial.printf(&quot;Picture file name: %s\\n&quot;, FILE_PHOTO_PATH);\r\n  File file = LittleFS.open(FILE_PHOTO_PATH, FILE_WRITE);\r\n\r\n  \/\/ Insert the data in the photo file\r\n  if (!file) {\r\n    Serial.println(&quot;Failed to open file in writing mode&quot;);\r\n  }\r\n  else {\r\n    file.write(fb-&gt;buf, fb-&gt;len); \/\/ payload (image), payload length\r\n    Serial.print(&quot;The picture has been saved in &quot;);\r\n    Serial.print(FILE_PHOTO_PATH);\r\n    Serial.print(&quot; - Size: &quot;);\r\n    Serial.print(fb-&gt;len);\r\n    Serial.println(&quot; bytes&quot;);\r\n  }\r\n  \/\/ Close the file\r\n  file.close();\r\n  esp_camera_fb_return(fb);\r\n}\r\n\r\nvoid sendPhoto( void ) {\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 data *\/\r\n  Session_Config config;\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 = 0;\r\n  config.time.day_light_offset = 1;\r\n\r\n  \/* Set the session config *\/\r\n  config.server.host_name = smtpServer;\r\n  config.server.port = smtpServerPort;\r\n  config.login.email = emailSenderAccount;\r\n  config.login.password = emailSenderPassword;\r\n  config.login.user_domain = &quot;&quot;;\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;ESP32-CAM&quot;;\r\n  message.sender.email = emailSenderAccount;\r\n\r\n  message.subject = emailSubject;\r\n  message.addRecipient(&quot;Sara&quot;, emailRecipient);\r\n\r\n  String htmlMsg = &quot;&lt;h2&gt;Photo captured with ESP32-CAM and attached in this email.&lt;\/h2&gt;&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 = FILE_PHOTO;\r\n  att.descr.mime = &quot;image\/png&quot;; \r\n  att.file.path = FILE_PHOTO_PATH;\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    return;\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\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  {\r\n    Serial.println(&quot;----------------&quot;);\r\n    Serial.printf(&quot;Message sent success: %d\\n&quot;, status.completedCount());\r\n    Serial.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\/ESP32-CAM-eBook\/raw\/master\/Code\/Module_3\/Send_Photos_Email_NEW\/Send_Photos_Email_NEW.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>Continue reading to learn how the code works, or skip to the <a href=\"#demonstration\" title=\"#demonstration\">Demonstration<\/a> section. Don\u2019t forget to insert your network credentials and email settings in the code. Also, if you\u2019re using a camera model other than an ESP32-CAM AI-Thinker, don\u2019t forget to change the pin assignment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Importing Libraries<\/h3>\n\n\n\n<p>Import the required libraries. The <span class=\"rnthl rntliteral\">ESP3_Mail_Client.h<\/span> is used to send emails, the <span class=\"rnthl rntliteral\">FS.h<\/span> is used to access and save files to LittleFS and the <span class=\"rnthl rntliteral\">WiFi.h<\/span> library is used to initialize Wi-Fi and connect your ESP32-CAM to your local network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include \"esp_camera.h\"\n#include \"SPI.h\"\n#include \"driver\/rtc_io.h\"\n#include &lt;ESP_Mail_Client.h&gt;\n#include &lt;FS.h&gt;\n#include &lt;WiFi.h&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Network Credentials<\/h3>\n\n\n\n<p>Insert your network credentials in the following variables:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Email Settings<\/h3>\n\n\n\n<p>Type the email sender account on the <span class=\"rnthl rntliteral\">emailSenderAccount<\/span> variable and its password on the <span class=\"rnthl rntliteral\">emailSenderPassword<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define emailSenderAccount   \"EXAMPLE_SENDER_ACCOUNT@gmail.com\"\n#define emailSenderPassword  \"SENDER_ACCOUNT_PASSWORD\"<\/code><\/pre>\n\n\n\n<p>Insert the recipient\u2019s email. This is the email that will receive the emails sent by the ESP32:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define emailRecipient \"YOUR_EMAIL_RECIPIENT@gmail.com\"<\/code><\/pre>\n\n\n\n<p>Insert your email provider SMTP settings on the following lines. We\u2019re using the settings for a Gmail account. If you\u2019re using a different email provider, replace with the corresponding SMTP settings.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define smtpServer \"smtp.gmail.com\"\n#define smtpServerPort 465<\/code><\/pre>\n\n\n\n<p>Write the email subject on the <span class=\"rnthl rntliteral\">emailSubject<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define emailSubject \"ESP32-CAM Photo Captured\"<\/code><\/pre>\n\n\n\n<p>Create a <span class=\"rnthl rntliteral\">STMPSession<\/span> object called <span class=\"rnthl rntliteral\">smtp<\/span> that contains the data to send via email and all the other configurations.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/* The SMTP Session object used for Email sending *\/\nSMTPSession smtp;<\/code><\/pre>\n\n\n\n<p>The photo taken with the ESP32 camera will be temporarily saved in LittleFS under the name <span class=\"rnthl rntliteral\">photo.jpg<\/span> on the root directory.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define FILE_PHOTO \"photo.jpg\"\n#define FILE_PHOTO_PATH \"\/photo.jpg\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">ESP32 Camera Pins<\/h3>\n\n\n\n<p>Define the pins used by your camera model. We&#8217;re using the ESP32-CAM AI-Thinker.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define PWDN_GPIO_NUM     32\n#define RESET_GPIO_NUM    -1\n#define XCLK_GPIO_NUM      0\n#define SIOD_GPIO_NUM     26\n#define SIOC_GPIO_NUM     27\n\n#define Y9_GPIO_NUM       35\n#define Y8_GPIO_NUM       34\n#define Y7_GPIO_NUM       39\n#define Y6_GPIO_NUM       36\n#define Y5_GPIO_NUM       21\n#define Y4_GPIO_NUM       19\n#define Y3_GPIO_NUM       18\n#define Y2_GPIO_NUM        5\n#define VSYNC_GPIO_NUM    25\n#define HREF_GPIO_NUM     23\n#define PCLK_GPIO_NUM     22<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">setup()<\/h3>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, connect the ESP32 to Wi-Fi.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Connect to Wi-Fi\nWiFi.begin(ssid, password);\nSerial.print(\"Connecting to WiFi...\");\nwhile (WiFi.status() != WL_CONNECTED) {\n  delay(500);\n  Serial.print(\".\");\n}\nSerial.println();<\/code><\/pre>\n\n\n\n<p>Print the ESP32-CAM IP address:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Print ESP32 Local IP Address\nSerial.print(\"IP Address: http:\/\/\");\nSerial.println(WiFi.localIP());<\/code><\/pre>\n\n\n\n<p>In the&nbsp;<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&nbsp;<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>The following lines configure the camera and set the camera settings:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>camera_config_t config;\nconfig.ledc_channel = LEDC_CHANNEL_0;\nconfig.ledc_timer = LEDC_TIMER_0;\nconfig.pin_d0 = Y2_GPIO_NUM;\nconfig.pin_d1 = Y3_GPIO_NUM;\nconfig.pin_d2 = Y4_GPIO_NUM;\nconfig.pin_d3 = Y5_GPIO_NUM;\nconfig.pin_d4 = Y6_GPIO_NUM;\nconfig.pin_d5 = Y7_GPIO_NUM;\nconfig.pin_d6 = Y8_GPIO_NUM;\nconfig.pin_d7 = Y9_GPIO_NUM;\nconfig.pin_xclk = XCLK_GPIO_NUM;\nconfig.pin_pclk = PCLK_GPIO_NUM;\nconfig.pin_vsync = VSYNC_GPIO_NUM;\nconfig.pin_href = HREF_GPIO_NUM;\nconfig.pin_sccb_sda = SIOD_GPIO_NUM;\nconfig.pin_sccb_scl = SIOC_GPIO_NUM;\nconfig.pin_pwdn = PWDN_GPIO_NUM;\nconfig.pin_reset = RESET_GPIO_NUM;\nconfig.xclk_freq_hz = 20000000;\nconfig.pixel_format = PIXFORMAT_JPEG;\nconfig.grab_mode = CAMERA_GRAB_LATEST;\n\nif(psramFound()){\n  config.frame_size = FRAMESIZE_UXGA;\n  config.jpeg_quality = 10;\n  config.fb_count = 2;\n} else {\n  config.frame_size = FRAMESIZE_SVGA;\n  config.jpeg_quality = 12;\n  config.fb_count = 1;\n}<\/code><\/pre>\n\n\n\n<p>Initialize the camera.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Initialize camera\nesp_err_t err = esp_camera_init(&amp;config);\nif (err != ESP_OK) {\n  Serial.printf(\"Camera init failed with error 0x%x\", err);\n  return;\n}<\/code><\/pre>\n\n\n\n<p>After initializing the camera, call the <span class=\"rnthl rntliteral\">capturePhotoSaveLittleFS()<\/span> and the <span class=\"rnthl rntliteral\">sendPhoto()<\/span> functions. These functions are defined at the end of the code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">capturePhotoSaveLittleFS() function<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">capturePhotoSaveLittleFS()<\/span> function captures a photo and saves it in the ESP32 LittleFS. In the following lines, you take a photo and save it in the framebuffer <span class=\"rnthl rntliteral\">FB<\/span>.<\/p>\n\n\n\n<p class=\"rntbox rntclgray\"><strong>Note:<\/strong> sometimes, the first pictures taken with the ESP32-CAM are not good because the sensor has not adjusted the white balance yet. So, to make sure we get a good picture, we discard the first three pictures (that number may vary depending on your board, so adjust accordingly).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/Dispose first pictures because of bad quality\ncamera_fb_t* fb = NULL;\n\/\/ Skip first 3 frames (increase\/decrease number as needed).\nfor (int i = 0; i &lt; 3; i++) {\n  fb = esp_camera_fb_get();\n  esp_camera_fb_return(fb);\n  fb = NULL;\n}\n    \n\/\/ Take a new photo\nfb = NULL;  \nfb = esp_camera_fb_get();  \nif(!fb) {\n  Serial.println(\"Camera capture failed\");\n  delay(1000);\n  ESP.restart();\n}  <\/code><\/pre>\n\n\n\n<p>Then, create a new file in LitteFS where the photo will be saved.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.printf(\"Picture file name: %s\\n\", FILE_PHOTO_PATH);\nFile file = LittleFS.open(FILE_PHOTO_PATH, FILE_WRITE);<\/code><\/pre>\n\n\n\n<p>Check if the file was successfully created. If not, print an error message.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (!file) {\n  Serial.println(\"Failed to open file in writing mode\");\n}<\/code><\/pre>\n\n\n\n<p>If a new file was successfully created, \u201ccopy\u201d the image from the buffer to that newly created file.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>file.write(fb-&gt;buf, fb-&gt;len); \/\/ payload (image), payload length\nSerial.print(\"The picture has been saved in \");\nSerial.print(FILE_PHOTO_PATH);\nSerial.print(\" - Size: \");\nSerial.print(fb-&gt;len);\nSerial.println(\" bytes\");<\/code><\/pre>\n\n\n\n<p>Close the file and clear the buffer for future use.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>file.close();\nesp_camera_fb_return(fb);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">sendPhoto() function<\/h3>\n\n\n\n<p>After having the photo successfully saved in LittleFS, we\u2019ll send it via email by calling the <span class=\"rnthl rntliteral\">sendPhoto()<\/span> function. Let\u2019s take a look at that function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void sendPhoto( void ) {<\/code><\/pre>\n\n\n\n<p>Enable debugging via serial port for the email status:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>smtp.debug(1);<\/code><\/pre>\n\n\n\n<p>Set the callback function to get the sending results:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>smtp.callback(smtpCallback);<\/code><\/pre>\n\n\n\n<p>Declare an email session:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Session_Config config;<\/code><\/pre>\n\n\n\n<p>Configure the time so that the email is timestamped correctly. You may need to adjust the <span class=\"rnthl rntliteral\">gmt_offset<\/span> variable depending on your location.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/*Set the NTP config time\nFor times east of the Prime Meridian use 0-12\nFor times west of the Prime Meridian add 12 to the offset.\nEx. American\/Denver GMT would be -6. 6 + 12 = 18\nSee https:\/\/en.wikipedia.org\/wiki\/Time_zone for a list of the GMT\/UTC timezone offsets\n*\/\nconfig.time.ntp_server = F(\"pool.ntp.org,time.nist.gov\");\nconfig.time.gmt_offset = 0;\nconfig.time.day_light_offset = 1;<\/code><\/pre>\n\n\n\n<p>Set the server host, port, sender email, and password for this email session:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/* Set the session config *\/\nconfig.server.host_name = smtpServer;\nconfig.server.port = smtpServerPort;\nconfig.login.email = emailSenderAccount;\nconfig.login.password = emailSenderPassword;\nconfig.login.user_domain = \"\";<\/code><\/pre>\n\n\n\n<p>Declare a <span class=\"rnthl rntliteral\">SMTP_Message<\/span> class:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>SMTP_Message message;<\/code><\/pre>\n\n\n\n<p>Set the message headers\u2014sender name, email sender, subject, and recipient (you can change the recipient name):<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/* Set the message headers *\/\nmessage.sender.name = \"ESP32-CAM\";\nmessage.sender.email = emailSenderAccount;\n\nmessage.subject = emailSubject;\nmessage.addRecipient(\"Sara\", emailRecipient);\n<\/code><\/pre>\n\n\n\n<p>In the following lines, set the content of the message in the <span class=\"rnthl rntliteral\">htmlMsg<\/span> variable:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String htmlMsg = \"&lt;h2&gt;Photo captured with ESP32-CAM and attached in this email.&lt;\/h2&gt;\";\nmessage.html.content = htmlMsg.c_str();\nmessage.html.charSet = \"utf-8\";\nmessage.html.transfer_encoding = Content_Transfer_Encoding::enc_qp;<\/code><\/pre>\n\n\n\n<p>Now, we need to take care of the attachments. Create an attachment:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>SMTP_Attachment att;<\/code><\/pre>\n\n\n\n<p>Set the attachment info: document name, mime type, file path, and where the content is saved (in our case it is saved in flash (<span class=\"rnthl rntliteral\">esp_mail_file_storage_type_flash<\/span>)):<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>att.descr.filename = FILE_PHOTO;\natt.descr.mime = \"image\/png\"; \natt.file.path = FILE_PHOTO_PATH;\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>Add the attachment to the message:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\n\/* Add attachment to the message *\/\nmessage.addAttachment(att);<\/code><\/pre>\n\n\n\n<p>Connect to the SMTP server:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/* Connect to server with the session config *\/\nif (!smtp.connect(&amp;config))\n  return;<\/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>\/* Start sending the Email and close the session *\/\nif (!MailClient.sendMail(&amp;smtp, &amp;message, true))\n  Serial.println(\"Error sending Email, \" + smtp.errorReason());<\/code><\/pre>\n\n\n\n<p>In this example, the email is sent once when the ESP32 boots, that\u2019s why the <span class=\"rnthl rntliteral\">loop()<\/span> is empty.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop() {<br>}<\/code><\/pre>\n\n\n\n<p>To send a new email, you just need to reset your board (press the on-board RESET button).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"demonstration\">Demonstration<\/h2>\n\n\n\n<p>After making the necessary changes to the code: camera pinout, sender\u2019s email address, sender\u2019s email password, recipient\u2019s email address, and network credentials, you can upload the code to your board.<\/p>\n\n\n\n<p>If you don&#8217;t know how to upload code to the ESP32-CAM, read the following post:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/program-upload-code-esp32-cam\/\">How to Program \/ Upload Code to ESP32-CAM AI-Thinker (Arduino IDE)<\/a><\/li>\n<\/ul>\n\n\n\n<p>After uploading, open the Serial Monitor and press the ESP32-CAM RESET button. The ESP32 should connect to Wi-Fi, take a photo, save it in LittleFS, connect to the SMTP server, and send the email as shown below.<\/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=\"606\" height=\"323\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/Send-email-ESP32-CAM-Serial-Monitor.png?resize=606%2C323&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Send Email ESP32-CAM Serial Monitor\" class=\"wp-image-132300\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/Send-email-ESP32-CAM-Serial-Monitor.png?w=606&amp;quality=100&amp;strip=all&amp;ssl=1 606w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/Send-email-ESP32-CAM-Serial-Monitor.png?resize=300%2C160&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/figure><\/div>\n\n\n<p>After a few seconds, you should have a new email from the ESP32-CAM in your inbox. As you can see in the image below, the sender\u2019s email name is \u201cESP32-CAM\u201d as we\u2019ve defined in the code, and the subject \u201cESP32-CAM Photo Captured\u201d.<\/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=\"1145\" height=\"307\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-CAM-Email-with-Photo-Received-Inbox.png?resize=1145%2C307&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 CAM Email with Photo Received Inbox\" class=\"wp-image-98287\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-CAM-Email-with-Photo-Received-Inbox.png?w=1145&amp;quality=100&amp;strip=all&amp;ssl=1 1145w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-CAM-Email-with-Photo-Received-Inbox.png?resize=300%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-CAM-Email-with-Photo-Received-Inbox.png?resize=1024%2C275&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-CAM-Email-with-Photo-Received-Inbox.png?resize=768%2C206&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1145px) 100vw, 1145px\" \/><\/figure><\/div>\n\n\n<p>Open the email and you should see a photo captured by the ESP32-CAM.<\/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=\"965\" height=\"624\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-CAM-Send-Photo-To-Email-Gmail-Inbox.png?resize=965%2C624&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 CAM Send Photo To Email Gmail Inbox\" class=\"wp-image-98288\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-CAM-Send-Photo-To-Email-Gmail-Inbox.png?w=965&amp;quality=100&amp;strip=all&amp;ssl=1 965w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-CAM-Send-Photo-To-Email-Gmail-Inbox.png?resize=300%2C194&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/ESP32-CAM-Send-Photo-To-Email-Gmail-Inbox.png?resize=768%2C497&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 965px) 100vw, 965px\" \/><\/figure><\/div>\n\n\n<p>You can open or download the photo to see it in full size.<\/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=\"562\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/Photo-taken-with-ESP32-CAM-Full-Size.jpg?resize=750%2C562&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Photo taken with ESP32 CAM Full Size\" class=\"wp-image-98289\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/Photo-taken-with-ESP32-CAM-Full-Size.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/07\/Photo-taken-with-ESP32-CAM-Full-Size.jpg?resize=300%2C225&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\u2019ve learned how to send emails with photos taken with the ESP32-CAM. The example presented is as simple as possible: it takes a photo and sends it via email when the ESP32-CAM first boots. To send another email, you need to reset the board.<\/p>\n\n\n\n<p>This project doesn\u2019t have a practical application, but it is useful to understand how to send an email with an attachment. After this, it should be fairly easy to include this feature in your own ESP32-CAM projects.<\/p>\n\n\n\n<p>Other ESP32-CAM projects you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-post-image-photo-server\/\">ESP32-CAM Post Images to Local or Cloud Server using PHP (Photo Manager)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-http-post-php-arduino\/\">ESP32-CAM HTTP POST Photos to Local or Cloud Server<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-take-photo-save-microsd-card\/\">ESP32-CAM Take Photo and Save to MicroSD Card<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-take-photo-display-web-server\/\">ESP32-CAM Take Photo and Display in Web Server<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP32-CAM:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-projects-ebook\/\">Build ESP32-CAM Projects using Arduino IDE eBook<\/a><\/strong><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32-cam\/\">More ESP32-CAM Projects and Tutorials\u2026<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to send captured photos from the ESP32-CAM to your email account using an SMTP Server. We\u2019ll show you a simple example that takes a photo when &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32-CAM: Take and Send Photos via Email using an SMTP Server\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-cam-send-photos-email\/#more-98272\" aria-label=\"Read more about ESP32-CAM: Take and Send Photos via Email using an SMTP Server\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":136594,"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":[276,281,277,319,264],"tags":[],"class_list":["post-98272","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp32-arduino-ide","category-esp32-cam","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/ESP32-CAM-Send-Email.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\/98272","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=98272"}],"version-history":[{"count":10,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/98272\/revisions"}],"predecessor-version":[{"id":159105,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/98272\/revisions\/159105"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/136594"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=98272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=98272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=98272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}