Email with sd files

Tagged: 

Viewing 13 reply threads
  • Author
    Posts
    • #23502
      fr
      Participant

        Hello,

        I managed to write an email with files located in Spiffs thanks to your work: thank you

        I would like to be able to send an email with a file or files located in an SD card and I can’t find any example.
        I work with an ESP32-S2 wemos S2 mini. and sd shield for wemos https://fr.aliexpress.com/item/1005004384471964.html.
        I know how to handle the files saved in the SD but I don’t know how to attach the file from the SD card in an email.

        Can anyone help me? please

        /*  esp32-S2  and shield micro sd wemos    nov 2022
         *   
         *   PB avec SD ?  http://mischianti.org/how-to-use-sd-card-with-esp8266-and-arduino/
         *   
         *   
         * SD : SCK 7, MISO 9, MOSI 11, SS 12, 3.3v et gnd 
         */
         
        
        #include <Arduino.h>
        #include <WiFi.h>
        #include <EMailSender.h>
        #include "SPI.h"
        #include "SD.h"
        
        const char* ssid = "xxxxxxxxxx";                         
        const char* password = "xxxxxxxxxx";    
        
        const char* author_email = "zzzzzzzzzz@gmail.com";
        const char* passGmail = "zzzzzzzzzzzzzz";
        const char* dest_email = "vvvvvvvvv@gmail.com";
        
        #define SDSPEED 27000000
        #define SCK 7
        #define MISO 9
        #define MOSI 11
        #define SS 12
        #define BUFF_MAX 100 
        
        File root;
        SPIClass spiSD(FSPI);                        
        EMailSender emailSend("eeeeee@gmail.com", "eeeeeeeeeeeee"); 
        
        void affichSD(int a)  
        {  if ( a == 1)
            {    Serial.println("Pb avec SD");
                 while (1)
                  {   Serial.println("no SD card");    }
           if ( a==2)
            {    Serial.println("SD OK");
                 uint8_t cardType = SD.cardType();
                 if(cardType == CARD_NONE)
                  {   while (1)
                       {   Serial.println("No SD");
                           return;    }
                 Serial.println("SD  : ");
                 if(cardType == CARD_MMC)
                  {   Serial.println("MMC");  }
                  else if(cardType == CARD_SD)
                  {   Serial.println("SDSC"); }
                  else if(cardType == CARD_SDHC)
                  {   Serial.println("SDHC"); }
                  else
                  {   Serial.println("type inconnu");
                       }  }  }  }   }
        
        void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
            Serial.printf("Listing directory: %s\r\n", dirname);
            File root = fs.open(dirname);
            if(!root){
                Serial.println("- failed to open directory");
                return;    }
            if(!root.isDirectory()){
                Serial.println(" - not a directory");
                return;   }
            File file = root.openNextFile();
            while(file){
                if(file.isDirectory()){
                    Serial.print("  DIR : ");
                    Serial.println(file.name());
                    if(levels){
                        listDir(fs, file.path(), levels -1);            }        } 
                else {
                    Serial.print("  FILE: ");
                    Serial.print(file.name());
                    Serial.print("\tSIZE: ");
                    Serial.println(file.size());        }
                file = root.openNextFile();    }  }
        
        void setup(){
          delay(5000);
          Serial.begin(115200);
          Serial.println("========================");
          Serial.println(__FILE__);
          Serial.print("Connecting to AP");
          pinMode(SS, OUTPUT);  
         
          WiFi.begin(ssid, password);
          while (WiFi.status() != WL_CONNECTED)  {
              Serial.print(".");
              delay(200);  }
          Serial.println("");
          Serial.println("WiFi connected.");
          Serial.println("IP address: ");
          Serial.println(WiFi.localIP());
          Serial.println();
          spiSD.begin(SCK,MISO,MOSI,SS); 
          if(!SD.begin(SS, spiSD, SDSPEED))
           {   affichSD(1);    
               return;   }
            else  { affichSD(2);  } 
          listDir(SD, "/", 0);
          EMailSender::EMailMessage message;
          message.subject = "ESP32-S2";
          message.message = "Super";
          
          EMailSender::FileDescriptior fileDescriptor[1];
        
          fileDescriptor[0].filename = F("data.txt");
          fileDescriptor[0].url = F("/data.txt");
          fileDescriptor[0].mime = MIME_TEXT_PLAIN;
          fileDescriptor[0].encode64 = false;
          fileDescriptor[0].storageType = EMailSender::EMAIL_STORAGE_TYPE_SD;
        
          EMailSender::Attachments attachs = {1, fileDescriptor};
          EMailSender::Response resp = emailSend.send("dddddd@gmail.com", message, attachs);
          Serial.print("Sending status: "); Serial.println(resp.status);
          Serial.print("resp.code : ");  Serial.println(resp.code);
          Serial.print("resp.desc : ");  Serial.println(resp.desc);    }
          
          
        void loop()
        {
        }

        And the answer is
        Sending status: 0
        resp.code : 404
        resp.desc : Error opening attachments file /data.txt

      • #23509
        Renzo Mischianti
        Keymaster

          Hi fr,
          the error says that there isn’t a file named data.txt on SD.
          But the sending code is correct.
          You must also check the EMailSenderKey.h for the esp32 config.
          bye Renzo

        • #23514
          fr
          Participant

            Hello,

            I know how to read the fact that the file is not found, yet it is recognized when ordering. listDir(SD, “/”, 0);
            Send me back to an EMailSenderKey.h configuration?
            This is the updated configuration to version 3.0.6 in arduino 1.8.19.
            Intervening in it is well beyond my skills.
            A hypothesis: for my reading of SD I defined
            spiSD.begin(SCK,MISO,MOSI,SS);
            if(!SD.begin(SS, spiSD, SDSPEED))

            In your library wouldn’t you change these values?
            Which explains=would be that after having seen the file it becomes untraceable to attach it to the email.
            Note also that it is an ESP32-S2 and that sometimes there are things valid for an ESP32 that no longer work with esp32-s2
            Have a good evening
            fr

          • #23515
            Renzo Mischianti
            Keymaster

              Hi fr,
              mmmm… as you can see in these lines I do begin of SD class only if you don’t do It.
              So I don’t initialize the SD class, but I use your initialization.
              Bye Renzo

            • #23541
              fr
              Participant

                Hello,
                Sorry I do not understand.
                What is the solution so that I can attach a file from SD ?
                fr

              • #23563
                fr
                Participant

                  Hello Renzo,

                  Can you help me please
                  fr

                • #23565
                  Renzo Mischianti
                  Keymaster

                    Hi fr,
                    I can’t replicate your issue, try to uncomment debug define, and post the result here.
                    Bye RM

                  • #23566
                    fr
                    Participant

                      Hi,

                      in emailsender.cpp line 868 replace INTERNAL by EXTERNAL and it is ok
                      @+
                      fr

                    • #23587
                      Renzo Mischianti
                      Keymaster

                        Hi fr,
                        It’s strange, It’s wrong, the internal storage was the SPIFF and with this operation you are going to read from SD with SPIFF configuration.

                        I’m going to check soon.
                        Bye Renzo

                      • #23589
                        fr
                        Participant

                          Hello,
                          I noted that the error message occurred from line 888, so I looked at which line offered the test and it’s 868.
                          My subject being an attachment with SD I replaced INTERNAL with EXTERNAL and everything worked.
                          As for why line 868 is being tested, I didn’t look any further… It’s your code 😉
                          fr

                        • #23564
                          fr
                          Participant

                            Hello,
                            After looking at where the error message was programmed, I saw
                            https://github.com/xreef/EMailSender/blob/master/EMailSender.cpp#L866,L870 that there was an EMAIL_FILE myFile = INTERNAL_STORAGE_CLASS which did not match SD which is external.
                            So I set EMAIL_FILE myFile = EXTERNAL_STORAGE_CLASS.open(attachments.fileDescriptor[i].url, EMAIL_FILE_READ);
                            And everything works.
                            I don’t know if this will disturb the rest of the library, but as a creator you must know it.
                            Cordially
                            fr

                          • #23876
                            Renzo Mischianti
                            Keymaster

                              hi,
                              check the library version.

                            • #23904
                              Renzo Mischianti
                              Keymaster

                                Hi fr,
                                you are right, I finally find the bug.
                                Thanks for your patience, I release the version 3.0.9 soon.
                                Thank again Renzo

                              • #28815
                                Mariano B
                                Participant

                                  (*)

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