2012-02-05 2 views
-1

첨부 파일로 여러 단어 문서가 포함 된 이메일을 보내는 기능이 있습니다. 단지 문제는 내가 보낸 문서의 수에 관계없이 이메일에 1 개만 표시되고 손상된 것으로 표시된다는 것입니다. 파일 경로 이름이 정확합니다.이 테스트를 거쳤습니다.하지만 전자 메일에서 문서를 열려고하면 Microsoft Word에서 손상된 파일에 대해 불만을 제기합니다. 누군가 내가 뭘 잘못하고 있는지 말해줘?PHP에서 첨부 파일로 여러 단어 문서 보내기

function mail_attachment_multiple($to, $subject, $message, $attachment, $from){//sends email as an attachment. attachment parameter is the path to file 

$fileatt_type = "application/msword"; // File Type 
$email_from = $from; // Who the email is from 
$email_subject = $subject; // The Subject of the email 
$email_txt = $message; // Message that the email has in it 
$email_to = $to; // Who the email is to 
$headers = "From: ".$email_from; 
$msg_txt="\n\n You have recieved a new attachment message from $from"; 

$email_message = ""; 

//loop through array: $key = filename; $value = filenamePATH 
foreach($attachment as $key => $value){ 
    $fileatt_name = $key; //name of file 
    $fileatt_path = $value; // file path (http://www.example.com/filePath) 

    $file = fopen($fileatt_path,'rb'); 
     $temp = get_headers($fileatt_path, 1); 
    $file_size = $temp['Content-Length'];   
    $data = fread($file, $file_size); //filesize($fileatt_path) 
    fclose($file); 

    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
    $headers .= "\nMIME-Version: 1.0\n" . 
     "Content-Type: multipart/mixed;\n" . 
     " boundary=\"{$mime_boundary}\""; 
    $email_txt .= $msg_txt; 
    $email_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" . 
    $email_txt . "\n\n"; 
    $data[$key] = chunk_split(base64_encode($data));   

    $email_message .= "--{$mime_boundary}\n" . 
       "Content-Type: {$fileatt_type};\n" . 
       " name=\"{$fileatt_name}\"\n" . 
       //"Content-Disposition: attachment;\n" . 
       //" filename=\"{$fileatt_path}\"\n" . 
       "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" ."--{$mime_boundary}--\n"; 
} 

return @mail($email_to, $email_subject, $email_message, $headers);?> 

감사합니다.

+2

아니요. 직접 해결하십시오. Swiftmailer 나 PHPmailer로 잘 작동하는 대안이있을 때는 아무도 패치를하지 말아야합니다. 스파게티 코드를 사용하는 것은 귀하의 결정입니다. – mario

답변

1

정말로은 이런 종류의 메일 라이브러리를 사용해야합니다. 나는 SwiftMailer을 강력히 추천한다.

+0

나는 그것으로 볼 것이다. 감사! 전에 PHPmailer를 사용 해본 적이 있습니까? 그렇다면 왜 PHP 메일러보다 SwiftMailer를 추천합니까? – Johny

+0

@Johny 전에 PHPMailer를 보지 않았습니다. Symfony와 Doctrine을 담당하는 Fabien Potencier/Senio Labs가 부분적으로 관리하고 있기 때문에 SwiftMailer를 추천합니다. 이것을 바탕으로 SwiftMailer는 알맞은 코드 기반과 단위 테스트를 수행 할 것입니다. – Cobby

관련 문제