폼메일 전송 안됨
본문
test_form.html
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Cache-Control" content="No-Cache" />
<meta http-equiv="Pragma" content="No-Cache" />
<title>메일쓰기</title>
<script src="//code.jquery.com/jquery.min.js"></script>
<script src="//cdn.ckeditor.com/4.4.7/full/ckeditor.js"></script>
<script type="text/javascript">
$(document).ready(function(){
if(parent.resize_layer) parent.resize_layer($('#cBody').height());
CKEDITOR.config.width = 770;
CKEDITOR.config.height = 440;
CKEDITOR.config.removePlugins = 'elementspath,resize';
CKEDITOR.config.toolbar = [
{ name: 'styles', items: [ 'Styles', 'Font', 'FontSize' ] },
{ name: 'colors', items: [ 'TextColor', 'BGColor' ] },
{ name: 'tools', items: [ 'Maximize', 'ShowBlocks' ] },
{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat' ] },
{ name: 'insert', items: [ 'Image', 'Table', 'HorizontalRule', 'SpecialChar' ] }
];
CKEDITOR.config.removeButtons = 'Source,Save,NewPage,Print,Templates,Find,Replace,SelectAll,Scayt,HiddenField,Outdent,Indent,Blockquote,CreateDiv,BidiLtr,BidiRtl,Language,Anchor,Flash,Smiley,PageBreak,Iframe,Format,Subscript,Superscript,About';
var editObj = CKEDITOR.replace( 'message', { filebrowserUploadUrl: 'upload.php', contentsCss:'p{margin:0;}' } );
editObj.on('key', function(evt){
var cLen = editObj.getData().length;
if(cLen > 65000) {
alert('65000자를 넘을 수 없습니다.');
editObj.execCommand('undo');
}
else {
editObj.fire('saveSnapshot');
}
});
});
function mail_check(v, n) {
var regEmail = /\w{2,}[@][\w\-]{2,}([.]([\w\-]{2,})){1,3}$/;
var tmps = new Array();
if(v.indexOf(',') > -1) {
tmps = v.split(',');
}
else if(v.indexOf(';') > -1) {
tmps = v.split(';');
}
else {
tmps.push(v);
}
for(var i=0; i<tmps.length; i++) {
if(tmps[i].indexOf('<') > -1 && tmps[i].indexOf('>') > -1) {
tmps[i] = tmps[i].substring(tmps[i].indexOf('<')+1, tmps[i].indexOf('>'));
}
if(!regEmail.test(tmps[i])) {
return false;
}
}
if(n == 1 && tmps.length != 1) {
return false;
}
return true;
}
function mail_proc(f) {
if(!f.femail.value) {
alert('보내는이 메일 주소를 입력하세요.');
f.femail.focus();
return false;
}
if(!mail_check(f.femail.value, 1)) {
alert('이메일 주소가 유효하지 않습니다.');
f.femail.focus();
return false;
}
if(!f.temail.value) {
alert('받는이 메일 주소를 입력하세요.');
f.temail.focus();
return false;
}
else {
if(!mail_check(f.temail.value)) {
alert('메일 주소가 유효하지 않습니다.');
f.temail.focus();
return false;
}
}
if(f.remail.value) {
if(!mail_check(f.remail.value)) {
alert('메일 주소가 유효하지 않습니다.');
f.remail.focus();
return false;
}
}
if(f.hemail.value) {
if(!mail_check(f.hemail.value)) {
alert('메일 주소가 유효하지 않습니다.');
f.hemail.focus();
return false;
}
}
if(!f.subject.value) {
alert('제목을 입력하세요.');
f.subject.focus();
return false;
}
var editObj = CKEDITOR.instances.message;
if(editObj.getData().length == 0) {
alert('메일 내용을 입력하세요.');
editObj.focus();
return false;
}
f.target = 'frame_mail_proc';
f.submit();
return false;
}
</script>
</head>
<body id="cBody">
<div id="cLayer" style="width:100%;height:auto;padding-left:20px;padding-top:20px;padding-bottom:5px;font-size:10pt;font-family:돋움;" align="left">
<div id="mailLayer" style="width:100%;height:auto;" align="center">
<div style="width:770px;padding-bottom:10px;font-size:12pt;font-weight:bold;" align="left">
폼메일 쓰기
</div>
<table>
<form name="mailForm" method="post" enctype="multipart/form-data" action="test_mail.php" onsubmit="return mail_proc(this);">
<tr>
<td width="110" align="left">보내는이</td>
<td align="left"><input type="text" name="femail" style="width:658px;" value="<?=$femail?>" /></td>
</tr>
<tr>
<td width="110" align="left">받는이</td>
<td align="left"><input type="text" name="temail" style="width:658px;" value="*** 개인정보보호를 위한 이메일주소 노출방지 ***" /></td>
</tr>
<tr>
<td width="110" align="left">참조</td>
<td align="left"><input type="text" name="remail" style="width:658px;" /></td>
</tr>
<tr>
<td width="110" align="left">숨은참조</td>
<td align="left"><input type="text" name="hemail" style="width:658px;" /></td>
</tr>
<tr>
<td width="110" align="left">제목</td>
<td align="left"><input type="text" name="subject" style="width:658px;" /></td>
</tr>
<tr>
<td width="110" align="left">파일첨부</td>
<td align="left"><input type="file" name="attach" /></td>
</tr>
<tr>
<td colspan="2" height="2"></td>
</tr>
<tr>
<td colspan="2" align="left">
<textarea name="message"><?=$message?></textarea>
</td>
</tr>
<tr>
<td colspan="2" height="15"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="메일발송" onclick="return mail_proc(this.form);" />
</td>
</tr>
</form>
</table>
<div style="display:none;">
<iframe src="" name="frame_mail_proc" id="frame_mail_proc" frameborder=0 margin=0 allowTransparency=false width="100%" height="100%"></iframe>
</div>
</div>
</div>
</body>
</html>
test_mail.php
<?php
$MAXLIMIT = 5242880; // 5MByte limit
if($_FILES['attach']['size'] > $MAXLIMIT) {
$ment = "파일 사이즈가 ".($MAXLIMIT/1024/1024)."MByte를 초과하였습니다.\\n";
$ment .= ($MAXLIMIT/1024/1024)."MByte 이하의 파일만 첨부가능합니다.";
echo "<script>alert('".$ment."');</script>";
return;
}
if($_FILES['attach']) {
$attach = $_FILES['attach'];
$name = preg_replace("/\.(php|phtm|htm|cgi|pl|exe|jsp|asp|inc)/i", "$0-x", $attach['name']);
$name = substr(md5(uniqid($_SERVER['REQUEST_TIME'])),0,8).'_'.str_replace('%', '', urlencode($name));
$ATTACH_DIR = "./attach/mail/";
$dest_file = $ATTACH_DIR.$name;
$url = $ATTACH_DIR.$name;
if(!is_dir($ATTACH_DIR)) {
if(@mkdir($ATTACH_DIR, 0777, true)) {
if(is_dir($ATTACH_DIR)) {
@chmod($ATTACH_DIR, 0777);
}
}
}
if(move_uploaded_file($attach['tmp_name'], $dest_file)) {
$attach_url = $url;
$attach_name = $attach['name'];
$attach_size = $attach['size'];
$attach_type = $attach['type'];
}
}
$femail = $_POST['femail'];
$temail = $_POST['temail'];
$remail = $_POST['remail'];
$hemail = $_POST['hemail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$boundary = "----" . uniqid("part"); // 구분자
// --- 헤더 --- //
$headers = "Return-Path: ".str_replace("\\", "", $femail)."\r\n";
$headers .= "From: ".str_replace("\\", "", $femail)."\r\n";
if($remail) {
$headers .= "Cc: ".str_replace("\\", "", $remail)."\r\n";
}
if($hemail) {
$headers .= "Bcc: ".str_replace("\\", "", $hemail)."\r\n";
}
if($attach_url && $attach_name) { // --- 첨부파일 --- //
$filename=$attach_name;
$fp = fopen($attach_url,"r");
$file = fread($fp,$attach_size);
fclose($fp);
if ($attach_type == ""){
$attach_type = "application/octet-stream";
}
// --- 헤더 --- //
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: Multipart/mixed; boundary=\"$boundary\"";
// --- 본문 --- //
$mailbody = "This is a multi-part message in MIME format.\r\n\r\n";
$mailbody .= "--$boundary\r\n";
$mailbody .= "Content-Type: text/html; charset=utf-8\r\n";
$mailbody .= "Content-Transfer-Encoding: base64\r\n\r\n";
$mailbody .= chunk_split(base64_encode(str_replace("\\", "", $message))) . "\r\n";
// --- 첨부 --- //
$mailbody .= "--$boundary\r\n";
$mailbody .= "Content-Type: ".$attach_type."; name=\"".$filename."\"\r\n";
$mailbody .= "Content-Transfer-Encoding: base64\r\n";
$mailbody .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$mailbody .= base64_encode($file)."\r\n\r\n";
$mailbody .= "--$boundary--";
}
else {
// --- 헤더 --- //
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: Multipart/alternative; boundary = \"$boundary\"";
// --- 본문 --- //
$mailbody = "--$boundary\r\n";
$mailbody .= "Content-Type: text/html; charset=utf-8\r\n";
$mailbody .= "Content-Transfer-Encoding: base64\r\n\r\n";
$mailbody .= chunk_split(base64_encode(str_replace("\\", "", $message))) . "\r\n";
$mailbody .= "--$boundary--\r\n\r\n";
}
$ret = mail($temail, $subject, $mailbody, $headers);
if(!$ret) {
$msg = "메일 발송을 실패하였습니다.";
}
else {
$msg = "메일을 발송하였습니다.";
}
?>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Cache-Control" content="No-Cache" />
<meta http-equiv="Pragma" content="No-Cache" />
<script>alert('<?=$msg?>');</script>";
</head>
</html>
https://itrooms.tistory.com/m/183
위 페이지를 참고하여 폼메일을 작성하여 몇년간 정상적으로 사용중이었습니다.
그런데 갑자기 어떤 메일도 전송되지 않는데 문제를 모르겠습니다ㅠㅠ
타 도메인에서 같은 폼메일 전송시 잘작동되고 있습니다ㅠㅠ
!-->!-->답변 1
타도메인에선 잘 되는데, 특정도메인만 안 되는 거면...
서버 apm 버전 특히 php 버전 차이 때문이지 않을까요.
잘 되는 서버 apm 버전
안 되는 서버 apm 버전
각각 어떻게 되는지?
답변을 작성하시기 전에 로그인 해주세요.