2010-07-14 7 views
2

서버에 "uploads"폴더를 만들고 HTML 양식의 사용자가 업로드 한 파일을 해당 폴더에 넣습니다. 그런 다음 폴더에서 가져 와서 전자 메일에 첨부하려고 시도하지만이 부분은 작동하지 않습니다. 여기 내 PHP입니다 :PHP를 사용하여 HTML 양식을 통해 전자 메일 첨부 파일 보내기

여기
<?php 
$name = $_POST['name']; 
$email = $_POST['email']; 
$form_subject = $_POST['form_subject']; 
$message = $_POST['message']; 


//File upload 

// Where the file is going to be placed 
$target_path = "uploads/"; 

/* Add the original filename to our target path. 
Result is "uploads/filename.extension" */ 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 

//End of file upload 


[email protected]$REMOTE_ADDR; 
$date=date("l, F j, Y, g:i a"); 

$to = "[email protected]"; 
$subject = "New data submitted!"; 
$body = "Here is the information submitted to 
www.polycysticliverdisease.com/html/pld_form.php 
from $ip on $date.\n\n 
--------------------------------\n\n 
name: $name \n\n 
email address: $email \n\n 
subject: $form_subject \n\n 
comment: $message"; 
//mail($to, $subject, $body); 

// Obtain file upload vars  
$fileatt  = $_FILES['uploadedfile']['tmp_name'];  
$fileatt_type = $_FILES['uploadedfile']['type'];  
$fileatt_name = $_FILES['uploadedfile']['name']; 

if (is_uploaded_file($fileatt)) {  
// Read the file to be attached ('rb' = read binary)  
$file = fopen($fileatt,'rb');  
$data = fread($file,filesize($fileatt));  
fclose($file); 

// Generate a boundary string  
$semi_rand = md5(time());  
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  

// Add the headers for a file attachment  
$headers .= "\nMIME-Version: 1.0\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/plain; charset=\"iso-8859-1\"\n" .  
      "Content-Transfer-Encoding: 7bit\n\n" .  
      $message . "\n\n"; 

// Base64 encode the file data  
$data = chunk_split(base64_encode($data)); 

// Add file attachment to the message  
$message .= "--{$mime_boundary}\n" .  
      "Content-Type: {$fileatt_type};\n" .  
      " name=\"{$fileatt_name}\"\n" .  
      "Content-Disposition: attachment;\n" .  
      " filename=\"{$fileatt_name}\"\n" .  
      "Content-Transfer-Encoding: base64\n\n" .  
      $data . "\n\n" .  
      "--{$mime_boundary}--\n";  
} 

// Send the message  
$ok = @mail($to, $subject, $body, $headers);  
if ($ok) {  
echo "<p>Mail sent! Yay PHP!</p>";  
} else {  
echo "<p>Mail could not be sent. Sorry!</p>";  
} 

?> 

내 HTML 파일 업로드 컨트롤 :

<input type="submit" name="submit" class="submit" /> 
<input type="file" name="uploadedfile" class="upload" /> 

감사합니다!

답변

2

메일 링 라이브러리를 사용하고 싶습니다. 어쩌면 Swiftmailer입니다. 스스로 프로그래밍하는 것은 가치가 없습니다 (물론 공부 목적으로하지 않는다면).

+0

정말 고마워요! 그것은 아름답게 작동했습니다. 나는 Swiftmailer를 사랑한다! 그러나 나는 내 이메일 몸을 설정할 때, 나는 텍스트와 변수가 혼합되어 사용 는 $ 메시지 -> setBody ('여기에서 www.polycysticliverdisease.com/html/pld_form.php 에 제출 된 정보입니다이름 : $ name \ n \ n 이메일 주소 : $ email \ n \ n 제목 : $ form_subject \ n \ n 의견 : $ form_message ','text/html '); 모든 변수가 선언되고 적절한 값이 지정됩니다. 하지만 '\ n \ n'은 보낸 이메일에 텍스트의 일부로 표시됩니다. 즉, 변수로 인식되지 않습니다. 왜? – vlevsha

+0

@vlevsha 작은 따옴표를 사용하고 있기 때문입니다. 큰 따옴표를 사용하십시오. – Unicron

+0

고맙습니다. 당신의 대답을보기 전에 했었습니다. :) 이제 더 심각한 문제가 있습니다. http://stackoverflow.com/questions/3290966/retrieving-a-file-name-to-attach-to-an-email-with-swiftmailer- and-php – vlevsha

0

SwiftMailer 또는 PHPMailer과 같은 사전 제작 된 메일러 패키지를 사용하십시오. 둘 다 쉽게 첨부 파일을 처리 할 수 ​​있습니다.

+0

고마워요! 나는 SwiftMailer를 선택했다. 그냥 좋아했습니다! – vlevsha

관련 문제