0

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


}


?>
4

2 回答 2

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

我还没有检查你的邮件代码。如果您在附加文件时遇到问题,请就此提出不同的问题(尽管这里已经有毫无疑问的答案)

祝你好运!

于 2013-06-14T00:17:43.733 回答
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;
  }

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

希望能帮助到你。

于 2013-06-14T00:33:48.077 回答