Ask your WordPress questions! Pay money and get answers fast! (more info)

Gravity Forms Attachments WordPress

  • SOLVED

Two part request:

1)Need to generate a CSV from the $entry object after submission and attach it to the notification
2)Simply need to attach a static PDF

For 1) I am looking for a small example of the $entry->proper CSV array->generate and save the CSV on the server->attach the same way as 2)

For 2) I have this, but it is not generating an attachment, what's missing?

add_filter('gform_notification_10', 'change_user_notification_attachments', 10, 3);
function change_user_notification_attachments( $notification, $form, $entry ) {

if($notification["name"] == "User Notification"){

$upload = wp_upload_dir();
$upload_path = $upload["basedir"];

$attachments = array();
$attachments[] = $upload_path .'/2013/12/Test-T-and-C-s-doc.pdf';
$notification["attachments"] = $attachments;

}

return $notification;
}


I checked the 'if=notification name', and that is properly working.

Answers (2)

2013-12-13

Dbranes answers:

I tested your code in <strong>2)</strong> and it works on my install, I receive an attached <em>pdf</em> file when I submit a test form.

What happens if you remove the <em>if</em>-condition? Can you open the <em>pdf</em> file in your browser? Can you check your server maillog?

<strong>1)</strong> You could try something like this to create a <em>csv </em>file and attach it to the <em>"User notification"</em>:


function change_user_notification_attachments( $notification, $form, $entry )
{

if( 'user notification' === strtolower( $notification['name'] ) )
{
// upload path
$upload = wp_upload_dir();
$upload_path = $upload['basedir'];
// Example: /absolute/path/to/wp-content/uploads/

$csv_path = sprintf( '%s/csv', $upload_path );
// Example: /absolute/path/to/wp-content/uploads/csv

$csv_file_name = sprintf( 'entry_form_%s_id_%s_%s.csv', $entry['form_id'], $entry['id'], uniqid() );
// Example: entry_form_10_id_33_52aa730f2f9f1.csv

$csv_file_path = sprintf( '%s/%s', $csv_path, $csv_file_name );
// Example: /absolute/path/to/wp-content/uploads/csv/entry_form_10_id_33_52aa730f2f9f1.csv

$csv_row = join( ', ', $entry );


// create directory if it doesn't exists
if ( ! is_dir( $csv_path ) ) {
wp_mkdir_p( $csv_path );
}

// create a csv file and add it as an attachment
if( FALSE !== file_put_contents( $csv_file_path, $csv_row ) )
{
$attachments = array();
$attachments[] = $csv_file_path;
$notification['attachments'] = $attachments;
}

}

return $notification;
}

add_filter( 'gform_notification_10', 'change_user_notification_attachments', 10, 3 );


I tested this and I got an email with the attached file:

entry_form_10_id_33_52aa730f2f9f1.csv


with the content:

33, 10, 2013-12-13 12:39:03, 0, 0, 127.0.0.1, http://localhost/form, , USD, , , , , , 1, , Mozilla/5.0, active, abc


Kyle comments:

Thanks for the reply and extensive testing, it is very much appreciated! I am about to call it a night here in the est timezone, is there a time I should shoot for to get back to you before you finish up yourself so we can collaborate a little?


Dbranes comments:

I'm going offline too (middle of the night here) so I will check again tomorrow ;-)

cheers


Kyle comments:

Sounds good, I'll do some testing and post a reply in the morning.


Kyle comments:

This all seems to work absolutely perfectly :) Thanks!

2013-12-12

Hariprasad Vijayan answers:

Hello,

I think you need to use
add_filter("gform_user_notification_attachments_10"


instead of

add_filter('gform_notification_10'
for attachment
Check this

http://www.gravityhelp.com/documentation/page/Gform_user_notification_attachments


Kyle comments:

Thanks for the reply. That hook has been deprecated and is not a viable long term solution.


Hariprasad Vijayan comments:

okay.

you have used

$upload["basedir"];
I think it should
$upload_dir['baseurl'];


Kyle comments:

Good idea, but no luck


Hariprasad Vijayan comments:

Please check Example 5

http://www.gravityhelp.com/documentation/page/Gform_notification


Kyle comments:

Yes, that is what mine is based off of, except that example is based on file upload fields, not pulling files from the server.