2009-09-29 12 views
1

첨부 파일을 보낼 수있는 전자 메일 양식을 만들려고합니다. 내 PHP 책과 여러 웹 사이트에서 코드를 하나로 결합했습니다.PHP로 첨부 파일 보내기

이 양식을 사용하여 전자 메일을 보내면 첨부 파일이 손상되어 "$ comments"가 통과하지 못하는 것 같습니다. 여기

나는 코드를 사용하고 있습니다 :

그래서
<?php 
$to = $_POST['to']; 
$from = $_POST['from']; 
$re = $_POST['re']; 
$comments= $_POST['comments']; 

$att = $_FILES['att']; 
$att_path= $_FILES['att']['tmp_name']; 
$att_name = $_FILES['att']['name']; 
$att_size = $_FILES['att']['size']; 
$att_type = $_FILES['att']['type']; 

//open, read, then close the file 
$fp = fopen($att_path, "rb"); 
$file = fread($fp, $att_size); 
fclose($fp); 

#create a boundary string 
$num = md5(time()); 
$str = "==Multipart_Boumdary_x{$num}x"; 

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

//define header 
$hdr = "MIME-Versio: 1.0\r\n"; 
$hdr .= "Content-Type: multipart/mixed; "; 
$hdr .= "boundary=\"{$str}\"\r\n"; 
$hdr .= "From: $from \r\n"; 

#define message 
$msg = "This is a multi-part message in MIME format\r\n\n"; 
$msg .= "--{$str}\r\n"; 
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; 
$msg .= "Content-Transfer-Encoding: 8bit\r\n"; 
$msg .= "$comments\r\n\n"; 
$msg .= "--{$str}\r\n"; 

#define the non-text attatchment 
$msg .= "Content-Type: {$att_type}; "; 
$msg .= "name=\"{$att_name}\"\r\n"; 
$msg .= "Content-Disposition: attachment; "; 
$msh .= "filename=\"{$att_name}\"\r\n"; 
$msg .= "Content-Transfer-Encoding: base64\r\n"; 
$msg .= "$file\r\n\n"; 
$msg .= "--{$str}"; 

#sendmail 
$ok = mail($to, $re, $msg, $hdr); 
if($ok) echo "OK"; 

?> 

, 내가 무슨 일을하고 있는가?

감사합니다.

+0

는 Mail_Mime 클래스를 참조하십시오를 PEAR 프로젝트, http://pear.php.net/package/Mail_Mime –

답변

0

를 사용하여 찾을 거라고 생각합니다.

라인 교체 :

$file = chunk_split(base64_encode($file)); 

으로 :

//Encode the file for transit 
    $content = ''; 
    $fp = fopen($att_path, 'r'); 
    do { 
    $data = fread($fp, 8192); 
    if (strlen($data) == 0) break; 
    $content .= $data; 
    } while (true); 
    $file = chunk_split(base64_encode($content)); 
+0

그 코드는 첨부 파일을 메시지 텍스트에 넣는 것처럼 보입니다. – lanrat

2

당신에게 더 나은 사용 http://swiftmailer.org/ 또는 http://phpmailer.worxware.com/

+0

Swift Mailer는 멋지지만,이 제안은 내 PHP 기술을 향상시키기위한 것이므로, 내가 잘못했는지 알고 싶습니다. 그래서 개선 할 수 있습니다. – lanrat

+0

당신은 그들의 소스를 확인할 수 있습니다, 그들은 깊이 모든 종류의 첨부 파일을 추가하는 편리한 방법이 있습니다. – Kane

0
$str = "==Multipart_Boumdary_x{$num}x"; 

희망 당신이 시도했다가 (경계의 철자를 통지) :

$str = "==Multipart_Boundary_x{$num}x"; 
+0

고마워, 나는 그것을 고쳤지 만 나는 여전히 같은 문제를 겪고있다. – lanrat