以CSV文件格式通过电子邮件发送表单数据

时间:2013-06-13 23:41:43

标签: php csv

我有一个相当大的表单,在填写和提交时,我希望将数据形成CSV文件并通过电子邮件发送到特定的电子邮件地址。 有谁知道是否可以在这个表格的底部添加文件上传功能? 这是针对客户端的,并且刚刚遇到了我对PHP的了解。

这是我正在使用的代码:

<?php

if(isset($_POST['formSubmit'])) {

$email=$_REQUEST['email']; 
$firstName=$_REQUEST['firstName']; 
$lastName=$_REQUEST['lastName']; 
$to = "***@***.co.uk"; 
$subject = "New application submission"; 


$message = "". 
"Email: $email" . "\n" . 
"First Name: $firstName" . "\n" . 
"Last Name: $lastName";   

//The Attachment    
$varTitle = $_POST['formTitle'];    
$varForname = $_POST['formForname'];
$varMiddlename = $_POST['formMiddlename'];
$varSurname = $_POST['formSurname'];
$varKnownas = $_POST['formKnownas'];
$varAdressline1 = $_POST['formAdressline1'];
$varAdressline2 = $_POST['formAdressline2'];
$varAdressline3 = $_POST['formAdressline3'];
$varAdressline4 = $_POST['formAdressline4'];
$varAdressline5 = $_POST['formAdressline5'];
$varPostcode = $_POST['formPostcode'];
$varTelephone = $_POST['formTelephone'];
$varMobile = $_POST['formMobile'];
$varEmail = $_POST['formEmail'];
$varApproval = $_POST['formApproval'];
$varothersurname = $_POST['formothersurname'];
$varsex = $_POST['formsex'];
$varninumber = $_POST['formninumber'];
$varjobtitle = $_POST['formjobtitle'];
$vardates = $_POST['formdates'];
$varresponsibilities = $_POST['formresponsibilities'];
$varjobtitle2 = $_POST['formjobtitle2'];
$vardates2 = $_POST['formdates2'];
$varresponsibilities2 = $_POST['formresponsibilities2'];
$varjobtitle3 = $_POST['formjobtitle3'];
$vardates3 = $_POST['formdates3'];
$varresponsibilities3 = $_POST['formresponsibilities3'];
$varwebsite = $_POST['formwebsite'];
$vartshirt = $_POST['formtshirt'];
$vardietary = $_POST['formdietary'];
$varpc = $_POST['formpc'];
$varmac = $_POST['formmac'];
$varlaptop = $_POST['formlaptop'];
$vardongle = $_POST['formdongle'];
$varediting = $_POST['formediting'];
$varsocial = $_POST['formsocial'];
$varphotography = $_POST['formphotography'];
$varfilming = $_POST['formfilming'];
$vartraining = $_POST['formtraining'];
$varexhibition = $_POST['formexhibition'];
$varspecial = $_POST['formspecial'];
$varhobbies = $_POST['formhobbies'];
$varphotography = $_POST['formphotography'];
$varfilming = $_POST['formfilming'];
$vartraining = $_POST['formtraining'];
$varexcel = $_POST['formexcel'];
$varbigpicture = $_POST['formbigpicture'];
$varcriminal = $_POST['formcriminal'];

$attachments[] = Array( 
'data' => $data, 
'name' => 'application.csv', 
'type' => 'application/vnd.ms-excel' ); 


 //Generate a boundary string 

$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

//Add the headers for a file attachment 


$headers = "MIME-Version: 1.0\n" . 
       "From: {$from}\n" . 
         "Cc: davidkirrage@gmail.com\n". 
       "Content-Type: multipart/mixed;\n" . 
       " boundary=\"{$mime_boundary}\""; 

//Add a multipart boundary above the plain message 


$message = "This is a multi-part message in MIME format.\n\n" . 
      "--{$mime_boundary}\n" . 
      "Content-Type: text/html; charset=\"iso-8859-1\"\n" . 
      "Content-Transfer-Encoding: 7bit\n\n" . 
      $text . "\n\n"; 

//Add sttachments 

foreach($attachments as $attachment){ 
$data = chunk_split(base64_encode($attachment['data'])); 
$name = $attachment['name']; 
$type = $attachment['type']; 

$message .= "--{$mime_boundary}\n" . 
      "Content-Type: {$type};\n" . 
      " name=\"{$name}\"\n" .               
      "Content-Transfer-Encoding: base64\n\n" . 
      $data . "\n\n" ; 


$message = "--{$mime_boundary}--\n"; 
mail($to, $subject, $message, $headers);
header('Location: http://bigpictest.co.uk/thanks.php');
exit();
}


}


?>

2 个答案:

答案 0 :(得分:1)

没有必要去做那么多麻烦。 以下内容可能有所帮助:

$filename="directory/csvfile.csv"; //specify filename and path
$keys=array_keys($_POST);     //get list of post keys
$file=fopen($filename,"w");   //open a csv file to write to;
fputcsv($file,$keys);         //write post keys as first line of CSV file.
fputcsv($file,$_POST);        //write post data as second line of csv file.
fclose($file);                //close the file.

这样就创建了您需要附加的csv文件。不需要所有这些变量。

我没有检查过您的邮件代码。如果您在附加文件时遇到问题,请提出一个不同的问题(虽然这里已经有无疑的答案)

祝你好运!

答案 1 :(得分:0)

我在asp和asp.net中完成了这个,但从来没有用php(即从服务器发送带附件的邮件)。

要理解的是,当您提交表单时,数据将被发送到某处处理,比方说:FormProcessor可能位于单独的文件中或php页面中。

因此,当FormProcessor接收数据时,您可以将其组合或以您喜欢的方式格式化。 您需要访问文件系统才能在服务器上创建文件。 假设发送电子邮件的功能在FormProcessor范围内。 定义电子邮件对象时,可以使用电子邮件标题添加附件。

这是我在php中发送的电子邮件功能之一,我已将其修改为用作示例:

function FormProcessor_SendEmail($v){ // $v is an array containing the data to use for sending the email
           // parameters = $v[0] = user email, $v[1] = business name,
           // $v[2] = address, $v[3] = city, $v[4] = state, $v[5] = zip code,
           // $v[6] = phone, $v[7] = message, $v[8] = service area
           $eol = "\r\n";
           $tb = "\t";
           $stb = "\t\t";

           // notify new registrant
           $to = $v[0];
           $subject = "This is the subject text";
           $message = "Date : " . $this->prepare_new_timestamp() . ".<br /><br />" . $eol;
           $message .= "Thank you for requesting more information about " . $this->get_sitename() . "<br /><br />" . $eol;
           $message .= "We will be contacting you as soon as possible<br />" . $eol;
           $message .= "with information that is tailored to your needs and interests.<br /><br />" . $eol;
           $message .= "We have received the following information from you:<br /><br />" . $eol;
           $message .= "Your Business Name: " . $v[1] . "<br />" . $eol;
           $message .= "Business Address: " . $v[2] . "<br />" . $eol;
           $message .= "City: " . $v[3] . "<br />" . $eol;
           $message .= "State: " . $v[4] . "<br />" . $eol;
           $message .= "Zip Code: " . $v[5] . "<br />" . $eol;
           $message .= "Phone: " . $v[6] . "<br />" . $eol;
           $message .= "Service Area: " . $v[8] . "<br />" . $eol;
           $message .= "Message: <br />" . $v[7] . "<br /><br />" . $eol;
           $message .= "If you have additional questions, please address them to: " . $this->get_support_email() . "<br />" . $eol;
           $message .= "or by phone at: " . $this->get_support_phone() . "<br /><br />" . $eol;
           $message .= "If you did not submit this request, please contact us and let us know<br />" . $eol;
           $message .= "so that we may take the proper actions necessary.<br /><br />" . $eol;
           $message .= "Sincerely,<br />" . $eol;
           $message .= "The Team at " . $this->get_sitename() . "<br />" . $eol;
           $headers = "MIME-Version: 1.0" . "\r\n";
           $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
           $headers .= 'From: sitename <' . $this->get_webmaster_email() . '>' . "\r\n";
           $headers .= 'Cc: webmaster <' . $this->get_webmaster_email() . '>' . "\r\n";
           mail($to,$subject,$message,$headers);
           return 1;
  }

它不会发送附件,但我确信此网站上的其他人可以帮助您找到更好的方法。

希望它有所帮助。