2013-10-31 5 views
2

비슷한 질문이 여기에 보관되어있는 것 같습니다. (나는 많은 것을 살펴 봤습니다.) Google은 내 친구입니다. (검색하고 검색했습니다). 당신이 충분히 열심히 노력하고 있다고 생각하지 않는다면 이것을 downvote해라. 그러나 나는 단지 그 과정에서 머리를 감싸는 것처럼 보이지 않을 것이고, 확실히 도움을받을 수있다.PHP 문의 양식 첨부 파일 업로드 첨부 파일

파일 업로드 옵션을 추가하려는 간단한 PHP 문의 양식 (Seen Here)이 있으므로 클라이언트가 중요한 문서를 첨부하고 PHP 메일 기능을 사용하여 메일을 보낼 수 있습니다.

양식 자체는 정상적으로 작동하지만 첨부 파일을 업로드하고 서버에 일시적으로 저장하고 전자 메일의 일부로 보내는 코드를 얻지 못하는 것 같습니다. 여기에 내가 사용하고 코드입니다 : 나는 현재이 스크립트를 실행하면

<?php 

if ($_POST['test'] != '') { 
    echo 'Unfortunately, by filling out the hidden field, you have been identified as a potential spambot and your message has been terminated.'; 
} else { 

//Validate the name: 
if (!empty($_POST['name'])) { 
    $name = $_POST['name']; 
} else { 
    echo "You forgot to enter your name.<br>"; 
} 

//Validate the phone: 
if (!empty($_POST['phone'])) { 
    $phone = $_POST['phone']; 
} else { 
    echo "You forgot to enter your phone number.<br>"; 
} 

//Validate the e-mail: 
if (!empty($_POST['email'])) { 
    $email = $_POST['email']; 
} else { 
    echo "You forgot to enter your e-mail.<br>"; 
} 

//Validate the message: 
if (!empty($_POST['message'])) { 
    $message = $_POST['message']; 
} else { 
    echo "You forgot to enter a message."; 
} 

if (!empty($_POST['name']) && !empty($_POST['phone']) && !empty($_POST['email']) && !empty($_POST['message'])) { 

    // Obtain file upload variables: 
    $attachment = $_FILES['attachment']['tmp_name']; 
    $attachment_type = $_FILES['attachment']['type']; 
    $attachment_name = $_FILES['attachment']['name']; 

    if (file($attachment)) { 
    // Read the file to be attached ('rb' = read binary): 
    $file = fopen($attachment,'rb'); 
    $data = fread($file,filesize($attachment)); 
    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: {$attachment_type};\n" . 
    " name=\"{$attachment_name}\"\n" . 
    //"Content-Disposition: attachment;\n" . 
    //" filename=\"{$attachment_name}\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . 
    $data . "\n\n" . 
    "--{$mime_boundary}--\n"; 
    } 

    $body = "$name\n$phone\n$email\n\n$message"; 
    mail("*@*.com", "Starcrest Escrow, Inc. Website - Real Property Sale", $body, $headers); 
    header("Location: confirm.html"); 
} 

} 

?> 

, 그것은 확인 페이지에 저를 전달는 있지만 전자 메일은 전혀 생성 될 나타납니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 모든 조언은 대단히 감사합니다! http://www.excellentwebworld.com/send-file-in-email-attachment-on-form-submit-using-php/

당신의 HTML 양식 입력이 URL에서 PHP 측과 작업을 일치하는지 확인하고이 해결 나타날 수 있습니다

+0

서버 오류가 발생하고 있습니까? $ 데이터가 첨부 파일의 내용으로 채워져 있습니까? –

+0

나는 HTML 형식의 정확한 스크립트를 가리키고 있지 않다는 것을 깨달았다. 이제 문제가 해결되어 모든 정보가 담긴 전자 메일을 보냅니다. 그러나 첨부 파일은 메시지의 맨 아래에 텍스트 데이터로 표시됩니다. 예를 들어, PDF 파일은 "JVBERi0xLjMKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29k"의 줄이됩니다. 어떤 생각인지 알 겠어? 감사! – 712Jefferson

+0

예! –

답변

0

코드는 여기에 PHP 코드의 편집 된 버전으로 보인다.

+1

게임에 약간 늦었지만 OP의 게시물을 보았고 링크를 따라 가면서 여전히 내 운이 없었습니다. 올바르게 첨부 된 파일 대신 쓰레기 텍스트. * 한숨 * – SMT

+0

슬프게도, @SMT, 나는 아직도 이것을 이해하지 못했다. 쓰레기 텍스트와 동일한 문제가 발생했습니다. 작동하는 것을 찾으면 알려주세요. 나는 운이없는 많은 다른 옵션을 시도했다. 엄청나게 짜증나! – 712Jefferson

+0

@SMT, 위의 내 대답을 참조하십시오! – 712Jefferson

0
<?php 

if (isset($_POST['txtEmail'])) { 

    // EDIT THE 2 LINES BELOW AS REQUIRED 

    $email_to = "[email protected]"; 
    $email_subject = "Subject"; 
    $email_from = "[email protected]"; 
    function died($error) 
    { 

     // your error code can go here 

     echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
     echo "These errors appear below."; 
     echo $error . "<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation expected data exists 

    if (!isset($_POST['txtName']) || !isset($_POST['txtEmail']) || !isset($_POST['txtAddress']) || !isset($_POST['txtContact']) || !isset($_POST['txtUpload'])) { 
     died('We are sorry, but there appears to be a problem with the form you submitted.'); 
    } 

    $name = $_POST['txtName']; // required 
    $email = $_POST['txtEmail']; // required 
    $address = $_POST['txtAddress']; // required 
    $contact = $_POST['txtContact']; // not required 
    $upload = $_POST['txtUpload']; // required 
    $email_message = "Form Details are below.\n\n"; 
    function clean_string($string) 
    { 
     $bad = array(
      "content-type", 
      "bcc:", 
      "to:", 
      "cc:", 
      "href" 
     ); 
     return str_replace($bad, "", $string); 
    } 

    $email_message.= "Full Name: " . clean_string($name) . "\n\n"; 
    $email_message.= "Address: " . clean_string($address) . "\n\n"; 
    $email_message.= "Email ID: " . clean_string($email) . "\n\n"; 
    $email_message.= "Contact No.: " . clean_string($contact) . "\n\n"; 
    $email_message.= "File: " . clean_string($upload) . "\n\n"; 

    // create email headers 

    $headers = 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email . "\r\n" . 'X-Mailer: PHP/' . phpversion(); 
    @mail($email_to, $email_subject, $email_message, $headers); 
?> 
    <!-- include your own success html here --> 

    Thank you for contacting us. We will be in touch with you very soon. 

    <?php 
} 

?> 
+0

PHP 위의 그것을 추가하는 방법 – Tezzo

+0

... OP의 질문에이 코드를 추가하는 방법을 묻는거야 ?? – SMT

+0

배트맨은 짧은 코드 조각 및 여러 줄에 대한 네 개의 공백은 http://stackoverflow.com/editing-help를 참조하십시오. 그러나 가이드 [answer] ** too **;) (기본적으로 : 코드를 설명하고 복사 한 경우 소스를 언급하십시오. 그것은 어딘가에서) – brasofilo