PHP

php mail() 첨부파일 메일 보내기 3탄_첨부파일 전송

raim-2 2023. 5. 30. 18:49
반응형

php mail() 첨부파일 메일 보내기 3탄_첨부파일 전송

 

드디어 3탄 첨부파일 전송하기!

 

물론, 완벽하진 않다. 전송된 메일을 확인하면 계속 맨 하단에 boundary가 보여짐..!

👉 그 전에는 안보였다가 이젠 파일 전송은 잘되는데, boundary가 문제🤦‍♀️ 

👉 그래도 메일 전송에는 문제가 안되므로 그냥 여기서 끝내야지!

 

나중에라도 아시는 분은 댓글 좀...(없으면 나중에 다시 시도해봐야지)

 

✔️ 첨부한 이미지, 첨부파일 형태로 메일 전송하기

1) 앞부분과 if문은 2탄에서 작성한 것과 동일하고, 파일 선택했을 때 적용되는 else 부분을 고쳐줘야 함

대신 2탄은 태그로 처리하느라 사용하지 않았던 boundary와 multipart를 사용한다.

 

👉 $boundary

👉 $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";

$name_01=$_POST['name'];
 $mail_02=$_POST['email'];
 $phone_03=$_POST['tel'];
 $type_04=$_POST['type'];
 $msg_05=$_POST['message'];
 $file_06=$_FILES['file']; //전달될 때 이진형태로 전달
 $agree_07=$_POST['agree'];

 $file_name=$_FILES['file']['name'];
 $file_size=$_FILES['file']['size'];
 $file_type=$_FILES['file']['type'];
 $file_tmp=$_FILES['file']['tmp_name']; //임시명 - 임시경로(서버 저장전)

// 파일 첨부가 없는 경우 기본 MIME 유형을 설정
if ($file_type == "") {
    $file_type = 'application/octet-stream';
};

//별도로 data 파일에 이미지 저장 안됨
$filename = basename($file_name);
$fp = fopen($file_tmp, "r"); // 임시 경로($file_tmp)를 사용합니다.
$file = fread($fp, $file_size);
fclose($fp);

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

 $to='earnestga@naver.com'; //master mail
 $subject='동아ST사이트에서 관리자에게 보낸 메일';
 $msg="보낸사람: $name_01<br>".
     "보낸사람 메일주소: $mail_02<br>".
     "보낸사람 전화번호: $phone_03<br>".
     "문의유형: $type_04<br>".
     "내용: $msg_05<br>".
     "개인정보 수집 및 이용동의: $agree_07<br>";

$attach_msg="<hr>첨부파일 이름: {$file_name}<br>".
        "첨부파일 크기: {$file_size}<br>".
        "첨부파일 타입: {$file_type}<br>".

$boundary = md5(uniqid(microtime()));

 

2) 첨부파일이 들어갈 본문의 Content-Type은 multipart/mixed로 처리하고, boundary를 추가한다

 

   $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";

 

3) boundary는 영역을 나눌 때 사용된다. $bodytext에서 msg가 들어갈 부분은 html로 처리한다.

 

   $bodytext .= "Content-Type: text/html; charset=UTF-8\r\n";
   $bodytext .= "Content-Transfer-Encoding: 8bit\r\n\r\n";

 

4) 첨부파일이 들어가는 $bodytext는 base64로 인코딩된 $attach 변수를 사용하므로, 
Content-Type은 $file_type을, 첨부파일 표시 방법은 attachment로 작성해준다.

 

   $bodytext .= "Content-Type: $file_type; name=\"$file_name\"\r\n";
   $bodytext .= "Content-Transfer-Encoding: base64\r\n";
   $bodytext .= "Content-Disposition: attachment; filename=\"$file_name\"\r\n\r\n";

 

if ($file_tmp == "") {
   $headers = "MIME-Version: 1.0\r\n";
   $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
   $headers .= "Content-Transfer-Encoding: 8bit\r\n\r\n";

   $bodytext = $msg;
} else {
   $headers = "MIME-Version: 1.0\r\n";
   $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
   $headers .= "Content-Transfer-Encoding: 8bit\r\n\r\n";

   $bodytext .= "--$boundary\r\n";
   $bodytext .= "Content-Type: text/html; charset=UTF-8\r\n";
   $bodytext .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
   $bodytext .= $msg . "\r\n\r\n";
   $bodytext .= $attach_msg . "\r\n\r\n";
   $bodytext .= "--$boundary\r\n";
   $bodytext .= "Content-Type: $file_type; name=\"$file_name\"\r\n";
   $bodytext .= "Content-Transfer-Encoding: base64\r\n";
   $bodytext .= "Content-Disposition: attachment; filename=\"$file_name\"\r\n\r\n";  //첨부파일 형태로 보여준다.
   $bodytext .= $attach. "\r\n\r\n";
   $bodytext .= "--$boundary--\r\n";
};

 

5) 뒷 부분은 2탄과 동일하게 처리하면 된다.

 

mail($to, $subject, $bodytext, $headers);  

echo "<script>
        alert('성공적으로 메일이 전송되었습니다.');
        //history.go(-1);
        location.href='./sub6_3.html' ;
</script>";

?>

 

이렇게 코드를 작성해주면, 아래와 같이 첨부파일을 포함한 형태로 메일이 온다😁