简体   繁体   中英

Generate csv file from form data on fly then email to address

I have a form that users are able to fill in and want to be able to generate an Excel file and populate it with the data from the form. I then want it to email me the file with the completed data. How can this be done?

This is currently generating the email:

    $body = "You have a new member account application from the website, please see the details below:

\n
Membership Type: $group1_field\n\n

Contact Details\n
Organisation: $member_organ_field\n
Address: $member_address_field\n
Postcode: $member_post_field\n
Tel: $member_tel_field\n
Fax: $member_fax_field\n
Email address: $member_email_field\n
Website: $member_website_field\n\n
Contacts \n
Member 1 \n
Name: $member1_name1_field\n
Position: $member1_position1_field\n
Direct Email: $member1_email1_field\n
Direct Tel: $member1_tel1_field\n\n
Member 2 \n
Name: $member1_name2_field\n
Position: $member1_position2_field\n
Direct Email: $member1_email2_field\n\n
Member 3 \n
Name: $member1_name3_field\n
Position: $member1_position3_field\n
Direct Email: $member1_email3_field\n\n
Type Of Organisation: ".$checkbox1results."\n
Type Of Activity: ".$checkbox2results."$member3_other_field\n\n
Summary Description \n
$member_summary_field \n\n
Company Information \n
Total no. of employees (Welsh site): $member4_total_field\n
Turnover (from Welsh site): $member4_turnover_field\n
Date of company formation: $member4__formation\n
Do you export aborad? Where? $member4_export_field\n\n
Member consent: ".$checkbox3results."\n\n
Completed by: $member4_com_field\n
Company position: $member4_com_pos_field\n
Invoice required: $CheckboxGroup4_field\n\n

Please follow up this request asap.\n\n
";

mail($to, $subject, $body);

echo "Thank you for your account application, we will contact you shortly.";

} ?>

The easiest way would be can create a csv, which is readable by Excel.

It's essentially a comma seperated list of values. Each line representing a row. The first line can optionally be the column headings, eg:

$string = '"Organisation","Address","Postcode"' . PHP_EOL;
$string .= "\"$member_organ_field\",\"$member_address_field\",\"$member_post_field\"" . PHP_EOL;

file_put_contents('myfile.csv', $string); // write file

Be sure to quote field names and values as a stray unquoted , (comma) would mess up your columns.

Other wise there is an PEAR library for write excel files.

You can them email the file as an attachment using the built-in mail function , see example here

Or using Swift Mailer , see example here

UPDATE

This is the general gist of what you need, the code may not be perfect to please read over it and don't copy and paste.

<?php

$random_hash = md5(date('r', time()));

$csvString = "..."; // your entire csv as a string
$attachment = chunk_split(base64_encode($csvString));

$to = "to@me.com";
$from = "from@you.com";
$subject = "my subject";
$body = "... what ever you want to appear in the body";

$headers = "From: $from\r\nReply-To: $from";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

$output = "
--PHP-mixed-$random_hash;
Content-Type: multipart/alternative; boundary='PHP-alt-$random_hash'
--PHP-alt-$random_hash
Content-Type: text/plain; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit

$body

--PHP-mixed-$random_hash
Content-Type: text/csv; name=mycsv.csv
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment
--PHP-mixed-$random_hash--";

mail($to, $subject, $output, $headers);

echo "Thank you for your account application, we will contact you shortly.";

?>

There are two options:

  1. Create a CSV file from the data above
  2. Create an Excel spreadsheet from the data above

By default, windows will associate CSV files with Excel (if it's installed) so most people will open a CSV in Excel (if they have it installed).

For each case, read here:

  1. http://www.dreamincode.net/forums/topic/10131-working-with-excel-files-comma-delimited-or-csv/
  2. http://phpexcel.codeplex.com/

It's also worth noting that if you're generating a .xls file then your PHP will need to run on windows with a copy of Excel installed (I wouldn't advise this, and if you want to go with the Excel option then you might want to consider creating .xlsx files with the PHPExcel tool I linked you to, this does not require an installed copy of Excel and so it can be deployed on Linux)

EDIT: To email the files as attachments you can user PHPMailer (an example of how easy this is can be found here , and you can download the class here )

Pear could be useful here. I found: Spreadsheet_Excel_Writer . Basic documentation and usage for it is here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM