|
| 1 | +/* |
| 2 | + * EMailSender library for Arduino, esp8266 and esp32 |
| 3 | + * Simple esp32 Gmail send to a distribution list example |
| 4 | + * |
| 5 | + * https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/ |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +#include "Arduino.h" |
| 10 | +#include <EMailSender.h> |
| 11 | +#include <WiFi.h> |
| 12 | + |
| 13 | +uint8_t connection_state = 0; |
| 14 | +uint16_t reconnect_interval = 10000; |
| 15 | + |
| 16 | +EMailSender emailSend("<YOUR-SMTP>", "<YOUR-SMTP-PASSWD>"); |
| 17 | + |
| 18 | +uint8_t WiFiConnect(const char* nSSID = nullptr, const char* nPassword = nullptr) |
| 19 | +{ |
| 20 | + static uint16_t attempt = 0; |
| 21 | + Serial.print("Connecting to "); |
| 22 | + if(nSSID) { |
| 23 | + WiFi.begin(nSSID, nPassword); |
| 24 | + Serial.println(nSSID); |
| 25 | + } |
| 26 | + |
| 27 | + uint8_t i = 0; |
| 28 | + while(WiFi.status()!= WL_CONNECTED && i++ < 50) |
| 29 | + { |
| 30 | + delay(200); |
| 31 | + Serial.print("."); |
| 32 | + } |
| 33 | + ++attempt; |
| 34 | + Serial.println(""); |
| 35 | + if(i == 51) { |
| 36 | + Serial.print("Connection: TIMEOUT on attempt: "); |
| 37 | + Serial.println(attempt); |
| 38 | + if(attempt % 2 == 0) |
| 39 | + Serial.println("Check if access point available or SSID and Password\r\n"); |
| 40 | + return false; |
| 41 | + } |
| 42 | + Serial.println("Connection: ESTABLISHED"); |
| 43 | + Serial.print("Got IP address: "); |
| 44 | + Serial.println(WiFi.localIP()); |
| 45 | + return true; |
| 46 | +} |
| 47 | + |
| 48 | +void Awaits() |
| 49 | +{ |
| 50 | + uint32_t ts = millis(); |
| 51 | + while(!connection_state) |
| 52 | + { |
| 53 | + delay(50); |
| 54 | + if(millis() > (ts + reconnect_interval) && !connection_state){ |
| 55 | + connection_state = WiFiConnect(); |
| 56 | + ts = millis(); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void setup() |
| 62 | +{ |
| 63 | + Serial.begin(115200); |
| 64 | + const char* ssid = "<YOUR-SSID>"; |
| 65 | + const char* password = "<YOUR-PASSWD>"; |
| 66 | + |
| 67 | + connection_state = WiFiConnect(ssid, password); |
| 68 | + if(!connection_state) // if not connected to WIFI |
| 69 | + Awaits(); // constantly trying to connect |
| 70 | + |
| 71 | + EMailSender::EMailMessage message; |
| 72 | + message.subject = "Soggetto"; |
| 73 | + message.message = "Ciao come stai<br>io bene.<br>www.mischianti.org"; |
| 74 | + |
| 75 | + // Send to 3 different email |
| 76 | + const char* arrayOfEmail[] = {"<FIRST>@gmail.com", "<SECOND>@yahoo.com", "<THIRD>@hotmail.com"}; |
| 77 | + EMailSender::Response resp = emailSend.send(arrayOfEmail, 3, message); |
| 78 | + |
| 79 | +// // Send to 3 different email, 2 in C and 1 in CC |
| 80 | +// const char* arrayOfEmail[] = {"<FIRST>@gmail.com", "<SECOND>@yahoo.com", "<THIRD>@hotmail.com"}; |
| 81 | +// EMailSender::Response resp = emailSend.send(arrayOfEmail, 2, 1, message); |
| 82 | +// |
| 83 | +// // Send to 3 different email first to C second to CC and third to CCn |
| 84 | +// const char* arrayOfEmail[] = {"<FIRST>@gmail.com", "<SECOND>@yahoo.com", "<THIRD>@hotmail.com"}; |
| 85 | +// EMailSender::Response resp = emailSend.send(arrayOfEmail, 3, message); |
| 86 | + |
| 87 | + |
| 88 | + Serial.println("Sending status: "); |
| 89 | + |
| 90 | + Serial.println(resp.status); |
| 91 | + Serial.println(resp.code); |
| 92 | + Serial.println(resp.desc); |
| 93 | +} |
| 94 | + |
| 95 | +void loop() |
| 96 | +{ |
| 97 | + |
| 98 | +} |
0 commit comments