Need help for attachments with Post Image

Hello everyone!
I have created a form with multiple field “Post Image” windows, users can put photos into this field and with a custom code these photos go automatically in a specific folder and in the media gallery. However, I would like the notification sent by e-mail to include attachments. Would this be possible? If so, how? I saw that for the “File Upload” window there was a tutorial with a code provided but I can’t replace it with “Post Media”.

My code for now :

add_filter( 'gform_notification_21', 'rw_notification_attachments', 10, 3 );

function rw_notification_attachments( $notification, $form, $entry ) {
    // Liste des IDs de champs à vérifier
    $field_ids = array(23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33);
    
    // Initialise un tableau pour collecter les fichiers joints
    $attachments = array();

    // Parcourt chaque ID de champ
    foreach ( $field_ids as $field_id ) {
        // Récupère les données du champ post_image pour le champ actuel
        $image_data = rgar( $entry, $field_id );

        // Continue uniquement si des données d'image sont trouvées
        if ($image_data) {
            // Décodage des données JSON en tableau associatif
            $image_data = json_decode($image_data, true);
            
            // Vérifie si le tableau contient bien un fichier
            if (isset($image_data['file'])) {
                $uploaded_file = $image_data['file'];
                
                // Ajoute le fichier à la liste des pièces jointes
                $attachments[] = $uploaded_file;
            } else {
                error_log("Field ID $field_id does not contain a valid 'file' element.");
            }
        } else {
            error_log("Field ID $field_id does not contain any image data.");
        }
    }

    // Si des pièces jointes ont été trouvées, ajoutez-les à la notification
    if (!empty($attachments)) {
        $notification['attachments'] = $attachments;
        $notification['enableAttachments'] = 1;
    } else {
        error_log("No attachments found for form entry.");
    }

    // Retourne la notification modifiée
    return $notification;
}

Does anyone have a solution? It would be a great help, thank you :pray:

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