• Resolved thankssforhelp

    (@thankssforhelp)


    Hi there,

    I would appreciate help with the following:

    I want to send a file in the email notifications as an attachment. However, this file is not directly an upload from the respective form, but is a pre-populate file within a hidden field and comes from another form. Can I send this as an attachment as described?

    Thank you very much for your help.

    Kind regards

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @thankssforhelp

    I hope you’re well today!

    It’s not doable out of the box but I suppose it may be possible with some additional code. But let me ask first:

    1. the file in question is passed to the form so information about it is in a field of type “hidden” – what does this field contain? Is it a full URL of the image or the path only?

    2. and the file comes from the very same site (just different form/page) and it is added already to Media Library, right?

    Best regards,
    Adam

    Thread Starter thankssforhelp

    (@thankssforhelp)

    Hei Adam @wpmudev-support8,

    thank you very much for the quick feedback.

    Re 1: Exactly, the information in the form in question is in the “hidden” field. This contains the path to a PDF file that was previously uploaded via another form. So far I have used the following solution in the email notifications: URL…/{hidden-1}. Clicking on this will then open the PDF. My goal is that this is no longer necessary and the PDF is located directly as an attachment to the email.

    Re 2: Exactly, the file comes from the same page and was previously uploaded via another Forminator form.

    Thanks for the prompt help and kind regards

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @thankssforhelp

    Thank you for response and additional information!

    You will need a bit of additional custom code for this:

    <?php 
    
    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }
    
    if ( ! class_exists( 'WPMUDEV_Frominator_Upload_Email_Attachment' ) ) {
        
        class WPMUDEV_Frominator_Upload_Email_Attachment {
    
            public $attachments                 = 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_custom_form_submit_errors', array( $this, 'manage_field_data' ), 20, 3 );
    
            }
    
            public function manage_field_data( $submit_errors, $form_id, $data ) {
    			
    			
    			$field_ids = array( 'hidden-1', 'hidden-2' ); // fields that should be used for attachments
    			
                if ( empty( $submit_errors ) ) {
    
                    foreach ( $data as $key => $field_data ) {
    					
    								
    					if ( ! in_array( $field_data['name'], $field_ids ) ) {
    						continue;
    					}
    					
    					$this->attachments[] = ABSPATH . $field_data['value'];
    
                    }
                }
    
                return $submit_errors;
            }
    
            public function filter_email_args( $mail_args ) {
    
                if ( ! empty( $this->attachments ) ) {
                    $mail_args[ 'attachments' ] = $this->attachments;
    				
                }
    
                return $mail_args;
            }
    
        }
    
        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 );
        }
    
    }

    To add it to the site:

    1. create an empty file with a .php extension (e.g. “forminator-hidden-field-mail-attachment.php”)

    2. copy and paste code into it

    3. in this line of the code

    $field_ids = array( 'hidden-1', 'hidden-2' ); // fields that should be used for attachments

    replace hidden-1 and hidden-2 with (one or more; if more – comma separated) IDs of your hidden fields that contain the file to attach

    4. save the file and upload it to the “/wp-content/mu-plugins” folder of your site’s WordPress installation.

    If there is no “mu-plugins” folder directly in “wp-content”, just create an empty one first.

    This should work out of the box but note also that in your hidden field(s) you need to use specific path format. For example, if you want to attach the image located at

    https://yoursite.com/wp-content/uploads/something/myimage.jpg

    you would set the hidden field to “Custom value” option and in value put exactly this

    wp-content/uploads/something/myimage.jpg

    If this path is added automatically to hidden field and it is a bit different, then let us know exactly what the path is (you can just share example one) and we should be able to modify the code accordingly.

    Best regards,
    Adam

    Thread Starter thankssforhelp

    (@thankssforhelp)

    Hi Adam @wpmudev-support8,

    thank you very much for your quick reply.

    I have tested this code, it works fine.

    However, the value in the hidden-field is automatically added, as already described by you:

    If this path is added automatically to hidden field and it is a bit different, then let us know exactly what the path is (you can just share example one) and we should be able to modify the code accordingly.

    The value I have in the hidden field is a query parameter, so it changes every time.

    The uploads are located in the following path:
    wp-content/uploads/forminator/form-id_longnumber/uploads/{hidden-3}

    Whereby:

    1. it always stays the same: wp-content/uploads/forminator/form-id_longnumber/uploads/
    2. it changes every time: the name of the PDF in {hidden-3}

    Maybe brief background on how it comes to this value in {hidden-3}:

    1. customer fills out form 1 and uploads a PDF file.
    2. with the submission a notification email goes to several lecturers
    3. lecturers can open the link in the email and accept the customer’s order by filling out a form. With the submission, the PDF file name from step 1 is included in this form as {hidden-3}.
    4. once the instructor submits this acceptance form, he will get a notification mail with this link: wp-content/uploads/forminator/form-id_longnumber/uploads/{hidden-3}

    Now the lecturer can click on this link and access the PDF. But the aim is that the PDF is directly available as an attachment already in step 3.

    Thanks a lot and kind regards

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @thankssforhelp

    Thanks for response!

    The path, as you described it, should be fine then. The only difference would be that your hidden field must be set to “Query Var” option with a query var name in “Value” field instead – which, I believe, you already have set this way

    and you’d need to change this line in the code

    $this->attachments[] = ABSPATH . $field_data['value'];

    to this

    $this->attachments[] = ABSPATH . 'wp-content/uploads/forminator/form-id_longnumber/uploads/' . $field_data['value'];

    using, of course, the correct form-id_longnumber part.

    Best regards,
    Adam

    Thread Starter thankssforhelp

    (@thankssforhelp)

    Hi Adam @wpmudev-support8,

    I have tested directly, works wonderfully, huge thanks!

    Kind regards

    Thread Starter thankssforhelp

    (@thankssforhelp)

    Hi Adam @wpmudev-support8,

    I have one more question:

    Everything works fine, however, the PDF files in the attachment contain an addition that was not actually included, eg.
    xceH1GVnFETG-pdf-file-name
    or
    uReCVL4IcX8u-pdf-file-name,
    where pdf-file-name is the original name of the uploaded file.

    Is there a specific reason for this or can this be changed so that the original file name is always retained?

    Thanks a lot and kind regards

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @thankssforhelp

    This isn’t related to the code that I shared but rather to the fact that files are uploaded through the Forminator form.

    With recent releases of Forminator there were some changes made in a way how files are handled – for security reasons. Every file uploaded via the form has the name altered a bit with such “hash” added.

    This can’t be changed, I’m afraid.

    Kind regards,
    Adam

    Thread Starter thankssforhelp

    (@thankssforhelp)

    Hi Adam @wpmudev-support8,

    okay all fine and thanks again for the help!

    Kind regards

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Send file as attachment in notifications’ is closed to new replies.