How to attach current form data to populate CSV and add to notifications [RESOLVED]

Hi all, I’m busy working on populating a CSV file with form entries. I got the code to work and attaches the CSV file to the notification, however, the attachment seems to be from the previous form submission. For example, I submit a form with the name John. The very first test that I received, the CSV contained John. I then send a second test with the name Ben, However, the attached CSV still contains the name, John. The email updates though with Ben. I then run a third test and have the name Micheal. The notification now updates the CSV to contain Ben, not Micheal. Yet the data in the email is Micheal.

How do i get it so the attachment updates on the current form submission?

Here is my code:

function populate_csv( $entry, $form ) {

//Headers info
$headers = array('Name', 'Surname', 'Email', 'Phone', 'Business Name', 'Address (Street Address)', 'Address (Address Line 2)', 'Address (City)', 'Address (State / Province)', 'Address (ZIP/ Postal Code)', 'Address (Cuntry)', 'Donation Amount', 'Donation Type', 'Hear more?', 'How did you hear about us?');

//Build form data
$data = array(
    'Name'    							=> rgar( $entry, '12' ),
    'Surname'     						=> rgar( $entry, '13' ),
    'Email'        						=> rgar( $entry, '5' ),
		'Phone'								=> rgar( $entry, '8'),
		'Business Name'						=> rgar( $entry, '6'),
		'Address (Street Address)'			=> rgar( $entry, '9.1' ),
		'Address (Address Line 2)'			=> rgar( $entry, '9.2' ),
		'Address (City)'					=> rgar( $entry, '9.3' ),
		'Address (State / Province)'		=> rgar( $entry, '9.4' ),
		'Address (ZIP/ Postal Code)'		=> rgar( $entry, '9.5' ),
		'Address (Cuntry)'					=> rgar( $entry, '9.6' ),
		'Donation Amount'					=> rgar( $entry, '15'),
		'Donation Type'						=> rgar( $entry, '16'),
		//'Donation Amount'					=> rgar( $entry, '1'),
		//'Donation Type'						=> rgar( $entry, '2'),
		'Hear more?'				=> rgar( $entry, '14.1'),
		'How did you hear about us?'		=> rgar( $entry, '11'), 
  );
// Create CSV File
// would be great if it had the submission id and the date in it
$fh = fopen('donation-entry.csv', 'w');

//Create headers
fputcsv($fh, $headers);

//Populate the data 
fputcsv($fh,$data);

//Close the file
fclose($fh);

}
add_action( 'gform_after_submission_3', 'populate_csv', 10, 2 );

// attach the same file to all User Notifications
add_filter("gform_notification_3", "add_attachment", 10, 3);
function add_attachment( $notification, $form, $entry ){
    // reference the notification by name
    if( $notification["name"] == "Test" ){
        // this is the file to be attached to all notifications
        $attachment =  'donation-entry.csv';
        $notification['attachments'] = array($attachment);
    }
    return $notification;
}

I managed to figure it out. Because I was using after form submission it was sending the notification before the CSV File was updated. so I had to change the
add_action( 'gform_after_submission_3', 'populate_csv', 10, 2 );
to
add_action( 'gform_pre_submission_3', 'populate_csv', 10, 2 );

which allowed me to grab form data before notifications are sent. However because “gform_pre_submission_3” is triggered before notifications are sent and entries are created, you cant use
rgar( $entry, '12' )
So I changed that to:
rgar( $_POST, 'input_12' )

This solved my issue.

1 Like