Para a versão que usa SSL/TLS, veja este post.
Este código é um aperfeiçoamento do código apresentado aqui.
Testado com IDE 1.8.1 e ESP8266 Core v2.3.0
Melhorias:
- Não é preciso pré-codificar usuário e senha em base64. Eu inclui as rotinas para isso;
- Removi o uso de flash strings, que aparentemente era a causa da rotina demorar absurdamente em certos pontos;
- Usei constantes no lugar de strings “hardcoded”;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
/* * Este sketch não usa conexão criptografada. Suas credenciais poderão ser interceptadas * Use uma conta de email descartável * * */ // Você precisa mudar estas constantes ========================== #include <ESP8266WiFi.h> const char* SSID = "-------------"; const char* PASS = "-------------"; char smtp_server[] = "exemplo.com.br"; //esta tem que ser a porta do seu provedor que aceita conexões sem criptografia const int smtp_port = 587; //pode ser 25, 2525, 587, etc. Veja com seu provedor de email. const String mail_username="user@exemplo.com.br"; const String mail_password="minhasenha"; const String mail_from="ESP8266 <user@servidor_remetente>"; const String mail_to="Jefferson <user@servidor_destino>"; const String mail_subject="Enviado pelo ESP8266"; const String mail_line1="Esta é a linha 1\n"; const String mail_line2="Esta é a linha 2"; const String mail_line3="Esta é a linha 3"; //============================================================ const char* _b64_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; WiFiClient client; void a3_to_a4(unsigned char * a4, unsigned char * a3) { a4[0] = (a3[0] & 0xfc) >> 2; a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4); a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6); a4[3] = (a3[2] & 0x3f); } int base64_encode(char *output, const char *input, int inputLen) { int i = 0, j = 0; int encLen = 0; unsigned char a3[3]; unsigned char a4[4]; while(inputLen--) { a3[i++] = *(input++); if(i == 3) { a3_to_a4(a4, a3); for(i = 0; i < 4; i++) { output[encLen++] = _b64_alphabet[a4[i]]; } i = 0; } } if(i) { for(j = i; j < 3; j++) { a3[j] = '\0'; } a3_to_a4(a4, a3); for(j = 0; j < i + 1; j++) { output[encLen++] = _b64_alphabet[a4[j]]; } while((i++ < 3)) { output[encLen++] = '='; } } output[encLen] = '\0'; return encLen; } void setup() { Serial.begin(115200); delay(10); Serial.println(""); Serial.println(""); Serial.print("Connecting To "); Serial.println(SSID); WiFi.begin(SSID, PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi Connected"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); byte ret = sendEmail(); } void loop() { } byte sendEmail() { byte thisByte = 0; byte respCode; if (client.connect(smtp_server, smtp_port) == 1) { Serial.println("connected"); } else { Serial.println("connection failed"); return 0; } if (!eRcv()) return 0; char* buf = (char*)malloc(100); Serial.println("Sending EHLO"); client.print("EHLO "); client.println(smtp_server); if (!eRcv()) return 0; Serial.println("Sending auth login"); client.println("auth login"); if (!eRcv()) return 0; Serial.println("Sending User"); base64_encode(buf, mail_username.c_str(), mail_username.length()); client.println(buf); if (!eRcv()) return 0; Serial.println("Sending Password"); base64_encode(buf, mail_password.c_str(), mail_password.length()); client.println(buf); if (!eRcv()) return 0; Serial.println("Sending From"); // change to your email address (sender) client.print("MAIL From: "); client.println(mail_from); if (!eRcv()) return 0; // change to recipient address Serial.println("Sending To"); client.print("RCPT To: "); client.println(mail_to); if (!eRcv()) return 0; Serial.println("Sending DATA"); client.println("DATA"); if (!eRcv()) return 0; Serial.println("Sending email"); // change to recipient address client.print("To: "); client.println(mail_to); // change to your address client.print("From: "); client.println(mail_from); //Este é o "nome para exibição" client.print("Subject: "); client.print(mail_subject); client.println("\r\n"); client.println(mail_line1); client.println(mail_line2); client.println(mail_line3); client.println("."); //O email tem que terminar assim if (!eRcv()) return 0; Serial.println("Sending QUIT"); client.println("QUIT"); if (!eRcv()) return 0; client.stop(); Serial.println("disconnected"); return 1; } byte eRcv() { byte respCode; byte thisByte; int loopCount = 0; while (!client.available()) { delay(1); loopCount++; // if nothing received for 10 seconds, timeout if (loopCount > 10000) { client.stop(); Serial.println("\r\nTimeout"); return 0; } } respCode = client.peek(); while (client.available()) { thisByte = client.read(); Serial.write(thisByte); } if (respCode >= '4') { // efail(); return 0; } return 1; } |
Versão que prefiro
Eu criei esta versão porque preferia uma rotina de envio mais “limpa” e o log na porta serial feito de forma a representar com mais fidelidade a comunicação com o servidor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
/* * Este sketch não usa conexão criptografada. Suas credenciais poderão ser interceptadas * Use uma conta de email descartável * * */ #include <ESP8266WiFi.h> const char* SSID = ""; const char* PASS = ""; char smtp_server[] = ""; //esta tem que ser a porta do seu provedor que aceita conexões sem criptografia const int smtp_port = 587; //pode ser 25, 2525 587, 465, etc. Veja com seu provedor de email. const String mail_username=""; const String mail_password=""; const String mail_from=""; const String mail_to=""; const String mail_subject="Enviado pelo ESP8266"; const String mail_line1="Esta é a linha 1\n"; const String mail_line2="Esta é a linha 2"; const String mail_line3="Esta é a linha 3"; bool construindoLinha=false; const char* _b64_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; WiFiClient client; void printFreeRAM(){ Serial.print("RAM livre: "); Serial.print(ESP.getFreeHeap()); Serial.println(" bytes."); } void a3_to_a4(unsigned char * a4, unsigned char * a3) { a4[0] = (a3[0] & 0xfc) >> 2; a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4); a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6); a4[3] = (a3[2] & 0x3f); } int base64_encode(char *output, const char *input, int inputLen) { int i = 0, j = 0; int encLen = 0; unsigned char a3[3]; unsigned char a4[4]; while(inputLen--) { a3[i++] = *(input++); if(i == 3) { a3_to_a4(a4, a3); for(i = 0; i < 4; i++) { output[encLen++] = _b64_alphabet[a4[i]]; } i = 0; } } if(i) { for(j = i; j < 3; j++) { a3[j] = '\0'; } a3_to_a4(a4, a3); for(j = 0; j < i + 1; j++) { output[encLen++] = _b64_alphabet[a4[j]]; } while((i++ < 3)) { output[encLen++] = '='; } } output[encLen] = '\0'; return encLen; } void setup() { Serial.begin(115200); delay(10); Serial.println(""); Serial.println(""); Serial.print("Conectando a: "); Serial.println(SSID); Serial.print("Com a senha: "); Serial.println(PASS); WiFi.begin(SSID, PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi Conectado."); Serial.print("Endereco IP local: "); Serial.println(WiFi.localIP()); printFreeRAM(); byte ret = sendEmail(); printFreeRAM(); } void loop() { } void SEND(String message) { if (!construindoLinha){Serial.print("EU: ");}; //Imprimo apenas no início de cada linha Serial.print (message); client.print (message); construindoLinha=true; } void SENDln(String message) { if (!construindoLinha){Serial.print("EU: ");}; //Imprimo apenas no início de cada linha Serial.println (message); client.println (message); construindoLinha=false; } byte sendEmail() { byte thisByte = 0; byte respCode; if (client.connect(smtp_server, smtp_port) == 1) { Serial.println("Conectado."); } else { Serial.println("Falha na conexao."); return 0; } if (!eRcv()) return 0; char* buf = (char*)malloc(100); SEND("EHLO "); SENDln(smtp_server); if (!eRcv()) return 0; SENDln("auth login"); if (!eRcv()) return 0; base64_encode(buf, mail_username.c_str(), mail_username.length()); client.println(buf); Serial.println(buf); if (!eRcv()) return 0; base64_encode(buf, mail_password.c_str(), mail_password.length()); client.println(buf); Serial.println(buf); if (!eRcv()) return 0; SEND("MAIL From: "); SENDln(mail_from); if (!eRcv()) return 0; SEND("RCPT To: "); SENDln(mail_to); if (!eRcv()) return 0; SENDln("DATA"); if (!eRcv()) return 0; SEND("To: "); SENDln(mail_to); SEND("From: "); SENDln(mail_from); //Este é o "nome para exibição" SEND("Subject: "); SENDln(mail_subject); SENDln(mail_line1); SENDln(mail_line2); SENDln(mail_line3); SENDln("."); //O email tem que terminar assim if (!eRcv()) return 0; SENDln("QUIT"); if (!eRcv()) return 0; client.stop(); Serial.println("Desconectado."); return 1; } byte eRcv() { byte respCode; byte thisByte; int loopCount = 0; while (!client.available()) { delay(1); loopCount++; // Se nada for recebido em 10s, desisto. if (loopCount > 10000) { client.stop(); Serial.println("\r\nTimeout"); return 0; } } respCode = client.peek(); while (client.available()) { thisByte = client.read(); Serial.write(thisByte); } if (respCode >= '4') { // efail(); return 0; } return 1; } |
Nessa segunda versão, se você rodar várias vezes enviando e-mails, de tempos em tempos, em certo momento a memória vai estourar por conta do malloc, pra resolver é só desalocar no fim da função.
Desculpe a demora para aprovar seu comentário e obrigado pela dica!
Para resolver o problema indicado por Marco é preciso incluir uma linha:
free(buf);
no(s) lugar(es) apropriado(s) da função sendEmail().
Não exatamente no fim da função, pois esta pode terminar em qualquer ponto onde há um “return”. Eu só vou corrigir a listagem do programa quando eu tiver tempo para voltar a esse problema e testar no ESP8266.
Ola Jefferson. Parabéns pela iniciativa. Já fez a atualização do código?
gostei da leveza.
hoje zap nodemcu envolve 8 boletos de micro serviços pra cobrir a falta que faz uma interface/api graphql de alto nivel as custas de trampo de baixo nivel que nao vai aparecer.
claro, td em 20 ou menos linhas.