xreef / EMailSender

Arduino, esp32, Esp8266 EMailSender with Arduino IDE, simple library to send email via smtp with attachments.
https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/
MIT License
75 stars 28 forks source link

Can't log into Gmail with EMailSenderESP32GMailTest sketch #12

Closed maulepilot closed 3 years ago

maulepilot commented 3 years ago

I haven't been able to log into Gmail using your EMailSenderESP32GMailTest sketch with my Arduino MKR WiFi 1010 and the Arduino.h, WiFiNINA.h and EMailSender.h libraries. I get the following messages on the serial monitor:

Connection: ESTABLISHED Got IP address: 192.168.1.42 Sending status: 0 1 SMTP Response TIMEOUT!

I've tried using the two parameter emailSend constructor with my gmail userid and password as well as the longer constructor with my gmail userid, password, mail from userid, smtp.gmail.com as the outgoing server and port 587, as well as port 465.

I went through parts 1 and 2 of your tutorial but it hasn't helped. I'm wondering if your EMailSender.h library isn't compatible with the MKR WiFi 1010.

I logged out of Google completely and logged back in with a browser to make sure my Gmail account was working and it's fine. There are no sent messages from the Arduino.

Any advice? Thanks.

xreef commented 3 years ago

Hi manuelpilot,

Give me a feedback Bye Renzo

maulepilot commented 3 years ago

Yes, lesssecureapp is activated. Yes, I used my own gmail account as the FromAddress. I don't think there's any kind of network limitation or firewall problem. I've been sending POST messages to a PHP script that I wrote on a GoDaddy shared HTTPS server without any problems.

xreef commented 3 years ago

It's very strange, I try to send an email now from an esp32 DEV KIT v1 and work correctly. Please post your code, and in the file EMailSenderKey.h uncomment this line //#define EMAIL_SENDER_DEBUG and send the result (cover the login and passwd). Thanks Renzo

maulepilot commented 3 years ago

Here's your slightly modified sketch. I have to use #include to compile without errors. That might be the problem but I can't tell for sure. All the private information in caps is defined in the #include "arduino_secrets_gmail.h" file.

/*

include "Arduino.h"

include

include

include "arduino_secrets_gmail.h"

const char ssid = SECRET_SSID; const char password = SECRET_PASS;

uint8_t connection_state = 0; uint16_t reconnect_interval = 10000;

EMailSender emailSend(SECRET_GMAIL_USERNAME, SECRET_GMAIL_PASSWORD);

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(SECRET_GMAIL_RECIPIENT, message);

Serial.println("Sending status: ");

Serial.println(resp.status);
Serial.println(resp.code);
Serial.println(resp.desc);

}

void loop() {

}

Here's the serial monitor output with the #define EMAIL_SENDER_DEBUG in EmailSender.h library file uncommented. I didn't include the SSID network name at the top for privacy's sake. I'm wondering if the fact that it's an insecure client is what is causing the problem. I tried temporarily changing #include to #include in your EmailSender.h file but it didn't help. I've had to change WiFi.h to WiFiNINA.h in all of the sketch examples in order to compile them without errors on the MKR WiFi 1010.

Connection: ESTABLISHED Got IP address: 192.168.1.42 ONLY ONE RECIPIENTInsecure client:0 Sending status: 0 1 SMTP Response TIMEOUT!

xreef commented 3 years ago

Hi Mauelepilot, to send email with MKR WiFi 1010 and WiFININA you must do some additional work. I find this https://gitlab.com/gratefulfrog/arduino-wifinina-gmail-smtp seems you must enable 2 factor security and generate secret. Give me a feedback if this can solve the problem Bye Renzo

maulepilot commented 3 years ago

Thanks, Renzo. I was successful using the arduino-wifinina-gmail-smtp library and modified WiFiSSLClient_gmail_smtp sketch by Grateful Frog above. The process is complex. In Google Account Manager I created a separate gmail account, set up 2 factor authentication and created an app password. The app password must be used when logging into the gmail account. In the modified sketch I removed all private data from the sketch and put it in the arduino_secrets.h file. Attached are the sketch and the output from the serial monitor. gmailoutput-1.txt

gmailoutput.txt

xreef commented 3 years ago

Perfect! In the future I try to integrate the process in my library, but now I'm happy that you had found the solution. Bye Renzo

I think I only add/change this part

  WiFiSSLClient client;

  if (client.connectSSL(server, port)==1){ 

and all work.

xreef commented 3 years ago

Hi Manuelpilot, if you can add the sketch also for people that can need It. Thanks Renzo

maulepilot commented 3 years ago

Attached are the Arduino sketch and the serial monitor output (minus personal data). wifisslclientgmail_sketch.txt wifisslclientgmail_output.txt

xreef commented 3 years ago

Thanks Maulepilot