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
74 stars 26 forks source link

On nano 33 IOT, EmailSender Supports Only Encrypted Mail #31

Closed lerman06470 closed 2 years ago

lerman06470 commented 2 years ago

EMailSender ignores the isSecure flag on this platform and probably others.

xreef commented 2 years ago

Hi, isSecure is supported only by esp8266 for core < 2.4.2. Now all platform automatically manage that flag. Bye Renzo

lerman06470 commented 2 years ago

The flag isn't managed in the sense that it doesn't work if I try to connect to port 25 which is not secure. If your statement is meant to say that it isn't an issue and that EMailServer is intended to support only encrypted mail, I can understand that. The documentation should say that in bold print.

xreef commented 2 years ago

Please post your code and explain your test.

The meaning of setInsecure in the espressif environment It isn't the encryption of the connection but the validation of the chain. You can find something here https://www.mischianti.org/2022/01/09/esp8266-self-ota-update-in-https-ssl-tls-with-trusted-self-signed-certificate-3/.

Bye Renzo

lerman06470 commented 2 years ago

My test code is copied from J-oseph with modifications to use my account on sendgrid. This code fails.

/*
 * MADE BY J-oseph ON GITHUB
 * MOST OF THIS CODE IS NOT MY OWN
 * PLEASE CHECK OUT MY VIDEO https://youtu.be/GFDTMFBPCn4 TO SEE HOW TO SET EVERYTHING UP
 * AND TO FIND LINKS TO OTHER PEOPLE'S CODE
 * THIS CODE WORKS WITH ARDUINO NANO 33 IOT
 * USE WITH OTHER ARDUINOS AT YOUR OWN RISK ;)

 * Modified by Kenneth Lerman (lerman@se-ltd.com)
*/
#include <WiFiNINA.h>
#include <EMailSender.h>
#include "arduino_secrets.h"

char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;   
char eMailPass[] = SECRET_APIKEY;

char smtpServer[] = "smtp.sendgrid.net";
int smtpPort = 25;

char eMailUser[] = "apikey"; // account must have correct settings
char eMailRecipient[] = "lerman@se-ltd.com";

const String TEXT = "T E X T"; // main body text of the email
const char SUBJ[] = "S U B J"; // subject of the email

WiFiClient client; // sets up the wifi client

void setup(){  
  Serial.begin(9600);

  // all we do is connect to wifi and send and email
  setupWifi();

  sendEmail();
}

void loop() {}

void setupWifi(){
  // connects to the wifi
  Serial.print("Connecting to ");
  Serial.print(ssid);
  Serial.print("...");

  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    // will stay in a loop if it doesnt connect
    delay(2000);
    Serial.print(".");
  }
  Serial.println("Connected to WiFi");
  Serial.print("Local IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("");              
  Serial.println("");
}

void sendEmail(){
    // sends an email
    // the name it will display who the email is from
    char senderName[] = "Sump Pump Controller";

    // the actual stuff that sends the email:
    EMailSender emailSend(eMailUser, eMailPass, "lerman+sendgrid@se-ltd.com",
        senderName, smtpServer, smtpPort);

    EMailSender::EMailMessage msg;
    EMailSender::Response resp;
    Serial.println("Sending email...");
    msg.subject = SUBJ;
    msg.message = TEXT;

    resp = emailSend.send(eMailRecipient, msg);

    // all of this just prints the info
    Serial.println("Sending status: ");
    Serial.print(resp.status);
    Serial.println(resp.code);
    Serial.println(resp.desc);
    Serial.println("");
    Serial.print("FROM: ");
    Serial.println(eMailUser);
    Serial.print("TO: ");
    Serial.println(eMailRecipient);
    Serial.print("SUBJECT: ");
    Serial.println(SUBJ);
    Serial.print("DATA:");
    Serial.println(TEXT);
    Serial.println("");
}

It will work if I insert into EMailSender.h around line 217:

#define EMAIL_NETWORK_CLASS WiFiSSLClient
//!!!KL
#define KL
#ifndef KL
#define EMAIL_NETWORK_SSL_CLASS WiFiSSLClient
#define EMAIL_NETWORK_SERVER_CLASS WiFiServer
#else
#define EMAIL_NETWORK_SSL_CLASS WiFiClient
#define EMAIL_NETWORK_SERVER_CLASS WiFiServer
#endif

That is, if I use WiFiClient instead of WiFiSSLClient. That's because WiFiSSLClient is always using SSL.

Thanks for looking at this.

I should comment that at this point, I'm up and running using the above change to the library, so there is no urgency for me.

Regards,

Ken

xreef commented 2 years ago

Hi, I add a define that allows disabling SSL clients for the particular management of the SAMD board. Give me a feedback about that. Bye Renzo

lerman06470 commented 2 years ago

That's just another patch that's probably better than mine.

If I were writing a requirements document for this program, one of the requirements would be:

At run time, the user shall be able to set whether SSL is used for each message sent.

I should be able to write and build a program where the user sets what server is used, what port is used, and whether it is encrypted or not. The default might be that some ports (such as port 25) are not encrypted and other ports are encrypted.