PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Issue with multiple files attachment

  1. Hi guys!

    I was using the example that you wrote on the gform_admin_notificacion_attachments documentation (http://www.gravityhelp.com/documentation/page/Gform_admin_notification_attachments). It works great! but after a few tests I realize something:

    If you have (for example) 5 file upload fields, and you only attach 2 (the 5 file fields are marked as not required... so, is ok to attach only 2), no one file will be attached!

    The problem is that in the example, $attachments will be returned as an array of 5 indexes, indexes 0 and 1 will have the path of each image, but indexes 2, 3 and 4 will be isset but empty. That seems to cause that no one attachment is sent.

    [php]
    <?php
    add_filter("gform_admin_notification_attachments", "add_attachment", 10, 3);
    function add_attachment($attachments, $lead, $form){
        $fileupload_fields = GFCommon::get_fields_by_type($form, array("fileupload"));
        if(!is_array($fileupload_fields))
            return $attachments;
        $attachments = array();
        $upload_root = RGFormsModel::get_upload_root();
        foreach($fileupload_fields as $field){
            $url = $lead[$field["id"]];
            $attachments[] = preg_replace('|^(.*?)/gravity_forms/|', $upload_root, $url);
        }
        return $attachments;
    }
    ?>

    To fix this, I made a little change to the function:

    [php]
    <?php
    add_filter("gform_admin_notification_attachments_4", "add_attachment", 10, 3);
    function add_attachment($attachments, $lead, $form){
    	$fileupload_fields = GFCommon::get_fields_by_type($form, array("fileupload"));
    	if(!is_array($fileupload_fields)){
    		return $attachments;
    	}
    	$attachments = array();
    	$upload_root = RGFormsModel::get_upload_root();
    	foreach($fileupload_fields as $field){
    		$url = $lead[$field["id"]];
    		$attachment = preg_replace('|^(.*?)/gravity_forms/|', $upload_root, $url);
    		if($attachment){
    			$attachments[] = $attachment;
    		}
    	}
    	return $attachments;
    }
    ?>

    Now the returned array has only the 2 indexes with images, and the images are sent to the email.

    I hope this would be helpful to somebody as it was for me.

    Posted 7 years ago on Friday August 10, 2012 | Permalink
  2. I have up to 6 attachment fields, none of which are required, so any number could be used. Is this the code I would need to use? (And I'm a total amateur at this... so would I just be copying and pasting the above code as is into functions.php (no changes)?

    I was also wondering if there's a way to attach uploaded files to the notification email without saving them into WordPress. We'd like to have them come straight to us rather than piling up on our website.

    Thanks for any help!

    Posted 7 years ago on Tuesday August 21, 2012 | Permalink
  3. Hi vostn,

    First of all, I'm sorry for the delay of my answer, I didn't get a notification of your reply =)

    You can just copy/paste the second block of code that I pasted and use it as is in your functions.php. The first block is the one that is suggested on the documentation, but as I said, it has functionality issues.

    The code is being showed a little strange in my first post (I think I didn't use the correct format for publish code on this forum) so I will add it again, just copy/paste the following:

    [php]
    add_filter("gform_admin_notification_attachments", "add_attachment", 10, 3);
    function add_attachment($attachments, $lead, $form){
    	$fileupload_fields = GFCommon::get_fields_by_type($form, array("fileupload"));
    	if(!is_array($fileupload_fields)){
    		return $attachments;
    	}
    	$attachments = array();
    	$upload_root = RGFormsModel::get_upload_root();
    	foreach($fileupload_fields as $field){
    		$url = $lead[$field["id"]];
    		$attachment = preg_replace('|^(.*?)/gravity_forms/|', $upload_root, $url);
    		if($attachment){
    			$attachments[] = $attachment;
    		}
    	}
    	return $attachments;
    }

    Finally, answering your question of attach images to email and don't save them in Wordpress, it is possible, but that's a different topic. Browse the forum to see if there's a topic about it, or publish a new one.

    Best,

    Posted 7 years ago on Thursday August 30, 2012 | Permalink
  4. @miguel:

    The code is being showed a little strange in my first post (I think I didn't use the correct format for publish code on this forum)

    I fixed the formatting by removing the extraneous HTML.

    Posted 7 years ago on Thursday August 30, 2012 | Permalink
  5. @miguel

    Thank you, this worked great!

    Posted 7 years ago on Thursday August 30, 2012 | Permalink
  6. Thank you @Chris Hajer.

    Posted 7 years ago on Thursday August 30, 2012 | Permalink