2011-03-04 107 views
1

我有一個表單,用戶可以填寫並希望能夠生成Excel文件並使用表單中的數據填充表單。然後我希望它通過電子郵件向我發送完整數據的文件。如何才能做到這一點?從表單數據生成csv文件,然後通過電子郵件發送到地址

這是目前生成的電子郵件:

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

} ?> 

回答

9

最簡單的方法是創建一個csv,它可以被Excel讀取。

它本質上是逗號分隔的值列表。每行代表一行。 第一行可任選列標題,例如:

$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 

一定要引用字段名和值作爲流浪未加引號的,(逗號)會弄亂你的列。

其他方面有一個PEAR library寫excel文件。

可以將這些文件使用built-in mail function郵件作爲附件,see example here

或者使用Swift Mailersee example here

UPDATE

這是你需要什麼,代碼的一般要點可能不完美,請仔細閱讀,不要複製和粘貼。

<?php 

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

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

$to = "[email protected]"; 
$from = "[email protected]"; 
$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."; 

?> 
+0

是的,這正是我想要的,這怎麼可以通過電子郵件發送到一個地址? – Rob 2011-03-04 11:41:01

+0

使用您正在使用的任何電子郵件庫,只需在將文件寫入硬盤後將其作爲附件添加即可。您可以在完成發送電子郵件後刪除。 – xzyfer 2011-03-04 11:42:41

+0

使用內置的「郵件」功能,附件可能有點棘手。但也許可能,只是谷歌它。雖然我會推薦PEAR郵件庫或更好的Swift郵件程序,請參閱這裏的文檔(http://swiftmailer.org/docs/message-attachments) – xzyfer 2011-03-04 11:45:00

1

有兩種選擇:

  1. 之上創建
  2. 從數據CSV文件中的數據創建一個Excel電子表格上面

默認情況下,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/

這也是值得注意的是,如果你生成.xls文件,然後你的PHP將需要在運行安裝Excel副本的窗口(我不會建議這個,如果你想用Excel選項,那麼你可能要考慮創建。xlsx文件與我鏈接到的PHPExcel工具,這不需要安裝Excel副本,因此它可以部署在Linux上)

編輯:要將文件作爲附件通過電子郵件發送給用戶PHPMailer很容易這可以找到here,你可以下載類here

+0

PHPExcel也可以創建BIFF8 .xls文件以及較新的Office Open Spreadsheet .xlsx文件。 – 2011-03-04 11:48:44

+0

是的,抱歉...我的意思是,最好使用xlsx文件擴展名,因爲您不必依靠COM對象與Excel進行交互 – JamesHalsall 2011-03-04 11:49:51

+0

csv選項看起來像我想要的,它好像省下了將數據作爲csv文件並將其保存到服務器。我怎樣才能讓這個文件通過電子郵件發送到某個地址? – Rob 2011-03-04 14:18:26

相關問題