테스트 사이트 - 개발 중인 베타 버전입니다

폼메일 파일첨부하지않았는데 채택완료

인장이 1년 전 조회 1,881

폼메일로 파일첨부하면 메일로 잘 전송이 됩니다.

문제는  사용자가 파일을 첨부하지않았는데

의미없는 파일이 첨부되는 이유는 무엇일까요?

 

attach(1).txt 0KB  혹은 NONAME 0KB

 

 

</p>

<p><meta charset="utf-8">

<?php

 

if(isset($_POST['button']) && isset($_FILES['attachment']))

{

     $email_to = "aa@naver.com,bb@gmail.com"; 

    //Load POST data from HTML form

    $sender_name = $_POST["sender_name"]; //sender name

    $reply_to_email = $_POST["sender_email"]; //sender email, it will be used in "reply-to" header

    $subject     = $_POST["subject"]; //subject for the email

    $message     = $_POST["message"]; //body of the email

    $from_email  = $_POST["sender_email"]; //body of the email

    //$sender_email  = $_POST["sender_email"]; //body of the email

 

    /*Always remember to validate the form fields like this

    if(strlen($sender_name)<1)

    {

        die('Name is too short or empty!');

    }

    */   

    //Get uploaded file data using $_FILES array

    $tmp_name = $_FILES['attachment']['tmp_name']; // get the temporary file name of the file on the server

    $name     = $_FILES['attachment']['name']; // get the name of the file

    $size     = $_FILES['attachment']['size']; // get size of the file for size validation

    $type     = $_FILES['attachment']['type']; // get type of the file

    $error     = $_FILES['attachment']['error']; // get the error (if any)

 

    //validate form field for attaching the file

    /*

    if($error > 0)

    {

        die('Upload error or No files uploaded');

    }

    */

    //read from the uploaded file & base64_encode content

    $handle = fopen($tmp_name, "r"); // set the file handle only for reading the file

    $content = fread($handle, $size); // reading the file

    fclose($handle);                 // close upon completion

 

    $encoded_content = chunk_split(base64_encode($content));

    $boundary = md5("random"); // define boundary with a md5 hashed value

 

    //header

    $headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version

    $headers .= "From:".$from_email."\r\n"; // Sender Email

    $headers .= "Reply-To: ".$reply_to_email."\r\n"; // Email address to reach back

    $headers .= "Content-Type: multipart/mixed;"; // Defining Content-Type

    $headers .= "boundary = $boundary\r\n"; //Defining the Boundary

         

    </p>

<p>    

    //plain text

    $body = "--$boundary\r\n";

    $body .= "Content-Type: text/plain; charset=utf-8\r\n";

    $body .= "Content-Transfer-Encoding: base64\r\n\r\n";

    $body .= chunk_split(base64_encode($message));

 

         

    //attachment

    $body .= "--$boundary\r\n";

    $body .="Content-Type: ".$upfile_type."; name=\"".$filename."\"\r\n";   // 내용

    $body .="Content-Disposition: attachment; filename=".$name."\r\n";

    $body .="Content-Transfer-Encoding: base64\r\n";

    $body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n";

    $body .= $encoded_content; // Attaching the encoded file with email

     

    $sentMailResult = mail($email_to, $subject, $body, $headers);

 

    if($sentMailResult ){

        echo "문의접수가 완료되었습니다.";

        // unlink($name); // delete the file after attachment sent.

    }

    else{

        die("Sorry but the email could not be sent.

                    Please go back and try again!");

    }

}

?></p>

<p>

 

<!DOCTYPE html>

<html lang="ko">

<head>

    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <link rel="stylesheet" href="<a href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">" target="_blank" rel="noopener noreferrer">https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css"></a>

    <title>Send Attachment With Email</title>

</head>

<body>

    <div style="display:flex; justify-content: center; margin-top:10%;">

        <form enctype="multipart/form-data" method="POST" action="" style="width: 500px;">

            <div class="form-group">

                <input class="form-control" type="text" name="sender_name" placeholder="이름" required/>

            </div>

            <div class="form-group">

                <input class="form-control" type="email" name="sender_email" placeholder="메일주소" required/>

            </div>

            <div class="form-group">

                <input class="form-control" type="text" name="subject" placeholder="Subject"/>

            </div>

            <div class="form-group">

                <textarea class="form-control" name="message" placeholder="Message"></textarea>

            </div>

            <div class="form-group">

                <input class="form-control" type="file" name="attachment" placeholder="Attachment" />

            </div>

            <div class="form-group">

                <input class="btn btn-primary" type="submit" name="button" value="Submit" />

            </div>            

        </form>

    </div>

</body>

</html></p>

<p><strong>

댓글을 작성하려면 로그인이 필요합니다.

답변 1개

채택된 답변
+20 포인트
웅푸
1년 전

파일이 올바르게 첨부되었는지 재 또 확인하고, 만약에 이상이 없다면,

 파일이 첨부되지 않았을 때의 조건을 추가정의 해 보세요

로그인 후 평가할 수 있습니다

답변에 대한 댓글 7개

인장이
1년 전
파일이 첨부되지 않았을때의 조건을 알려주실수 있을까요?
사용자가 파일 첨부하지 않았을때 의미없는 파일명은 나오지 않게 하고 싶습니다.
웅프
1년 전
<?php
if(isset($_POST['button']) && isset($_FILES['attachment'])) {
if (!empty($_FILES['attachment']['name'])) {

$email_to = "*** 개인정보보호를 위한 이메일주소 노출방지 ***,*** 개인정보보호를 위한 이메일주소 노출방지 ***";
웅프
1년 전
$sentMailResult = mail($email_to, $subject, $body, $headers);

if($sentMailResult ){
echo "문의접수가 완료되었습니다.";
// unlink($name); // delete the file after attachment sent.
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
} else {
die('파일 첨부해 주시기 바랍니다.');
}
}
?>
인장이
1년 전
도움을 주셧는데 죄송합니다.
적용하였는데 그래도 메일에는 의미없는 파일이 전송되네요.
웅프
1년 전
그렇면 다른부분을 찾아 봐야 할것 같은데요, 오류내용이라던지. 디버그해서 찬찬히 들어야 봐야 할것 같습니다.
파일 처리 부분을 검토하여, 파일을 올리고 그 파일을 첨부하는 과정을 다시 살펴보시기 바랍니다. 파일 업로드, 파일 처리, 그리고 메일 첨부 과정을 차례로 디버깅하여 각 과정이 제대로 되는지도살펴보셔요. 답은 쉬운곳에 있을겁니다. 느림의 미학으로 찬찬히 확인 또확인 화이팅!~
웅프
1년 전
http://www.webmadang.net/develop/develop.do?action=read&boardid=1003&page=3&seq=29

https://sir.kr/g5_tip/16355?sfl=wr_subject%7C%7Cwr_content&stx=%EB%A9%94%EC%9D%BC+%ED%8C%8C%EC%9D%BC

참조해보세요
인장이
1년 전
네 감사합니다. :)

좋은 하루되세요.

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인