Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Needs more than 40 seconds to send. Any ideas why? #26

Closed
BenSeventy9 opened this issue Jun 21, 2022 · 20 comments
Closed

Needs more than 40 seconds to send. Any ideas why? #26

BenSeventy9 opened this issue Jun 21, 2022 · 20 comments

Comments

@BenSeventy9
Copy link

Serial Monitor:

...............................
Connection: ESTABLISHED
Got IP address: 192.168.0.23
Sending status: 
1
0
Message sent!
Time for sending: 42.47 seconds

My code:
Credentials and mail replaced by "***"

/*
   EMailSender library for Arduino, esp8266 and esp32
   Simple esp8266 Gmail send example

   https://www.mischianti.org/category/my-libraries/emailsender-send-email-with-attachments/

*/

#include "Arduino.h"
#include <EMailSender.h>
#include <ESP8266WiFi.h>

const char* ssid = "***";
const char* password = "***";

uint8_t connection_state = 0;
uint16_t reconnect_interval = 2000;

EMailSender emailSend("***", "***", "***", "***", "mail.gmx.net", 465);

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

  emailSend.setPublicIpDescriptor("WemosSensor");
  //emailSend.setIsSecure(true);
  emailSend.setEHLOCommand(true);

  EMailSender::EMailMessage message;
  message.mime = MIME_TEXT_PLAIN;
  message.subject = "Soggetto";
  message.message = "Ciao come stai<br>io bene.<br>www.mischianti.org";

  uint32_t start = millis();

  EMailSender::Response resp = emailSend.send("***", message);

  Serial.println("Sending status: ");

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

  uint32_t end = millis();

  Serial.print(F("Time for sending: "));
  Serial.print((double)(end - start) / 1000, 2);
  Serial.println(F(" seconds"));
}

void loop() {}
@xreef
Copy link
Owner

xreef commented Jun 28, 2022

It's very strange, but I think you have some network delay, try to put the router and device close.
Bye Renzo

@salasidis
Copy link

Maybe the delay is on the line
if (this->useEHLO == true) { for (int i = 0; i<=11; i++) awaitSMTPResponse(client); }

If the server sends fewer outputs, it still waits for 6. I will change the code on my end to stop the loop when there is a call with a delay occuring, and see if it helps on my end.

@xreef
Copy link
Owner

xreef commented Jun 28, 2022

Hi @salasidis,
If there is a timeout the email is not sent with a timeout error.
Bye Renzo

@xreef xreef closed this as completed Jun 30, 2022
@BenSeventy9
Copy link
Author

When I use a Gmail address it sends within 2s. It isn't caused by network delay. I send it only once when battery is low so I can live with those 40 seconds.

@xreef xreef reopened this Jul 1, 2022
@xreef
Copy link
Owner

xreef commented Jul 1, 2022

Hi Ben,
can you enable the DEBUG and send me the result.

#define EMAIL_SENDER_DEBUG

https://github.com/xreef/EMailSender/blob/master/EMailSenderKey.h#L43

Bye Renzo

@BenSeventy9
Copy link
Author

`...............................
Connection: ESTABLISHED
Got IP address: 192.168.1.80
ONLY ONE RECIPIENTmiltiple destination and attachments
Insecure client:0
MFLN supported: yes
mail.gmx.net
465
220 gmx.net (mrgmx005) Nemesis ESMTP Service ready

EHLO WemosSensor:
250-gmx.net Hello WemosSensor: [84.226.107.152]

250-8BITMIME

250-AUTH LOGIN PLAIN

250 SIZE 69920427

AUTH LOGIN:
334 VXNlcm5hbWU6

Encoding
hidden@gmx.net
18
hidden
Encoding
hidden@gmx.net
18
334 UGFzc3dvcmQ6

Encoding
hidden
24
hidden
Encoding
hidden
24
235 Authentication succeeded

MAIL FROM: *hidden*@gmx.net
250 Requested mail action okay, completed

RCPT TO: *hidden*@gmx.ch
250 OK

DATA:
354 Start mail input; end with .

Message end
250 Requested mail action okay, completed: id=1MLzBj-1oP2pU2fyb-00Hv5c

221 gmx.net Service closing transmission channel

Sending status:
1
0
Message sent!
Time for sending: 43.00 seconds
`

@xreef
Copy link
Owner

xreef commented Jul 1, 2022

There is a point that needs more time?

@BenSeventy9
Copy link
Author

After "250 SIZE 69920427" starts the delay until "AUTH LOGIN:". Hope that helps

@BenSeventy9
Copy link
Author

BenSeventy9 commented Jul 1, 2022

I played a bit around and changed the loop to 2:
if (this->useEHLO == true) { for (int i = 0; i<=2; i++) awaitSMTPResponse(client); }
Output:
250 Requested mail action okay, completed: id=1MWzfv-1o9ceG3VCj-00XI1S

221 gmx.net Service closing transmission channel

Sending status:
1
0
Message sent!
Time for sending: 2.40 seconds

Looks like salasidis was right!

@salasidis
Copy link

salasidis commented Jul 1, 2022 via email

@BenSeventy9
Copy link
Author

I added a test code on my end, I time the rime between successive 250-××× If the time of any one is greater than a few seconds, I presume there are no more left, and I proceed to an auth login This way the delay happens just once, and is independent of the number of 250×× replies from the server. Otherwise there wad a risk of interviewing the 250××× responses with the auth login going out, or too long delay, if exoect more 250s than there actually are.

Do you have some code snipped for me?

@salasidis
Copy link

salasidis commented Jul 1, 2022 via email

@BenSeventy9
Copy link
Author

Thank you. I also had something similar in mind.

@BenSeventy9
Copy link
Author

BenSeventy9 commented Jul 2, 2022

I simplified it a bit:
if (this->useEHLO == true) { for (int i = 0; i<=6; i++) { response = awaitSMTPResponse(client, "250", "EHLO error", 2500); if (!response.status) { DEBUG_PRINTLN(response.desc); break; } } }
Got it down to 5 seconds.

@salasidis
Copy link

salasidis commented Jul 2, 2022 via email

@xreef
Copy link
Owner

xreef commented Jul 4, 2022

Hi all,
I do some changes to "regulate" the size of the response reader.
I hope to release it in the next week.
Bye Renzo

@xreef
Copy link
Owner

xreef commented Jul 6, 2022

Hi,
try now, if you find some other bugs write here I try to fix it.
Bye Renzo

@BenSeventy9
Copy link
Author

BenSeventy9 commented Jul 11, 2022

Hi, try now, if you find some other bugs write here I try to fix it. Bye Renzo

I think you made a little mistake. Should't it be like this?
if (this->additionalResponseLineOnHELO > 0){ for (int i = 0; i<=this->additionalResponseLineOnHELO; i++) { response = awaitSMTPResponse(client, "250", "EHLO error", 2500); if (!response.status && response.code == F("1")) { response.desc = F("Timeout! Reduce the HELO response line!"); client.flush(); // needs to be inside here client.stop(); // needs to be inside here return response; // needs to be inside here } } }
This works fine for me. But anyway thanks for implementing this.

@xreef
Copy link
Owner

xreef commented Jul 19, 2022

Azz.. yes, there is a copy-paste error @BenSeventy9 .
I release this evening the little fix.
Thanks again Renzo

@xreef
Copy link
Owner

xreef commented Jul 20, 2022

I hope It's the last fix.
Bye Renzo

@xreef xreef closed this as completed Jul 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants