电子邮件表单数据在CSV文件格式


Emailing form data in CSV file format

我有一个相当大的表单,在填写并提交后,我希望将数据形成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();
}

}

?>

没必要这么麻烦。以下内容可能会有所帮助:

$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文件就这样创建了。不需要所有这些变量。

我没有检查你的邮件编码。如果你在附加文件时遇到问题,请问一个不同的问题(尽管毫无疑问这里已经有答案了)

祝你好运!

我在asp和asp.net中这样做过,但从来没有在php中这样做过(即从服务器发送带有附件的邮件)。

需要理解的是,当你提交表单时,数据被发送到某个地方进行处理,比如:FormProcessor,它可能在一个单独的文件中,也可能在php页面本身。

所以当FormProcessor接收到数据时,你可以组合它或者以任何你喜欢的方式格式化它。您需要访问文件系统才能在服务器上创建文件。假设发送电子邮件的函数位于FormProcessor作用域中。在定义email对象时,可以使用email头来添加附件。

这是我在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;
  }

它不发送附件,但我相信这个网站上的其他人可以帮你找到更好的方法。

希望能有所帮助。