2013-06-13 1 views
0

필자는 양식을 작성하고 제출할 때 데이터를 CSV 파일로 만들고 특정 전자 메일 주소로 전자 메일을 보내고 싶습니다. 이 양식의 맨 아래에 파일 업로드 기능을 추가 할 수 있는지 여부를 아는 사람이 있습니까? 이것은 클라이언트 용이며 PHP에 대한 지식이 제한되어 있습니다. 너무 많은 문제에 갈 필요가 없습니다CSV 파일 형식으로 양식 데이터를 전자 메일로 보내기

<?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: [email protected]\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(); 
} 


} 


?> 
+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 파일을. 모든 변수가 필요 없습니다.

귀하의 우편 번호를 확인하지 않았습니다. 파일을 첨부하는 데 문제가있는 경우 다른 질문을하십시오. (이미 여기에 대한 답변은 이미 있음)

행운을 비네!

0

나는 asp와 asp.net에서이 작업을 수행했지만, PHP로 (서버에서 첨부 파일을 사용하여 메일을 보내는 것으로) 할 필요가 없었습니다.

이해해야 할 점은 양식을 제출할 때 데이터가 어딘가에서 처리되도록 전송된다는 것입니다. FormProcessor는 별도의 파일 또는 PHP 페이지 자체에있을 수 있습니다.

그래서 FormProcessor가 데이터를 수신하면이를 결합하거나 원하는대로 서식을 지정할 수 있습니다. 서버에 파일을 만들려면 파일 시스템에 액세스해야합니다. 전자 메일을 보내는 기능이 FormProcessor 범위에 있다고 가정 해 보겠습니다. 전자 메일 헤더를 사용하여 전자 메일 개체를 정의 할 때 첨부 파일을 추가 할 수 있습니다.

이 내가 여기 샘플로 사용하도록 수정 한 PHP에서 내 이메일 보내기 funcitons 중 하나입니다 : 그것은 첨부 파일을 보내지 않습니다

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; 
    } 

,하지만이에 다른 확실 사람이야 사이트를 통해 더 나은 방법을 찾을 수 있습니다.

희망이 있습니다.

관련 문제