Envoyer email protocole smtp

Bonjour j'ai besoin d'aide enfaite je me suis fixe comme projet de pouvoir avec un détecteur de mouvement envoyer un email avec une photo prise en même temps a l'aide donc d'un esp32-cam.Donc j'ai fais un petit début de code j'arrive a envoyer un email et une image mais donc je connais déjà le code je l'ai vu sur un site mais je n'arrive a connaitre le nom de se code de ses donne qui représente l'image et je sais pas comment faire pour convertir ma photo que je prend en code(dont je connais pas le nom)

#include <ESP_Mail_Client.h>
#include <WiFi.h>
#include <esp_camera.h>
#include <SPIFFS.h>
#include <Base64.h>
char*ssid="xxxxxxx";
char*passeword="xxxxxxxxxxxxxxxxx";
SMTPSession smtp;

#define IMAGE_PATH "/image.jpg"

void setup() {
  
  WiFi.begin(ssid,passeword);
  Serial.begin(115200);
  while(WiFi.status()!=WL_CONNECTED){
    Serial.println("Tentative");
    delay(200);
  }
  bool variable =InitialiserCamera();
  if (variable==false){
   Serial.println("Camera non initialiser");
 }
  Serial.println("Camera ok");
  Serial.println("OK WIFI");
 
  if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");

}
captureSave_image();
 Email();
 Serial.println("OK");
}

void loop() {
  

}

void Email(){
  ESP_Mail_Session email;
  const char*Adresse_email_emission="testesp32cam2@gmail.com";
  const char*Motdepasse="orxkbiougwrrqlgp";
  const char*Adresse_server="smtp.gmail.com";
  int Port=465;
  const char*Adresse_email_destination="nadgeoray@gmail.com";
email.server.host_name =Adresse_server;
  email.server.port = Port;
  email.login.email = Adresse_email_emission;
  email.login.password = Motdepasse;
  email.login.user_domain ="mydomain.net";
  
  SMTP_Message message;
 
  message.sender.name = "ESP Mail";
  message.sender.email = "testesp32cam2@gmail.com";
  message.subject = "Test sending plain text Email";
  message.addRecipient("Someone", "nadgeoray@gmail.com");

  String textMsg = "This is simple plain text message";
  message.text.content = textMsg.c_str();

   
   const char *greenImg ="iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAoUlEQVR42u"
                         "3RAQ0AMAgAoJviyWxtAtNYwzmoQGT/eqwRQoQgRAhChCBECEKECBGCECEI"
                         "EYIQIQgRghCECEGIEIQIQYgQhCBECEKEIEQIQoQgBCFCECIEIUIQIgQhCB"
                         "GCECEIEYIQIQhBiBCECEGIEIQIQQhChCBECEKEIEQIQhAiBCFCECIEIUIQ"
                         "ghAhCBGCECEIEYIQIUKEIEQIQoQg5LoBBaDPbQYiMoMAAAAASUVORK5CYII=";
   

 SMTP_Attachment att;

  // Set the attatchment info
  att.descr.filename = "green.png";
  att.descr.mime = "image/png";
  att.blob.data = (uint8_t *)greenImg;
  att.blob.size=strlen(greenImg);
  
  // Set the transfer encoding to base64
  att.descr.transfer_encoding = Content_Transfer_Encoding::enc_base64;
  // We set the content encoding to match the above greenImage data
  att.descr.content_encoding = Content_Transfer_Encoding::enc_base64;

  // Add attachment to the message
  message.addAttachment(att);  
  if (!smtp.connect(&email))
    return;

  /* Start sending Email and close the session */
  if (!MailClient.sendMail(&smtp, &message))
    Serial.println("Error sending Email, " + smtp.errorReason());

    
  
}
esp_err_t InitialiserCamera()
{
   
    esp_err_t       Retour_L;
    camera_config_t ConfigurationCamera_L;
    
    ConfigurationCamera_L.pin_d0 = 5;
    ConfigurationCamera_L.pin_d1 = 18;
    ConfigurationCamera_L.pin_d2 = 19;
    ConfigurationCamera_L.pin_d3 = 21;
    ConfigurationCamera_L.pin_d4 = 36;
    ConfigurationCamera_L.pin_d5 = 39;
    ConfigurationCamera_L.pin_d6 = 34;
    ConfigurationCamera_L.pin_d7 = 35;
    ConfigurationCamera_L.pin_xclk = 0;
    ConfigurationCamera_L.pin_pclk = 22;
    ConfigurationCamera_L.pin_vsync = 25;
    ConfigurationCamera_L.pin_href = 23;
    ConfigurationCamera_L.pin_sscb_sda = 26;
    ConfigurationCamera_L.pin_sscb_scl = 27;
    ConfigurationCamera_L.pin_pwdn = 32;
    ConfigurationCamera_L.pin_reset = -1;
   
    ConfigurationCamera_L.ledc_channel = LEDC_CHANNEL_0;
    ConfigurationCamera_L.ledc_timer = LEDC_TIMER_0;
    ConfigurationCamera_L.xclk_freq_hz = 20000000;
   
    ConfigurationCamera_L.pixel_format = PIXFORMAT_JPEG;
    ConfigurationCamera_L.fb_count = 2;
    ConfigurationCamera_L.jpeg_quality = 5;
    
    ConfigurationCamera_L.frame_size = FRAMESIZE_SVGA;
   
    Retour_L=esp_camera_init(&ConfigurationCamera_L);
    if (Retour_L == ESP_OK)
    {
        Serial.printf("La camera est initialisee\n");
    }
    else
    {
        Serial.printf("Erreur 0x%x lors de l'initialisation de la camera\n", Retour_L);
    }
    return(Retour_L);
}

void captureSave_image( void ) {
  camera_fb_t * fb = NULL; 
  bool ok = 0;

  
    Serial.println("ESP32-CAM capturing photo...");

    fb = esp_camera_fb_get();
    if (!fb) {
      Serial.println("Failed");
      return;
    }
    Serial.printf("Picture file name: %s\n", IMAGE_PATH);
    File file = SPIFFS.open(IMAGE_PATH, FILE_WRITE);
    if (!file) {
      Serial.println("Failed to open file in writing mode");
    }
    else {
      file.write(fb->buf, fb->len); 
      Serial.println("Photo enregistre");
    }
   
    file.close();
    esp_camera_fb_return(fb);
  }

Bonjour

tu trouveras peut être ton bonheur sur ce site très bien fourni :
https://randomnerdtutorials.com/esp32-cam-send-photos-email/

J'ai supprimé tes identifiants livebox du code.
D'autre part, 2 remarques

  • il est préférable de copier le code dans le corps du message (en utilisant le bouton </>) car les personnes qui répondent depuis leur téléphone ne peuvent pas toujours charger le code lorsqu'il est en pièce jointe.
  • il faudrait essayer d'être un rien plus clair dans tes demandes.

Bonjour je suis déjà allez sur ce site et ils y a plusieurs chose qui sont obsolète comme la classe SMTPDATA qui lorsque que je la marque moins elle ne marche pas.

ah oui j'avais pas vu pour les identifiants merci :grinning:
Sinon pour être plus clair enfaite sur le code ils y a une variable greenImage qui contient un code qui contient l'image et je sais pas comment s'appelle ce code (la base 64 par exemple ou autre) car il faudrait que je convertisse la photo que je génère avec l'esp32cam dans ce code.

C'est pourtant clair :

Pourquoi convertir ? il suffit d'envoyer l'image avec mime = "image/jpg"

Comment je fais pour remplacer la variable greenImg par l'image que je viens de prendre dans le même format.Dans la variable greenImg on a l'image qui est represente par un code et j'aimerai pouvoir convertir la photo que je vien de prendre et la convertir dans ce code pour la mettre dans la variable greenImg.

Tu veux dire que tu ne comprends pas ton propre code ?

Dans captureSave_image il y a tout ce qu'il faut.

// Récupération
    fb = esp_camera_fb_get();
// écriture du buffer dans un fichier
      file.write(fb->buf, fb->len);
// à remplacer par :
  att.blob.data = (uint8_t *)fb.buf;
  att.blob.size=fb.len;

Il est clair que dans ton cas, l'écriture dans un fichier est inutile.

ah ok merci je vais tester cela...

Merci cela marche enfesant quelque modification.Merci a vous!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.