• Resolved robaxxx

    (@robaxxx)


    Hi there and thanks for the great form plugin.

    I’ve made a form that has a few multi-file upload fields, just for JPG and PDF, and everything seems to work except the files won’t attach to the email. Instead, the notification shows them as links.

    I’ve applied the notification setting to attach uploaded files and I’ve read the other couple of support tickets here with the same query, but there isn’t a fix on those.

    I’ve got it set to not show the files in the media library and to delete after 10 days. The files are present on the server in \wp-content\uploads\sites\5\2022\08.

    Any advice is appreciated.
    Rob

    Wordpress 6.0.1
    Software: Microsoft-IIS/10.0
    MySQL version: MySQL Community Server (GPL) v5.7.34
    PHP Version: 7.4.13
    PHP Memory Limit: 512M

Viewing 3 replies - 16 through 18 (of 18 total)
  • Plugin Support Dimitris – WPMU DEV Support

    (@wpmudev-support6)

    Hello @robaxxx

    Could you please try to add the following snippet in a new MU plugin file (https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins) and test it first in a staging/dev environment?

    <?php
    /**
    * Plugin Name: [Forminator] - Attach upload file to email
    * Plugin URI: https://premium.wpmudev.org/
    * Description: Adds two new options in Uplaod's Edit Field popup under the Advanced tab. One option is for adding the uploaded file as an attachment. Second option is for deleting the file after upload
    * Author: Panos Lyrakis & Prashant Singh @ WPMUDEV
    * Author URI: https://premium.wpmudev.org/
    * License: GPLv2 or later
    */
    
    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }
    
    if ( ! class_exists( 'WPMUDEV_Frominator_Upload_Email_Attachment' ) ) {
        
        class WPMUDEV_Frominator_Upload_Email_Attachment {
    
            public $attachments                 = array();
            public $attachments_to_delete       = array();
            private static $_instance           = null;
           
            public static function get_instance() {
    
                if( is_null( self::$_instance ) ){
                    self::$_instance = new WPMUDEV_Frominator_Upload_Email_Attachment();
                }
                return self::$_instance;
                
            }
    
            private function __construct() {
    
                add_filter( 'wp_mail', array( $this, 'filter_email_args' ) );
                add_filter( 'forminator_field_upload_general_settings', array( $this, 'add_attachment_settings' ) );
    
                add_filter( 'forminator_custom_form_submit_field_data', array( $this, 'manage_field_data' ), 20, 2 );
                add_action( 'forminator_custom_form_mail_after_send_mail', array( $this, 'delete_uploaded_files' ) );
    
            }
    
            public function add_attachment_settings( $settings ) {
    
                $attachment_option = array(
                        'type'          => 'Toggle',
                        'label'         => 'Attach to email',
                        'name'          => 'use_as_attachment'
                    );
                $delete_upload_option = array(
                        'type'          => 'Toggle',
                        'label'         => 'Delete file after upload',
                        'name'          => 'delete_uploaded_file'
                    );
    
                array_push( $settings, $attachment_option, $delete_upload_option );
    
                return $settings;
    
            }
    
            public function manage_field_data( $data, $form_id ) {
    
                foreach ( $data as $key => $field_data ) {
    
                    if ( ! isset( $field_data['name'] ) ) {
                        continue;
                    }
    
                    $field = Forminator_API::get_form_field( $form_id, $field_data['name'] );
    
                    if ( is_wp_error( $field ) ) {
                        continue;
                    }
    
                    $field_type    = Forminator_Field::get_property( 'type', $field );
    
                    if ( 'upload' != $field_type ) {
                        continue;
                    }
                    if( !is_array( $field_data[ 'value' ][ 'file' ][ 'file_path' ] ) ){
                        if ( $file_path = $field_data[ 'value' ][ 'file' ][ 'file_path' ] ) {
                            
                            if ( Forminator_Field::get_property( 'use_as_attachment', $field ) ) {                   
                                $this->attachments[] = $file_path;
                            }
    
                            if ( Forminator_Field::get_property( 'delete_uploaded_file', $field ) ) {                    
                                $this->attachments_to_delete[] = $file_path;
                            }
    
                        }
                    } else {
                        foreach( $field_data[ 'value' ][ 'file' ][ 'file_path' ] as $fi_k => $fi_val ){
    
                            if ( Forminator_Field::get_property( 'use_as_attachment', $field ) ) {   
                                $this->attachments[] = $fi_val;
                            }
    
                            if ( Forminator_Field::get_property( 'delete_uploaded_file', $field ) ) {                    
                                $this->attachments_to_delete[] = $fi_val;
                            }
    
                        }
                    }
    
                }
    
                return $data;
            }
    
            public function filter_email_args( $mail_args ) {
    
                if ( ! empty( $this->attachments ) ) {
                    $mail_args[ 'attachments' ] = $this->attachments;
                }
    
                return $mail_args;
            }
    
            public function delete_uploaded_files() {
                
                if ( ! empty( $this->attachments_to_delete ) ) {
                    foreach ( $this->attachments_to_delete as $file_path ) {
                        unlink( $file_path );
                    }
                }
                if ( ! empty( $this->attachments ) ) {
                    unset( $this->attachments );
                }
            }
        }
    
        if ( ! function_exists( 'wpmudev_forminator_upload_email_attachment' ) ) {
    
            function wpmudev_forminator_upload_email_attachment() {
                return WPMUDEV_Frominator_Upload_Email_Attachment::get_instance();
            };
    
            add_action( 'plugins_loaded', 'wpmudev_forminator_upload_email_attachment', 10 );
        }
    
    }

    Thank you,
    Dimitris

    Thread Starter robaxxx

    (@robaxxx)

    Hi Dimitris

    I’ve tested this now on the Azure trial server I set up for this purpose. The form plugin there is still version 1.17.2 and has not been upgraded.

    The test emails arrived with the files correctly attached and the file was also successfully removed from the server when the second new option was enabled.

    But note – I have since also discovered that the new release of Forminator – version 1.18.1 correctly attaches the files to emails. This is on my live website where I first reported the problem and where I let the plugin update itself.

    I don’t know if that release already includes the fix above, but it certainly works now, so that’s great.

    Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @robaxxx,

    We are happy to hear that the issue has been resolved.
    Please let us know in case you need further help.

    Kind regards,
    Zafer

Viewing 3 replies - 16 through 18 (of 18 total)
  • The topic ‘Files not attached to email’ is closed to new replies.