从表单数据中生成csv文件,然后通过电子邮件发送到地址

时间:2011-03-04 11:34:36

标签: php email forms

我有一个表单,用户可以填写并希望能够生成Excel文件并使用表单中的数据填充它。然后我希望它用完整的数据通过电子邮件发送给我。怎么办呢?

目前正在生成电子邮件:

    $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.";

} ?>

3 个答案:

答案 0 :(得分:9)

最简单的方法是创建一个可以被Excel读取的csv。

它本质上是一个逗号分隔的值列表。每行代表一行。 第一行可以选择是列标题,例如:

$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

请务必引用字段名称和值作为杂散的不加引号,(逗号)会弄乱您的列。

另外,对于写excel文件有PEAR library

您可以使用built-in mail functionsee example here

将文件作为附件发送电子邮件

或使用Swift Mailersee example here

<强>更新

这是您需要的一般要点,代码可能不完美,请您阅读并不要复制和粘贴。

<?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.";

?>

答案 1 :(得分:1)

有两种选择:

  1. 根据以上数据创建CSV文件
  2. 根据上述数据创建Excel电子表格
  3. 默认情况下,Windows会将CSV文件与Excel相关联(如果已安装),因此大多数人都会在Excel中打开CSV(如果安装了它)。

    对于每种情况,请阅读:

    1. http://www.dreamincode.net/forums/topic/10131-working-with-excel-files-comma-delimited-or-csv/
    2. http://phpexcel.codeplex.com/
    3. 还值得注意的是,如果你正在生成.xls文件,那么你的PHP需要在安装了Excel副本的Windows上运行(我不建议这样做,如果你想使用Excel选项)那么您可能需要考虑使用我链接到的PHPExcel工具创建.xlsx文件,这不需要安装Excel的副本,因此可以在Linux上部署)

      编辑:要将文件作为附件发送电子邮件,您可以使用PHPMailer(这是一个简单易用的示例here,您可以下载课程here

答案 2 :(得分:0)

梨子在这里很有用。我找到了:Spreadsheet_Excel_Writer。它的基本文档和用法是here