Advertisement
Maderdash

HTML

Aug 19th, 2022
1,334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Rui Santos
  3.   Complete project details at:
  4.    - ESP32: https://RandomNerdTutorials.com/esp32-send-email-smtp-server-arduino-ide/
  5.    - ESP8266: https://RandomNerdTutorials.com/esp8266-nodemcu-send-email-smtp-server-arduino/
  6.  
  7.   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
  8.   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  9.   Example adapted from: https://github.com/mobizt/ESP-Mail-Client
  10. */
  11.  
  12. // To send Emails using Gmail on port 465 (SSL), you need to create an app password: https://support.google.com/accounts/answer/185833
  13.  
  14. #include <Arduino.h>
  15. #if defined(ESP32)
  16.   #include <WiFi.h>
  17. #elif defined(ESP8266)
  18.   #include <ESP8266WiFi.h>
  19. #endif
  20. #include <ESP_Mail_Client.h>
  21.  
  22. #define WIFI_SSID "REPLACE_WITH_YOUR_SSID"
  23. #define WIFI_PASSWORD "REPLACE_WITH_YOUR_PASSWORD"
  24.  
  25. #define SMTP_HOST "smtp.gmail.com"
  26. #define SMTP_PORT 465
  27.  
  28. /* The sign in credentials */
  29. #define AUTHOR_EMAIL "YOUR_EMAIL@XXXX.com"
  30. #define AUTHOR_PASSWORD "YOUR_EMAIL_PASS"
  31.  
  32. /* Recipient's email*/
  33. #define RECIPIENT_EMAIL "RECIPIENTE_EMAIL@XXXX.com"
  34.  
  35. /* The SMTP Session object used for Email sending */
  36. SMTPSession smtp;
  37.  
  38. /* Callback function to get the Email sending status */
  39. void smtpCallback(SMTP_Status status);
  40.  
  41. void setup(){
  42.   Serial.begin(115200);
  43.   Serial.println();
  44.   Serial.print("Connecting to AP");
  45.   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  46.   while (WiFi.status() != WL_CONNECTED){
  47.     Serial.print(".");
  48.     delay(200);
  49.   }
  50.   Serial.println("");
  51.   Serial.println("WiFi connected.");
  52.   Serial.println("IP address: ");
  53.   Serial.println(WiFi.localIP());
  54.   Serial.println();
  55.  
  56.   /** Enable the debug via Serial port
  57.    * none debug or 0
  58.    * basic debug or 1
  59.   */
  60.   smtp.debug(1);
  61.  
  62.   /* Set the callback function to get the sending results */
  63.   smtp.callback(smtpCallback);
  64.  
  65.   /* Declare the session config data */
  66.   ESP_Mail_Session session;
  67.  
  68.   /* Set the session config */
  69.   session.server.host_name = SMTP_HOST;
  70.   session.server.port = SMTP_PORT;
  71.   session.login.email = AUTHOR_EMAIL;
  72.   session.login.password = AUTHOR_PASSWORD;
  73.   session.login.user_domain = "";
  74.  
  75.   /* Declare the message class */
  76.   SMTP_Message message;
  77.  
  78.   /* Set the message headers */
  79.   message.sender.name = "ESP";
  80.   message.sender.email = AUTHOR_EMAIL;
  81.   message.subject = "ESP Test Email";
  82.   message.addRecipient("Sara", RECIPIENT_EMAIL);
  83.  
  84.   /*Send HTML message*/
  85.   String htmlMsg = "<div style=\"color:#2f4468;\"><h1>Hello World!</h1><p>- Sent from ESP board</p></div>";
  86.   message.html.content = htmlMsg.c_str();
  87.   message.html.content = htmlMsg.c_str();
  88.   message.text.charSet = "us-ascii";
  89.   message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
  90.  
  91.   /*
  92.   //Send raw text message
  93.   String textMsg = "Hello World! - Sent from ESP board";
  94.   message.text.content = textMsg.c_str();
  95.   message.text.charSet = "us-ascii";
  96.   message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
  97.  
  98.   message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_low;
  99.   message.response.notify = esp_mail_smtp_notify_success | esp_mail_smtp_notify_failure | esp_mail_smtp_notify_delay;*/
  100.  
  101.   /* Set the custom message header */
  102.   //message.addHeader("Message-ID: <abcde.fghij@gmail.com>");
  103.  
  104.   /* Connect to server with the session config */
  105.   if (!smtp.connect(&session))
  106.     return;
  107.  
  108.   /* Start sending Email and close the session */
  109.   if (!MailClient.sendMail(&smtp, &message))
  110.     Serial.println("Error sending Email, " + smtp.errorReason());
  111. }
  112.  
  113. void loop(){
  114.  
  115. }
  116.  
  117. /* Callback function to get the Email sending status */
  118. void smtpCallback(SMTP_Status status){
  119.   /* Print the current status */
  120.   Serial.println(status.info());
  121.  
  122.   /* Print the sending result */
  123.   if (status.success()){
  124.     Serial.println("----------------");
  125.     ESP_MAIL_PRINTF("Message sent success: %d\n", status.completedCount());
  126.     ESP_MAIL_PRINTF("Message sent failled: %d\n", status.failedCount());
  127.     Serial.println("----------------\n");
  128.     struct tm dt;
  129.  
  130.     for (size_t i = 0; i < smtp.sendingResult.size(); i++){
  131.       /* Get the result item */
  132.       SMTP_Result result = smtp.sendingResult.getItem(i);
  133.       time_t ts = (time_t)result.timestamp;
  134.       localtime_r(&ts, &dt);
  135.  
  136.       ESP_MAIL_PRINTF("Message No: %d\n", i + 1);
  137.       ESP_MAIL_PRINTF("Status: %s\n", result.completed ? "success" : "failed");
  138.       ESP_MAIL_PRINTF("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
  139.       ESP_MAIL_PRINTF("Recipient: %s\n", result.recipients);
  140.       ESP_MAIL_PRINTF("Subject: %s\n", result.subject);
  141.     }
  142.     Serial.println("----------------\n");
  143.   }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement