WiFiManager and Wemos d1 esp8266 sending mail problem

Home Forums The libraries hosted on the site EMailSender send email with attachments WiFiManager and Wemos d1 esp8266 sending mail problem

Viewing 13 reply threads
  • Author
    Posts
    • #18969
      claudacate
      Participant

        Total newbie here. I just can’t figure out why I can’t connect to the smtp server. I have used the app password. Can someone take a look for me? here is my code. Thanks for the assist

        #include <WiFiClient.h> 
        #include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
        #include <TimeLib.h> 
        #include <ESP8266WiFi.h>
        #include <ESP8266WebServer.h>
        #include <strings_en.h>
        #include <ESP8266mDNS.h>
        
        #include <ESP_EEPROM.h>
        #include "Arduino.h"
        //wifi,email, internet
        #include <EMailSender.h>
        //#include <ArduinoJson.h>
        //
        #include "mainPage.h"
        #include <Wire.h>
        #include <Beastdevices_INA3221.h>
        
        //Wifi setting
        
        #define AP_SSID "custom" // custom filter roller SSID
        #define AP_PWD  "1234567890"      // custom passowrd
        
        ESP8266WebServer server(80);
        WiFiManager wm;                 
        
        char email_to[32] = "";
        char email_login[32] = "";   //these are stored in EEPROM after input from web
        char email_password[32] = "";
        
        EMailSender emailSend(email_login, email_password);    
        void setup() {
         
          EEPROM.begin (120);
         
          
        //Wifi setup
          WiFi.mode(WIFI_STA); //set wifi toSTA;default STA+AP
          wm.setConfigPortalBlocking(false);
          wm.setConfigPortalTimeout(60);
        
            //automatically connect using saved credentials if they exist
            //If connection fails it starts an access point with the specified name
          // check if stored connection info is correct
          if (wm.autoConnect(AP_SSID, AP_PWD)) {
            Serial.println("\Connection data is correct!");
             Serial.println(WiFi.localIP());
          } else {
            Serial.println("\Connection data is incorrect…");
          }
        
        //wm.startConfigPortal();
        
         
        EMailSender::EMailMessage message;
            message.subject = "Float sensor may be stuck";
            message.message = "Float sensor may be stuck, motor disabled until float sensor is resetted";
            EMailSender::Response resp = emailSend.send(email_to, message);
            Serial.println("Sending status: ");
            Serial.println(resp.status);
            Serial.println(resp.code);
            Serial.println(resp.desc);
        };
      • #18979
        Renzo Mischianti
        Keymaster

          Hi Claudacate,
          can you send me the Serial output, the name of the provider and I must verify If WiFiManager set the DNS also or like some time ago It has a problem with the setting of DNS.

          How to manage dynamic WIFI configuration on esp8266 or esp32

          Bye Renzo

        • #18986
          claudacate
          Participant

            Thank you for replying. I think WifiManager set the DNS, but I am not sure. I will read up on the link you provided. Here is the serial output:

            wm:[1] AutoConnect: SUCCESS
            *wm:[1] STA IP Address: 192.168.88.217
            Connection data is correct!
            192.168.88.217
            HTTP server started
            ONLY ONE RECIPIENTmiltiple destination and attachments
            Insecure client:0
            smtp.gmail.com
            465
            Sending status:
            0
            2
            Could not connect to mail server

          • #18987
            claudacate
            Participant

              I read your link. I did not use your version of the Wi-Fimanager library. I think that is the problem. Do I just swap the whole library? Or should I change a file? Thanks

            • #18989
              Renzo Mischianti
              Keymaster

                Hi Claudcate,
                wait, first try to send an email with standard WiFi management.
                Give me a feedback.
                Bye Renzo

              • #19049
                claudacate
                Participant

                  I used the standard WiFi management and EMailSenderEsp8266GMailTest.ino and still I can’t get a connection. Here is the serial monitor output. Hopefully you can make sense of it. Thank you

                  Connecting to Tea King
                  …………….
                  Connection: ESTABLISHED
                  Got IP address: 192.168.88.217
                  ONLY ONE RECIPIENTmiltiple destination and attachments
                  Insecure client:0
                  smtp.gmail.com
                  465
                  Sending status:
                  0
                  2
                  Could not connect to mail server

                • #19054
                  Renzo Mischianti
                  Keymaster

                    Hi, claudcate,
                    try to do a telnet to
                    smtp.gmail.com 465
                    so you can check if you have a firewall that blocks your outgoing connection.

                    Bye Renzo

                  • #19065
                    claudacate
                    Participant

                      Hi, I have tried the following. 1. I have used my iPhone’s hotspot as a wifi source since my home network may have possible network problem such as firewall. Result: same error as before. 2. I have spoofed my email address to see if I get different error code. Result: same error as before. 3. telnet to smtp.gmail.com 25 result: telnet smtp.gmail.com 25
                      Trying 142.251.8.108…
                      Connected to smtp.gmail.com.
                      Escape character is ‘^]’.
                      220 smtp.gmail.com ESMTP z11-20020a17090a1fcb00b001bc58804974sm5738172pjz.27 – gsmtp

                      4. telnet to smtp.gmail.com 465. Result: telnet smtp.gmail.com 465
                      Trying 142.251.8.108…
                      Connected to smtp.gmail.com.
                      Escape character is ‘^]’.
                      Connection closed by foreign host.

                    • #19066
                      claudacate
                      Participant

                        However, upon further research. It seems port 465 requires SSL to connect. I used openssl and below is result. I think I am missing SSL in the code
                        openssl s_client -connect smtp.gmail.com:465
                        CONNECTED(00000005)
                        depth=3 C = BE, O = GlobalSign nv-sa, OU = Root CA, CN = GlobalSign Root CA
                        verify return:1
                        depth=2 C = US, O = Google Trust Services LLC, CN = GTS Root R1
                        verify return:1
                        depth=1 C = US, O = Google Trust Services LLC, CN = GTS CA 1C3
                        verify return:1
                        depth=0 CN = smtp.gmail.com
                        verify return:1

                        Here is the EMailSenderEsp8266GMailTest.ino I used

                        
                        #include "Arduino.h"
                        #include <EMailSender.h>
                        #include <ESP8266WiFi.h>
                        
                        const char *ssid = "hotspot";
                        const char *password = "12345678";
                        
                        uint8_t connection_state = 0;
                        uint16_t reconnect_interval = 10000;
                        
                        EMailSender emailSend("sendemailfrom@gmail.com", "passworddfdf");
                        
                        uint8_t WiFiConnect(const char *nSSID = nullptr,
                        		const char *nPassword = nullptr) {
                        	static uint16_t attempt = 0;
                        	Serial.print("Connecting to ");
                        	if (nSSID) {
                        		WiFi.begin(nSSID, nPassword);
                        		Serial.println(nSSID);
                        	}
                        
                        	uint8_t i = 0;
                        	while (WiFi.status() != WL_CONNECTED && i++ < 50) {
                        		delay(200);
                        		Serial.print(".");
                        	}
                        	++attempt;
                        	Serial.println("");
                        	if (i == 51) {
                        		Serial.print("Connection: TIMEOUT on attempt: ");
                        		Serial.println(attempt);
                        		if (attempt % 2 == 0)
                        			Serial.println(
                        					"Check if access point available or SSID and Password\r\n");
                        		return false;
                        	}
                        	Serial.println("Connection: ESTABLISHED");
                        	Serial.print("Got IP address: ");
                        	Serial.println(WiFi.localIP());
                        	return true;
                        }
                        
                        void Awaits() {
                        	uint32_t ts = millis();
                        	while (!connection_state) {
                        		delay(50);
                        		if (millis() > (ts + reconnect_interval) && !connection_state) {
                        			connection_state = WiFiConnect();
                        			ts = millis();
                        		}
                        	}
                        }
                        
                        void setup() {
                        	Serial.begin(115200);
                        
                        	connection_state = WiFiConnect(ssid, password);
                        	if (!connection_state) // if not connected to WIFI
                        		Awaits(); // constantly trying to connect
                        
                        	EMailSender::EMailMessage message;
                        	message.subject = "Soggetto";
                        	message.message = "Ciao come stai<br>io bene.<br>www.mischianti.org";
                        
                        	EMailSender::Response resp = emailSend.send("hcubeh@yahoo.com", message);
                        
                        	Serial.println("Sending status: ");
                        
                        	Serial.println(resp.status);
                        	Serial.println(resp.code);
                        	Serial.println(resp.desc);
                        }
                        
                        void loop() {
                        
                        }
                        
                        
                      • #19072
                        claudacate
                        Participant

                          I found my mistake! When I read the tutorial I followed the instruction for esp8266 core 2.4.2 and changed the emailsenderkey.h. I feel silly. Sorry I took up so much your time Renzo. Thanks again for your help.

                        • #19073
                          claudacate
                          Participant

                            I know this is not part of your library, but was wondering if you can help. I recorded my email and password in EEProm, but when the value are used to login I can’t login. However, if I placed the value like char email_login[32]= “claudacate@gmail.com” I’d have no problem. I serial print out the value they looked identical. Here is what I used to update my email and password. Thank you

                            
                            		char email_login[32];
                            		char email_password[32];
                            
                            		void loademail() {
                            			EEPROM.get(0, email_to);
                            			EEPROM.get(0 + sizeof(email_to), email_login);
                            			EEPROM.get(0 + sizeof(email_to) + sizeof(email_login),
                            					email_password);
                            			char ok[3];
                            			EEPROM.get(
                            					0 + sizeof(email_to) + sizeof(email_login)
                            							+ sizeof(email_password), ok);
                            			if (String(ok) != String(“OK”)) {
                            				email_to[0] = 0;
                            				email_login[0] = 0;
                            				email_password[0] = 0;
                            			}
                            		}
                            
                            		void saveemail() {
                            			EEPROM.put(0, email_to);
                            			EEPROM.put(0 + sizeof(email_to), email_login);
                            			EEPROM.put(0 + sizeof(email_to) + sizeof(email_login),
                            					email_password);
                            			char ok[3] = “OK”;
                            			EEPROM.put(
                            					0 + sizeof(email_to) + sizeof(email_login)
                            							+ sizeof(email_password), ok);
                            			EEPROM.commit();
                            
                            		}
                            
                            
                            • #19076
                              Renzo Mischianti
                              Keymaster

                                Hi Claudecate,
                                probably there are spaces added to the end of string to grow all byte array length

                                Try something similar to

                                
                                int writeStringToEEPROM(int addrOffset, const String &strToWrite)
                                {
                                  byte len = strToWrite.length();
                                  EEPROM.write(addrOffset, len);
                                  for (int i = 0; i < len; i++)
                                  {
                                    EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
                                  }
                                  return addrOffset + 1 + len;
                                }
                                int readStringFromEEPROM(int addrOffset, String *strToRead)
                                {
                                  int newStrLen = EEPROM.read(addrOffset);
                                  char data[newStrLen + 1];
                                  for (int i = 0; i < newStrLen; i++)
                                  {
                                    data[i] = EEPROM.read(addrOffset + 1 + i);
                                  }
                                  data[newStrLen] = '\0'; 
                                  *strToRead = String(data);
                                  return addrOffset + 1 + newStrLen;
                                }
                                
                                

                                where the addrOffset is the start of eeprom and you can use String variable

                                
                                String user = "ciccio";
                                String password = "passwd";
                                writeStringToEEPROM(0, user);
                                writeStringToEEPROM(32, password);
                                

                                and to read

                                
                                String user;
                                String password;
                                readStringFromEEPROM(0, &user);
                                readStringFromEEPROM(32, &password);
                                

                                Bye Renzo

                            • #19074
                              Renzo Mischianti
                              Keymaster

                                Hi Claudacate,
                                I do a lot of tests (with core 2.7 and 3.0), but no problem with WeMos D1.

                                Do you have to change some settings on EMailSenderKey?

                                Bye Renzo

                              • #19077
                                claudacate
                                Participant

                                  Thank you for your help. I really appreciate it.

                                  However,when I change the variable from char to String, I’d get error message

                                  testemail:14:50: error: no matching function for call to ‘EMailSender::EMailSender(String&, String&)’
                                  14 | EMailSender emailSend(email_login, email_password);

                                • #19082
                                  Renzo Mischianti
                                  Keymaster

                                    Try

                                    
                                    EMailSender emailSend(email_login.c_str(), email_password.c_str());
                                    

                                    Bye Renzo

                                Viewing 13 reply threads
                                • You must be logged in to reply to this topic.
                                Exit mobile version