사이트 내 전체검색
PHP
[php] 메일 함수(파일전송까지)
로빈아빠
https://cmd.kr/php/436 URL이 복사되었습니다.

본문

메일 함수(파일전송까지)

<?php 
function sendMail($to,$from,$title,$contents,$userfile,$userfile_name,$userfile_type,$userfile_size) { 
        // 메일을 보내기전에 필요필드에 대한 체크를 한다. 
        if(!$to) { 
                echo ("<script> 
                        window.alert('메일을 받을실분의 계정을 입력하세요!') 
                        history.go(-1) 
                        </script>"); 
                exit; 
        } 

//        if(!eregi("^[^@ ]+@[a-zA-Z0-9-.]+.+[a-zA-Z0-9-.]", $to)) { 
//                echo ("<script> 
//                        window.alert('받는사람의 E-Mail 주소형식이 틀립니다. [예] yourmail@server.domain') 
//                        history.go(-1) 
//                </script>"); 
//                exit; 
//        } 

        if(!$title) { 
                echo ("<script> 
                        window.alert('메일 제목을 입력하세요!') 
                        history.go(-1) 
                        </script>"); 
                exit; 
        } 

        // 메일의 header를 작성한다. 
        $mailheaders .= "Return-Path: $from\n"; 
        $mailheaders .= "From: $from\n"; 
        $mailheaders .= "X-Mailer: webmail\n"; 

        $boundary = "----".uniqid("part"); 

        if($userfile && $userfile_size) { 
                $filename = basename($userfile_name); 
                $fp = fopen($userfile, "r"); 
                $file = fread($fp, $userfile_size); 
                fclose($fp); 

                if($userfile_type == "") { 
                        $userfile_type = "application/octet-stream"; 
                } 

                // 헤더 파트를 작성한다. 
                // Multipart/mixed 일경우 첨부파일이 있다는 것을 의미한다. 
                $mailheaders .= "MIME-Version: 1.0\n"; 
                $mailheaders .= "Content-Type: Multipart/mixed; boundary = \"$boundary\""; 

                // 본문 파트를 작성한다. 
                $bodytext = "This is a multi-part message in MIME format.\n\n"; 
                $bodytext .= "--$boundary\n"; 
                $bodytext .= "Content-Type: text/html; charset=\"ks_c_5601-1987\"\n"; 
                $bodytext .= "Content-Transfer-Encoding: base64\n\n"; 
                $bodytext .= base64_encode(nl2br($contents))."\n\n"; 

                // 파일첨부 파트를 작성한다. 
                $bodytext .= "--$boundary\n"; 
                $bodytext .= "Content-Type: $userfile_type; name=\"$filename\"\n"; 
                $bodytext .= "Content-Transfer-Encoding: base64\n"; 
                $bodytext .= "Content-Disposition: attachment; filename=\"$filename\"\n\n"; 
                $bodytext .= base64_encode($file)."\n\n"; 

                // 멀티파트 종료를 작성한다. 
                $bodytext .= "--$boundary--"; 
        } else { 
                // 헤더 파트를 작성한다. 
                $mailheaders .= "MIME-Version: 1.0\n"; 
                $mailheaders .= "Content-Type: Multipart/alternative; boundary = \"$boundary\""; 

                // 본문 파트를 작성한다. 
                $bodytext .= "--$boundary\n"; 
                $bodytext .= "Content-Type: text/html; charset=\"ks_c_5601-1987\"\n"; 
                $bodytext .= "Content-Transfer-Encoding: base64\n\n"; 
                $bodytext .= base64_encode(nl2br($contents))."\n\n"; 

                // 멀티파트 종료를 작성한다. 
                $bodytext .= "--$boundary--\n\n"; 
        } 

        // 메일보내기에 성공하면 true, 실패하면 false를 리턴한다. 
        return mail($to, $title, $bodytext, $mailheaders); 
} 

echo "$userfile_name,$userfile_type,$userfile_size"; 
sendMail("tomail","frommail","test","asdfasdfasdfasdfasdf",$userfile,$userfile_name,$userfile_type,$userfile_size); 
?> 

댓글목록

등록된 댓글이 없습니다.

PHP
871 (11/18P)

Search

Copyright © Cmd 명령어 3.23.86.219