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.