├── ESP8366_Send_eMail_YT.ino ├── ESP8366_Send_eMail_YT_02.ino ├── Licence.txt └── README.md /ESP8366_Send_eMail_YT.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const int SMTP_PORT = 465; 6 | const char* SMTP_SERVER = "smtp.gmail.com"; 7 | String error_message; 8 | String ServerResponse; 9 | //################################################ 10 | const char* ssid = "your_wifi_ssid"; // WIFI network name 11 | const char* password = "your_wifi_password"; // WIFI network password 12 | //################################################ 13 | String Senders_Login = "your_email_user_account@gdomain.com"; 14 | String Senders_Password = "your_email_account_password"; 15 | String From; 16 | //################################################ 17 | String To, Subject, Message, Login_base64, Passwrd_base64; 18 | 19 | WiFiClientSecure client; 20 | 21 | void setup() { 22 | Serial.begin(115200); 23 | WiFi.begin(ssid, password); 24 | Serial.print("Connecting to :"+String(ssid)); 25 | } 26 | 27 | void loop() { 28 | To = "recipients_email_address@domain.com"; 29 | From = "your_email_address@domain.com"; 30 | Subject = "Test Message"; 31 | Message = "This is a test message to demonstrate how to send an email from and ESP8266"; 32 | SendMail(To, Subject, Message); 33 | if (error_message != "") Serial.println(error_message); 34 | delay(10000000); 35 | } 36 | 37 | void SendMail(String To, String Subject, String Message) { 38 | if (!client.connect(SMTP_SERVER, SMTP_PORT)) { 39 | error_message = "SMTP responded that it could not connect to the mail server"; 40 | return; 41 | } 42 | if (ErrorWhileWaitingForSMTP_Response("220", 500)) { 43 | error_message = "SMTP responded with a Connection Error"; 44 | return; 45 | } 46 | client.println("HELO server"); 47 | if (ErrorWhileWaitingForSMTP_Response("250", 500)) { 48 | error_message = "SMTP responded with an Identification error"; 49 | return; 50 | } 51 | client.println("AUTH LOGIN"); 52 | WaitSMTPResponse(ServerResponse, 500); 53 | client.println(base64::encode(Senders_Login)); 54 | WaitSMTPResponse(ServerResponse, 500); 55 | client.println(base64::encode(Senders_Password));; 56 | if (ErrorWhileWaitingForSMTP_Response("235", 500)) { 57 | error_message = "SMTP responded with an Authorisation error"; 58 | return; 59 | } 60 | String mailFrom = "MAIL FROM: <" + String(From) + '>'; 61 | client.println(mailFrom); 62 | WaitSMTPResponse(ServerResponse, 500); 63 | String recipient = "RCPT TO: <" + To + '>'; 64 | client.println(recipient); 65 | WaitSMTPResponse(ServerResponse, 500); 66 | client.println("DATA"); 67 | if (ErrorWhileWaitingForSMTP_Response("354", 500)) { 68 | error_message = "SMTP DATA error"; 69 | return; 70 | } 71 | client.println("From: <" + String(From) + '>'); 72 | client.println("To: <" + String(To) + '>'); 73 | client.print("Subject: "); 74 | client.println(String(Subject)); 75 | client.println("Mime-Version: 1.0"); 76 | client.println("Content-Type: text/html; charset=\"UTF-8\""); 77 | client.println("Content-Transfer-Encoding: 7bit"); 78 | client.println(); 79 | String body = "" + Message + ""; 80 | client.println(body); 81 | client.println("."); 82 | if (ErrorWhileWaitingForSMTP_Response("250", 1000)) { 83 | error_message = "SMTP responded with a Message error"; 84 | return; 85 | } 86 | client.println("QUIT"); 87 | if (ErrorWhileWaitingForSMTP_Response("221", 1000)) { 88 | error_message = "SMTP responded with a QUIT error"; 89 | return; 90 | } 91 | client.stop(); 92 | Serial.println("Message Sent"); 93 | } 94 | 95 | bool ErrorWhileWaitingForSMTP_Response(String Error_Code, int TimeOut) { 96 | int timer = millis(); 97 | while (!client.available()) { 98 | if (millis() > (timer + TimeOut)) { 99 | error_message = "SMTP responsed that a Timeout occurred"; 100 | return true; 101 | } 102 | } 103 | ServerResponse = client.readStringUntil('\n'); 104 | if (ServerResponse.indexOf(Error_Code) == -1) return true; 105 | return false; 106 | } 107 | 108 | bool WaitSMTPResponse(String Error_Code, int TimeOut) { 109 | int timer = millis(); 110 | while (!client.available()) { 111 | if (millis() > (timer + TimeOut)) { 112 | error_message = "SMTP responded that a Timeout occurred"; 113 | return false; 114 | } 115 | } 116 | ServerResponse = client.readStringUntil('\n'); 117 | if (ServerResponse.indexOf(Error_Code) == -1) return false; 118 | return true; 119 | } 120 | 121 | -------------------------------------------------------------------------------- /ESP8366_Send_eMail_YT_02.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const int SMTP_PORT = 465; 6 | const char* SMTP_SERVER = "smtp.gmail.com"; 7 | String error_message; 8 | String ServerResponse; 9 | //################################################ 10 | const char* ssid = "your_wifi_ssid"; // WIFI network name 11 | const char* password = "your_wifi_password"; // WIFI network password 12 | //################################################ 13 | String Senders_Login = "your_email_user_account@gdomain.com"; 14 | String Senders_Password = "your_email_account_password"; 15 | String From; 16 | //################################################ 17 | String To, Subject, Message, Login_base64, Passwrd_base64; 18 | 19 | WiFiClientSecure client; 20 | 21 | void setup() { 22 | Serial.begin(115200); 23 | WiFi.begin(ssid, password); 24 | Serial.print("Connecting to :"+String(ssid)); 25 | } 26 | 27 | void loop() { 28 | To = "recipients_email_address@domain.com"; 29 | From = "your_email_address@domain.com"; 30 | Subject = "Test Message"; 31 | Message = "This is a test message to demonstrate how to send an email from and ESP8266"; 32 | SendMail(To, Subject, Message); 33 | if (error_message != "") Serial.println(error_message); 34 | delay(10000000); 35 | } 36 | 37 | void SendMail(String To, String Subject, String Message) { 38 | if (!client.connect(SMTP_SERVER, SMTP_PORT)) { 39 | error_message = "SMTP responded that it could not connect to the mail server"; 40 | return; 41 | } 42 | if (ErrorWhileWaitingForSMTP_Response("220", 500)) { 43 | error_message = "SMTP responded with a Connection Error"; 44 | return; 45 | } 46 | client.println("HELO server"); 47 | if (ErrorWhileWaitingForSMTP_Response("250", 500)) { 48 | error_message = "SMTP responded with an Identification error"; 49 | return; 50 | } 51 | client.println("AUTH LOGIN"); 52 | WaitSMTPResponse(ServerResponse, 500); 53 | client.println(base64::encode(Senders_Login)); 54 | WaitSMTPResponse(ServerResponse, 500); 55 | client.println(base64::encode(Senders_Password));; 56 | if (ErrorWhileWaitingForSMTP_Response("235", 500)) { 57 | error_message = "SMTP responded with an Authorisation error"; 58 | return; 59 | } 60 | String mailFrom = "MAIL FROM: <" + String(From) + '>'; 61 | client.println(mailFrom); 62 | WaitSMTPResponse(ServerResponse, 500); 63 | String recipient = "RCPT TO: <" + To + '>'; 64 | client.println(recipient); 65 | WaitSMTPResponse(ServerResponse, 500); 66 | client.println("DATA"); 67 | if (ErrorWhileWaitingForSMTP_Response("354", 500)) { 68 | error_message = "SMTP DATA error"; 69 | return; 70 | } 71 | client.println("From: <" + String(From) + '>'); 72 | client.println("To: <" + String(To) + '>'); 73 | client.print("Subject: "); 74 | client.println(String(Subject)); 75 | client.println("Mime-Version: 1.0"); 76 | client.println("Content-Type: text/html; charset=\"UTF-8\""); 77 | client.println("Content-Transfer-Encoding: 7bit"); 78 | client.println(); 79 | String body = "" + Message + ""; 80 | client.println(body); 81 | client.println("."); 82 | if (ErrorWhileWaitingForSMTP_Response("250", 1000)) { 83 | error_message = "SMTP responded with a Message error"; 84 | return; 85 | } 86 | client.println("QUIT"); 87 | if (ErrorWhileWaitingForSMTP_Response("221", 1000)) { 88 | error_message = "SMTP responded with a QUIT error"; 89 | return; 90 | } 91 | client.stop(); 92 | Serial.println("Message Sent"); 93 | } 94 | 95 | bool ErrorWhileWaitingForSMTP_Response(String Error_Code, int TimeOut) { 96 | int timer = millis(); 97 | while (!client.available()) { 98 | if (millis() > (timer + TimeOut)) { 99 | error_message = "SMTP responsed that a Timeout occurred"; 100 | return true; 101 | } 102 | } 103 | ServerResponse = client.readStringUntil('\n'); 104 | Serial.println("Server responded:\n")+ServerResponse); 105 | if (ServerResponse.indexOf(Error_Code) == -1) return true; 106 | return false; 107 | } 108 | 109 | bool WaitSMTPResponse(String Error_Code, int TimeOut) { 110 | int timer = millis(); 111 | while (!client.available()) { 112 | if (millis() > (timer + TimeOut)) { 113 | error_message = "SMTP responded that a Timeout occurred"; 114 | return false; 115 | } 116 | } 117 | ServerResponse = client.readStringUntil('\n'); 118 | Serial.println("Server responded:\n")+ServerResponse); 119 | if (ServerResponse.indexOf(Error_Code) == -1) return false; 120 | return true; 121 | } 122 | -------------------------------------------------------------------------------- /Licence.txt: -------------------------------------------------------------------------------- 1 | This software, the ideas and concepts is Copyright (c) David Bird 2014 and beyond. 2 | 3 | All rights to this software are reserved. 4 | 5 | It is prohibited to redistribute or reproduce of any part or all of the software contents in any form other than the following: 6 | 7 | 1. You may print or download to a local hard disk extracts for your personal and non-commercial use only. 8 | 9 | 2. You may copy the content to individual third parties for their personal use, but only if you acknowledge the author David Bird as the source of the material. 10 | 11 | 3. You may not, except with my express written permission, distribute or commercially exploit the content. 12 | 13 | 4. You may not transmit it or store it in any other website or other form of electronic retrieval system for commercial purposes. 14 | 15 | 5. You MUST include all of this copyright and permission notice ('as annotated') and this shall be included in all copies or substantial portions of the software and where the software use is visible to an end-user. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS" FOR PRIVATE USE ONLY, IT IS NOT FOR COMMERCIAL USE IN WHOLE OR PART OR CONCEPT. 18 | 19 | FOR PERSONAL USE IT IS SUPPLIED WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | 21 | IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266-Sending-Emails 2 | How to use an ESP8266 to send emails - perhaps low voltage, low battery or an alert on over temperature 3 | 4 | NOTE: This is currenrtly only working with the IDE ESP8266 core version 2.3.0 5 | IDE, Tools, Board, Board Manager, then type ESP8266 in the top-right search box and choose the 2.3.0 release 6 | --------------------------------------------------------------------------------