개발 Q&A

제목 mail 함수로 메일보내기 질문드립니다.
글쓴이 Siam 작성시각 2015/03/13 14:00:23
댓글 : 3 추천 : 0 스크랩 : 0 조회수 : 19477   RSS
안녕하세요 예전에 가입해서 눈팅만 하다가 궁금한점이 생겨서 질문 드리게 됐습니다.
php에 함수 중 mail함수를 이용해서 첨부파일이 있는 형식의 메일을 보내려고 소스를 짰습니다.

테스트를 하는데 메일이 발송은 잘됩니다.
근데 유독 한메일에서만 메일 제목만 보이고 본문 내용은 아무것도 안뜨더라구요.
그래서 구글, 네이버로 보내면 정상적으로 전송이 됩니다.

다음으로 메일 보내고 네이버에서 다음메일 끌어와서 보면 보이구요.
다음 고객센터에 문의를 보냈는데 감감 무소식인지라 너무 답답해서 질문글을 올리게 됐습니다.

혹시 이런현상 겪어보신분 도움좀 부탁드리겠습니다.

소스코드 : 
<?php
function mail_fsend($receiver, $title, $sender, $contents, $filepath,$filename, $filesize, $filetype) {
    $msg = '';
    $title = $title; // 제목지정
    $msg .= 't >  '.$title.'/ ';
    $mailheaders = "Return-Path: ".$sender."\r\n"; // 회신주소
    $mailheaders .= "From: ".$sender."\r\n"; //보낸이 주소
    $mailheaders .= "X-Mailer: multimail\r\n"; //메일 방식 설정
    $boundary = "----".uniqid("part"); // 구분자 설정

    
    
    $upfile_name = $filename;
    $upfile_tmpname = $filename;
    $upfile_size = $filesize;
    $upfile_type = $filetype;
    //파일명, 파일크기 모두 존재 할 시
    if ($upfile_tmpname && $upfile_size) {
        $msg .= ' have file ';
        //$filename = basename($filepath);
        $msg .= '읽기시작 /';
        //파일의 내용을 읽어서 file_contents에 저장
        $result = fopen($filepath, 'r');
        $file_contents=fread($result,filesize($filepath));
        $msg .= '읽기성공 /';
        fclose($result);
        if ($upfile_type == "") {
            //파일타입이 없을 경우 자동으로 파일타입지정
            $userfile_type = "application/octet-stream";
            $msg .= 'have not filetype';
        }

        // 메일 전송을 위한 MIME 버전 명시
        $mailheaders .= "MIME-Version: 1.0\r\n";
        //헤더의 타입을 Multipart/mixed 로설정해야 첨부파일 보낼 수 있음
        $mailheaders .= "Content-Type: Multipart/mixed; boundary = \"$boundary\"";


        $bodytext = "This is a multi-part message in MIME format.\r\n\r\n";
        $bodytext .= "--$boundary\r\n"; //구분
        //본문은 text/html 타입으로 설정, 캐릭터셋은 euc-kr
        $bodytext .= "Content-Type: text/html; charset=EUC-KR\r\n";
        //암호화 인코딩 명시
        $bodytext .= "Content-Transfer-Encoding: base64\r\n\r\n";
        //본문내용을 암호화시킴
        $bodytext .= base64_encode($contents)."\r\n\r\n";
        
        // 첨부파일에 대한 처리 시작
        $bodytext .= "--$boundary\r\n";
        //파일타입은 해당 파일의 확장자, 이름 명시
        $bodytext .= "Content-Type: $upfile_type; name=\"$upfile_name\"\r\n";
        //암호화 인코딩 명시
        $bodytext .= "Content-Transfer-Encoding: base64\r\n";
        //타입을 attachment로 설정하여 첨부파일 인걸 명시
        $bodytext .= "Content-Disposition: attachment; filename=\"$upfile_name\"\r\n\r\n";
        //첨부파일 암호화
        $bodytext .= base64_encode($file_contents)."\r\n\r\n";
        
        // 마지막 구분
        $bodytext .= "--$boundary--";
    } else { //첨부 파일이 없을경우
        
        $mag = 'have not file srt';
        // 메일 전송을 위한 MIME 버전 명시 헤더의 타입을 첨부파일 없는 형식으로 설정
        $mailheaders .= "MIME-Version: 1.0\r\n";
        $mailheaders .= "Content-Type: Multipart/alternative; boundary = \"$boundary\"";

        
        $bodytext .= "--$boundary\r\n";
        // 본문내용 text/html타입으로 설정, 캐릭터셋 설정
        $bodytext .= "Content-Type: text/html; charset=\"ks_c_5601-1987\"\r\n";
        // 암호화 인코딩 명시, 본문내용 암호화 
        $bodytext .= "Content-Transfer-Encoding: base64\r\n\r\n";
        $bodytext .= base64_encode($contents)."\r\n\r\n";

        // 마지막 구분
        $bodytext .= "--$boundary--\r\n\r\n";
    }
    //메일 전송 함수 호출, 리턴값 true or false
    $mailresult = mail($receiver, $title, $bodytext, $mailheaders);
    //리턴값을 기준으로 msg에 succ 또는 fail 문자열을 추가하 
    if($mailresult) {
        $msg .= 'succ';
    }else {
        $msg .= 'fail';
    }
    // msg 리턴
    return $msg;
}
?> 
 다음글 Aptana에서 선택된 텍스트 블럭과 같은 단어를 강조... (2)
 이전글 이미지 드래그로 키우기 (2)

댓글

ci세상 / 2015/03/13 17:49:03 / 추천 0
제 경험으로는 html 태그가 잘못된 것이 있을때 발생하는 현상 같습니다.
Siam / 2015/03/16 09:03:57 / 추천 0
@ci세상 음 .. 그말씀은 애초에 발송될때 태그가 잘못돼서 빈 메일이 발송된다는 뜻이신거죠?
다음쪽에서 받는게 문제가아니라..?
변종원(웅파) / 2015/03/16 10:30:29 / 추천 0
빈메일이 아니라 메일서버에 따라 설정이 달라서 다른데서 읽는 메일을 에러라고 처리할 수도 있습니다.

다음으로 간 메일을 네이버로 끌고가면 나온다고 하니 빈 내용이 가는건 아니죠.

메일은 워낙 많은 요인이 있어서 나오는 에러나 경고메시지를 검색해서 하나씩 해결해가세요.