안녕하세요. 라임입니다.

이번 시간에는, 폼메일에 대한 실전 예제를 만들어 보도록 하겠습니다. 

 

1. 다음과 같이 html 과 php 파일을 각각 생성해줍니다.

--------------------------------------- formtest.html -----------------------------------

<form method='post' action='formtest.php'>
<table width="414">
 <tr>
  <td align="center" height="50" width="408" bgcolor="#FFFFFF" bordercolor="#FFFFFF" colspan="2">
  <b><font size="2">폼메일 테스트 </font></b></td>
 </tr>
 <tr>
  <td align="center" height="22" width="90" bgcolor="#7481AB">
  <font color="#FFFFFF">이름</font></td>
  <td width="316">
  <input type="text" name="name" maxlength="35" style="height:20; width:160" size="35"></td>
 </tr>
 <tr>
  <td align="center" height="22" width="90" bgcolor="#7481AB">
  <font color="#FFFFFF">성별</font></td>
  <td width="316">
  <input type="radio" value="male" name="sex">남자
  <input type="radio" value="female" name="sex" > 여자</td>
 </tr>
 <tr>
  <td align="center" height="22" width="90" bgcolor="#7481AB">
  <font color="#FFFFFF">직업</font></td>
  <td width="316">
  <select size="1" name="job" style="width:100">
  <option selected>직장인</option>
  <option>주부</option>
  <option>학생</option>
  <option>기타</option>
  </select></td>
 </tr>
 <tr>
  <td align="center" height="22" width="90" bgcolor="#7481AB">
  <font color="#FFFFFF">이메일</font></td>
  <td width="316">
  <input type="text" name="email" maxlength="35" style="height:20; width:160" size="35"></td>
 </tr>
 <tr>
  <td align="center" height="22" width="90" bgcolor="#7481AB">
  <font color="#FFFFFF">제목</font></td>
  <td width="316">
  <input type="text" name="subject" maxlength="60" style="height:20; width:316" size="60"></td>
 </tr>
 <tr>
  <td height="172" bgcolor="#7481AB">
  <p align="center"><font color="#FFFFFF">추가내용</font></td>
  <td height="172"><textarea name="content" rows="13" cols="49"></textarea></td>
 </tr>
 <tr>
  <td align="center" height="50" colspan="2">
  <input type="submit" value="입 력" style="width=100;height:24px">  <input type="reset" value="취  소" style="width=100;height:24px"></td>
 </tr>
</table>

</form>


----------------------------formtest.php-----------------------------------

<?

$tomail = "xxxx@naver.com"; //이 폼메일을 받을 메일주소


function error($text){
 echo "
  <script language=javascript>
  window.alert('$text')
  history.go(-1)
  </script>";
 exit;
}

function msg($text){
 echo "
  <script language=javascript>
  window.alert('$text')
  top.location.href = 'formtest.html'
  </script>
 ";
 exit;
}

if (!$name) {error('성명을 적어주세요');} // 이름이 없을때 에러 메세지
if (!$sex) {error('성별을 선택해 주세요');}

if (!$email) {error('메일 주소를 적어주세요');} // 메일주소가 없을때 에러 메세지
if (!$subject) {error('제목을 적어주세요');} // 제목이 없을때 에러 메세지

$mailheaders = "Return-Path: $email \r\n"; // 메일 헤더의 반송 메일 주소
$mailheaders .= "From: $name <$email>\r\n"; // 메일헤더의 이름과 메일 주소 표시

$body = " 이름 : $name \r\n";
$body .= " 메일주소 : $email \r\n";
$body .= " 성별 : $sex \r\n";
$body .= " 직업 : $job \r\n";
$body .= " 내 용 : $content \r\n";

$result=mail($tomail , $subject , $body , $mailheaders); // 메일 전송

if($result) {msg('메일이 성공적으로 발송되었습니다.');} // 전송 성공시

else{error('메일 발송에 실패하였습니다.');} // 전송 실패시

?>


------------------------------------------------------------------------------

2. 자, 아래와 같이 만들어 졌습니다.

1_thks10.jpg

3. 이제 서버에 올려야겠죠?

저는 알FTP를 사용하여 "\html\formtest" 에 업로드 하겠습니다.

2_thks10.jpg

4. 자, 아래와 같이 창이 뜹니다.

주소는 "http:// [서버/호스트명 or 도메인명] / [호스트디렉토리] / [파일명]" 입니다.

저는 http://raimsoft.com/formtest/formtest.html 로 브라우징된 화면이 아래입니다.^^

3_thks10.jpg

5. 이제 여기다 무언가 적어볼까요?

다 적은 후에 입력 버튼을 눌러줍니다.

4_thks10.jpg

"메일이 성공적으로 발송되었습니다." 라는 메세지 창이 뜹니다.

5_thks10.jpg

6. 어디 한번 볼까요?

정말 왔군요!~

7_thks10.jpg

8_thks10 (1).jpg

※ 안되는 경우

#1

3Line의 메일 주소를 수정하지 않은 경우

6_thks10.jpg

formtest.html에서 이메일 텍스트 박스 양식은 단순히 POST값을 전송 받을 뿐 입니다. (발송과는 무관합니다.) 이 부분을 적어주셔야 올바르게 작동합니다.

#2

파일명을 무작위로 수정한 경우

이 예제에서는 formtest로 연동되게 되어 있습니다.

formtest.html ↔ formtest.php

소스코드 안에서 formtest에 대한 부분을 찾아서

해당 파일명으로 변경 하시거나, 파일 이름을 코드에 알맞게 변경 해주셔도 됩니다.

※간혹 가다 저에게 테스트 메일을 보내시는 분이 계신데,

소스 상에서 위 3Line 부분이 해당 데이터가 전송 되는 메일 주소이며, html상에서

이메일을 텍스트박스에 적는 부분은 단순히 자료를 입력받는 부분일 뿐입니다.