CSV file as Mail attachment

hsukra 24 Jun, 2015
Hey!
I have a problem with creating a CSV file and sending it per Mail. There should be no download as it is in the standard action in chronoforms 5 – it is not a solution if the customer gets the csv to send it manually per mail.
It works fine in Chronoforms 4 and with PDF and XLS in Chronoforms 5, but not with CSV – I Don’t get the point here, why this is not implemented.
The process is the following:
- Customer fills the form
- Clicks submit
- Data is stored to the database
- Csv is created
- Csv is sent via mail (and is no longer needed)
I searched the internet and this forum, but I could only find partial solutions and I’m honest to be not able to get everything together.
I know the standard CSV routine does not fit here and I need Custom code. The following code can create the file, write a single field, save the file to the directory and afterwards get ‘my_file’ as an Email attachment. Now the only thing missing is the data.
The field names (or ID) should be the header and the data the values (of course). I only need a single record per file. A manual writing of all fields is not a good solution, because I need this for several different forms.
I think with a foreach and $form->data it should be possible, but I really can’t get the solution.

Thanks for your help and if there are any questions, please ask!🙂


 <?php
 // name and path
 $filename = 'attachment.csv';
 $filepath = 'tmp/';
 // creating the CSV row with a single field from the array (proves that the data is there)
 $csv_row = $form->data['VMName'];
 
 // saving it in the file
    $file = fopen ($filepath.$filename, 'w');
 fwrite ($file, $csv_row);
 fwrite ($file, $csv_row2);
    fclose ($file);
    

// creating 'my_file' for using as attachment
$form->files['my_file'] = array(
  'name' => $filename,
  'original_name' => '',
  'path' => $filepath.$filename,
  'size' => '',
  'link' => str_replace(JPATH_SITE.DS, JURI::root(), $filepath.$filename)
);
    ?>
GreyHead 25 Jun, 2015
Hi hsukra,

I suspect that the path may not be valid please try
$filepath = JPATH_SITE.'/tmp/';


Also $csv_row2 isn't defined as far as I can see.

Bob
hsukra 25 Jun, 2015
Hi Bob,

sorry there is a little mistake. You just can ignore $csvrow2 - I already deleted it.

But as I said, the file is generated without problems (even with the path in my first post).

As I described, the problem is to get the data into the file. I'm just not able to put the values from the array as separated entries in the csv-file
cambium 25 Jun, 2015
hi Bob.
I have a problem with attaching the file to my message from site.
i create the custom code element into submit action (install tab of form),
Added the code:
<?php

 $filename = 'rekvizitySB.pdf';
 $filepath =  JPATH_SITE.'/images/';
 
 $form->files['my_file'] = array(
  'name' => $filename,
  'original_name' => '',
  'path' => $filepath.$filename,
  'size' => '67478', //this size of file is from table of cpanel of my hosting
  'link' => str_replace(JPATH_SITE.DS, JURI::root(), $filepath.$filename)
);
?>

After that i set the file type option 'pdf' into one of email elements.
But is no file with my test message.
Please, tell me what is my mistake
GreyHead 26 Jun, 2015
Hi s.elena133,

You need to put my_file in the Attachments box of the Email action as that is the name you have given this data. If that doesn't help please drag a Debugger action into the On Submit event, then submit the form and post the debug - including the 'dummy emails' results here.

Bob
GreyHead 26 Jun, 2015
1 Likes
Hi hsukra,

Sorry for my getting the question wrong.

As your data is presumably an array, please try using fputcsv() instead of fwrite().

Bob
cambium 26 Jun, 2015
Нi Bob
Thousand thanks for your fast reply!
I did it and the attachment file was received with message.

but i'm not sure, that i did it correctly:
1. i added the File Field (name is 'file30') to my form,
2. put the Files Upload into the On Submit event and set it options
3. wrote the input's name in my code
$form->files['file30'] = array(....


The file was sent but with errors.
I attach listing here.

Tell me, where is my mistake and did i understand you correctly?
For attaching a files with message i have to add the File Field to my form?
GreyHead 26 Jun, 2015
1 Likes
Hi s.elena133,

There are no errors - you are just seeing the Debugger output, disable or delete the Debugger to hide it.

Bob
cambium 26 Jun, 2015
Hi Bob
I deleted it

And thank you so much for your help!
hsukra 29 Jun, 2015
Hi Bob!

Thanks for your help! Now I got almost what I need:

This code works perfectly to save the Data read from the database and saved in Model ID "Export" (I changed the whole routine a little bit). Now I get a perfect csv file with one line that contains the data.

 <?php
 // some variables
 $filename = 'attachment.csv';
 $filepath = 'tmp/';
 // creating the CSV row

$csv_row = $form->data['Export'];

 // saving it in the file
    

$file = fopen ($filepath . $filename, 'w');

foreach ($csv_row as $fields) {
    fputcsv($file, $fields);
}


    fclose ($file);
    // adding the file to the email as attachment

$form->files['my_file'] = array(
  'name' => $filename,
  'original_name' => '',
  'path' => $filepath.$filename,
  'size' => '',
  'link' => str_replace(JPATH_SITE.DS, JURI::root(), $filepath.$filename)
);
    ?>


Now I have an additional question: how can I get the Field IDs as a Title row?

I made some changes below, but I dont know how to get the Field IDs.

 <?php
 // some variables
 $filename = 'attachment.csv';
 $filepath = 'tmp/';
 // creating the CSV row
$csv_titlerow = $form->data............... // I know I need another row that contains the field IDS, but I don't know how to get it...
$csv_row = $form->data['Export'];

 // saving it in the file
    

$file = fopen ($filepath . $filename, 'w');

foreach ($csv_titlerow as $title) {
    fputcsv($file, $title);
} // ... and I have to add it to the file.

foreach ($csv_row as $fields) {
    fputcsv($file, $fields);
}


    fclose ($file);
    // adding the file to the email as attachment

$form->files['my_file'] = array(
  'name' => $filename,
  'original_name' => '',
  'path' => $filepath.$filename,
  'size' => '',
  'link' => str_replace(JPATH_SITE.DS, JURI::root(), $filepath.$filename)
);
    ?>


So how can I get the field IDs as a title row?

ID,name,street,city... <- this is what I need now
1,John,"Broadway 1","New York".... <- this is fine already

Thank you!!
GreyHead 30 Jun, 2015
Hi hsukra,

You can set a fixed array with the titles if they are always the same:
$titles = array(
  'Column 1,
  'Column 2',
  . . .
);
or you could get the keys from the Export array
$titles = array_keys($form->data['Export']);

Bob
hsukra 30 Jun, 2015
Hi Bob!

Perfekt! Have some virtual beer😉 !!! This is great and I got everything working with your help. I had to try it out a little bit, but this ist the code that gets everything together, if someone else has the same question:

 <?php
 // some variables
 $filename = 'attachment.csv';
 $filepath = 'tmp/';

 // creating the CSV row
$csv_row = $form->data['Export']; //gets the data from my DBRead with model ID = Export

// writing it to the file
$file = fopen ($filepath . $filename, 'w');

foreach ($csv_row as $fields) {
  fputcsv($file, array_keys($fields));
  fputcsv($file, $fields);
}

fclose ($file);

    // adding the file to the email as attachment

$form->files['my_file'] = array(
  'name' => $filename,
  'original_name' => '',
  'path' => $filepath.$filename,
  'size' => '',
  'link' => str_replace(JPATH_SITE.DS, JURI::root(), $filepath.$filename)
);
    ?>
This topic is locked and no more replies can be posted.