PHP로 Email 보내기
- result
html form 사용해서 post로 php에 정보
form method에 post인 것 명시하기. 안하면 get으로 넘어감
input태그 안 name이 php에서의 변수명이 되므로 꼭 표기해준다.
html 코드
<form id="touch" method="POST"> <label for="mail">Email address</label><br> <input type="email" name="emailTo" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" style=" width:100%" id ="mail" placeholder="Enter email" > <br><label for="subject">Subject</label><br> <input type="text" name="subject" id ="subject" style=" width:100%"> <br><label for="body">What would you like to ask us?</label><br> <textarea id ="body" name="body" style=" width:100%"></textarea> <button type="button" id="go" class="btn btn-primary">Submit</button> </form> </div>
php 코드
if($_POST){ $emailTo=$_POST["emailTo"];}
php의 mail() 함수 사용해서 email 보내기
html 코드
email 수신자, 제목, 내용, 발신자 (나는 수신자와 동일하게 ..)
<form id="touch" method="POST"> <label for="mail">Email address</label><br> <input type="email" name="emailTo" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" style=" width:100%" id ="mail" placeholder="Enter email" > <br><label for="subject">Subject</label><br> <input type="text" name="subject" id ="subject" style=" width:100%"> <br><label for="body">What would you like to ask us?</label><br> <textarea id ="body" name="body" style=" width:100%"></textarea> <button type="button" id="go" class="btn btn-primary">Submit</button> </form> </div>
php 코드
$_POST[“name”] 으로 html에서 넘어온 값을 받는다. 그리고 mail 함수 이용해서 메일 전송!
if($_POST){ $emailTo=$_POST["emailTo"]; $subject= $_POST["subject"]; $body= $_POST["body"]; $headers =$_POST["emailTo"]; mail($emailTo,$subject,$body,$headers ); }
전체 코드
<?php
if($_POST){
$emailTo=$_POST["emailTo"];
$subject= $_POST["subject"];
$body= $_POST["body"];
$headers =$_POST["emailTo"];
mail($emailTo,$subject,$body,$headers );
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<style>
</style>
</head>
<body>
<div class="container">
<h1>Get in touch!</h1>
<div class="alert alert-success" id="yes" role="alert" style ="display:none">
<h4 class="alert-heading">Your email is sent!</h4>
</div>
<div class="alert alert-danger" id="no" role="alert" style ="display:none"></div>
<form id="touch" method="POST">
<label for="mail">Email address</label><br>
<input type="email" name="emailTo" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" style=" width:100%" id ="mail" placeholder="Enter email" >
<br><label for="subject">Subject</label><br>
<input type="text" name="subject" id ="subject" style=" width:100%">
<br><label for="body">What would you like to ask us?</label><br>
<textarea id ="body" name="body" style=" width:100%"></textarea>
<button type="button" id="go" class="btn btn-primary">Submit</button>
</form>
</div>
<script type="text/javascript">
$("#go").click(function(){
var good = true;
$("#no").html("<strong>There were some errors</strong>");
if($("#mail").val()==""){
good=false
$("#no").html($("#no").html()+"<br>email is missing")
}
if($("#subject").val()==""){
good=false
$("#no").html($("#no").html()+"<br>subject is missing")
}
if($("#body").val()==""){
good=false
$("#no").html($("#no").html()+"<br>body is missing")
}
if(good){
$("#touch").submit();
$("#no").css("display","none");
$("#yes").css("display","block");
}else{
$("#no").css("display","block");
}
})
</script>
</body>
</html>