2013-03-18 3 views
0

안녕하세요, 저는 이메일에 양식을 사용하여로드 된 파일을 첨부하는 데 문제가있어서 글쓰기를하고 있습니다. 폴더에 첨부하기 전에 저장해야하는지 이해하지 못했습니다 .... 이것은 내 코드이며 메일은 도착하지만 첨부 파일이 없습니다. 누군가 내가 어디 잘못 됐는지 말해 줄래?메일 첨부 파일에 첨부 파일

$allegato=$_FILES['userfile']['tmp_name']; 
$allegato_name=$_FILES['userfile']['name']; 
$allegato_tipo=$_FILES['userfile']['type']; 
$uploaddir = '/uploads/'; 
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']); 
$headers = 'From: '.$email.'' . "\r\n" . 
     'Reply-To: [email protected]' . "\r\n" . 
     'X-Mailer: PHP/' . phpversion(); 

      ; 

if ($_FILES["userfile"]["error"] > 0){ 
     echo "Return Code: " . $_FILES["userfile"]["error"] . "<br>"; 
    }else{ 
    if (file_exists("uploads/" . $_FILES["userfile"]["name"])){ 
     echo $_FILES["userfile"]["name"] . " already exists. "; 
    }else{ 
     move_uploaded_file($_FILES["userfile"]["tmp_name"], 
     "uploads/" . $_FILES["userfile"]["name"]); 
     echo "Stored in: " . "uploads/" . $_FILES["userfile"]["name"]; 
    } 
    } 
    if(is_uploaded_file($allegato)){ 
     $file = fopen($allegato,'rb'); 
     $data = fread($file, filesize($allegato)); 
     fclose($file); 

     $data = chunk_split(base64_encode($data)); 

     $semi_rand = md5(time()); 
     $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

     $headers .= "\nMIME-Version: 1.0\n"; 
     $headers .= "Content-Type: multipart/mixed;\n"; 
     $headers .= " boundary=\"{$mime_boundary}\""; 

     $msg .= "This is a multi-part message in MIME format.\n\n"; 

     $msg .= "--{$mime_boundary}\n"; 

     $msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; 
     $msg .= "Content-Transfer-Encoding: 7bit\n\n"; 
     $msg .= $messaggio . "\n\n"; 

     $msg .= "--{$mime_boundary}\n"; 

     $msg .= "Content-Disposition: attachment;\n"; 
     $msg .= " filename=\"{$allegato_name}\"\n"; 
     $msg .= "Content-Transfer-Encoding: base64\n\n"; 
     $msg .= $data . "\n\n"; 

     $msg .= "--{$mime_boundary}--\n 
    }else{ 
     $msg = $messaggio; 
    } 

     if (mail($destinatario, $oggetto, $msg, $headers)){ 
      echo "<p>Mail inviata con successo!</p>"; 
     }else{ 
      echo "<p>Errore!</p>"; 
     } 

/FINE SCRIPT는/

 mail($destinatario, $oggetto, $messaggio, $headers) ; 
+0

_Please_ 메일러 클래스를 사용하십시오. 전자 메일은 꽤 복잡한 주제이고, 필요한 모든 부분을 "손으로"함께 모으고 PHP 메일()을 사용하는 것은 자기가하는 일을 정확하게 모르는 경우 어렵습니다. – CBroe

+0

PHPMailer와 같은 것을 사용하지 않는 이유는 무엇입니까? – Pitchinnate

+0

파일의 내용을 메일에 저장 했으므로 파일을 이동하거나 저장할 필요가 없습니다. Pitchinnate처럼 PHPMailer를 사용하여 쉽게 첨부하거나 삽입 할 수 있습니다. – Waygood

답변

0

나는 당신이 첨부 파일로 받고 때 PHPMailer를 사용하는 것이 좋습니다 것입니다. 간단한 부착 예 : 당신이 복잡한 작업을 위해 같은 Mailerclass Swiftmailer를 사용하는 경우

<?php 
    require_once ('../class.phpmailer.php'); 

    $mail = new PHPMailer(); 
    // defaults to using php "mail()" 

    $mail -> IsSendmail(); 
    // telling the class to use SendMail transport 

    $body = file_get_contents('contents.html'); 
    $body = preg_replace('/[\]/i', '', $body); 

    $mail -> SetFrom('[email protected]', 'First Last'); 

    $mail -> AddReplyTo("[email protected]", "First Last"); 

    $address = "[email protected]"; 
    $mail -> AddAddress($address, "John Doe"); 

    $mail -> Subject = "PHPMailer Test Subject via Sendmail, basic"; 

    $mail -> AltBody = "To view the message, please use an HTML compatible email viewer!"; 
    // optional, comment out and test 

    $mail -> MsgHTML($body); 

    $mail -> AddAttachment("images/phpmailer.gif"); 
    // attachment 
    $mail -> AddAttachment("images/phpmailer_mini.gif"); 
    // attachment 

    if (!$mail -> Send()) { 
     echo "Mailer Error: " . $mail -> ErrorInfo; 
    } else { 
     echo "Message sent!"; 
    } 
?> 
+0

괜찮아, 지금 해보 라. –

+0

OP를 위해 그들은 $ mail -> AddStringAttachment ($ _ FILES [ "userfile"] [ "tmp_name"]), $ _FILES [ "userfile"] [ "name"])); 파일을 추가하고 원래 이름을 유지하기 위해 – Waygood

+0

예, 보았습니다, 이제 모든 작품! 고마워요.... 궁극적 인 문제는 내가 얻고 자하는 파일의 유형을 정의하는 것입니다 ... phpmailer에서 필터링 할 수있는 기능이 있습니까? –