I’ve been meaning to share this dirty hack for a while: how to use eform to attach a CSV file to an email containing the form data.
1. Create a snippet called ’eFormEvents’; this will contain a function that is attached to the eform event eformOnBeforeMailSent.
This is an example of what I have, which grabs my form fields, formats the csv data string, inserts into a new field called csv, and also creates a field called registrationid containing a unique reference to the submitted data (so we can give the csv file a unique name):
// attach form data as a CSV file
function attachCSV(&$fields){
$cr = "\n";
$csv = '"Title","First name","Last name","Email","Telephone","Mobile","Address 1","Address 2","City","County","Postcode", "Worktype","Day rate","Functional Expertise","Industry Expertise","Skills","Job Reference","How did you hear?","Privacy policy"'.$cr;
$csv .=
csv($fields['title']).','
.csv($fields['firstname']).','
.csv($fields['lastname']).','
.csv($fields['email']).','
.csv($fields['telephone']).','
.csv($fields['mobile']).','
.csv($fields['address1']).','
.csv($fields['address2']).','
.csv($fields['city']).','
.csv($fields['county']).','
.csv($fields['postcode']).','
.csv($fields['worktype']).','
.csv($fields['dayrate'].' per '.$fields['rateperiod']).','
.csv(implode(" | ", $fields['functionalexpertise'])).','
.csv(implode(" | ", $fields['industryexpertise'])).','
.csv(implode(" | ", $fields['skills'])).','
.csv($fields['reference']).','
.csv($fields['howhear']).','
.csv($fields['privacy']).$cr;
$registrationid .= strtoupper(substr($fields['firstname'],0,1)).strtoupper(substr($fields['lastname'],0,1));
$registrationid .= date('ymdHi');
// set fields
$fields['csv'] = $csv;
$fields['registrationid'] = $registrationid;
return true;
}
function csv($value) {
if (strstr($value, ",") || strstr($value, "\n") || strstr($value, "\r")) {
return "\"" . str_replace("\"", "\"\"", $value) . "\"";
} else {
return "\"".$value."\"";
}
}
2. Add the function to your eform snippet call, eg:
[!eFormEvents!]
[!eForm? &formid=`myFormId` &to=`....` &tpl=`....` &report=`....` &subject=`....` &eformOnBeforeMailSent=`attachCSV` !]
3. Add this to eform.inc.php (version 1.4.4.5) at around line 473:
// attach csv
if ($fields['csv'] !='') {
$mail->AddStringAttachment($fields['csv'], 'CSV_'.$fields['registrationid'].'.csv');
}
Done. Glad I got that off my chest...