Site icon Renzo Mischianti

Email with sd files

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
Exit mobile version