Fomulário para enviar emails com PHP

16:06Fresh Young´s

Neste código será apresentado uma função bem simples para o envio de e-mails utilizando PHP.
  1. página: index.php
  2.  
  3. <?php
  4.  
  5. if (!empty($_GET['erro'])) {
  6. $erro = $_GET['erro'];
  7. }
  8. else {
  9. $erro = "";
  10. }
  11. switch ($erro) {
  12.  
  13. case "nome";
  14. echo "Nome não informado";
  15. break;
  16. case "email";
  17. echo "e-mail não informado";
  18. break;
  19. case "assunto";
  20. echo "assunto não informado";
  21. break;
  22. case "texto";
  23. echo "digite uma mensagem";
  24. break;
  25. default:
  26. echo "";
  27. break;
  28. }
  29. ?>
  30.  
  31. <form method="post" action="contato_envia.php">
  32. <table width="300" border="0" bordercolor="#000000" cellpadding="0" cellspacing="0" align="center">
  33. <tr>
  34. <td width="60"></td><td width="240"></td>
  35. </tr>
  36. <tr>
  37. <td>Nome:</td><td><input type="text" size="40" maxlength="40" name="frm_nome"></td>
  38. </tr>
  39.  
  40. <tr height="10">
  41. <td></td><td></td>
  42. </tr>
  43. <tr>
  44. <td>e-mail:</td><td><input type="text" size="40" maxlength="40" name="frm_email"></td>
  45. </tr>
  46. <tr height="10">
  47. <td></td><td></td>
  48. </tr>
  49. <tr>
  50. <td>Telefone:</td><td><input type="text" size="3" maxlength="3" name="frm_telefone2">
  51. <input type="text" size="31" maxlength="30" name="frm_telefone"></td>
  52. </tr>
  53. <tr height="10">
  54. <td></td><td></td>
  55. </tr>
  56. <tr>
  57. <td>assunto:</td><td><input name="frm_assunto" type="text" id="frm_assunto" size="40" maxlength="40" /></td>
  58. </tr>
  59. <tr height="10"><td></td><td></td></tr>
  60. <tr>
  61. <td>mensagem:</td><td><textarea cols="42" rows="10" name="frm_texto"></textarea></td>
  62. </tr>
  63. <tr height="10"><td></td><td></td></tr>
  64. <tr>
  65. <td></td>
  66. <td><input type="button" value="Limpar" onclick="window.open('contato.php?erro=0','_self')">&nbsp;
  67. <input type="submit" value="Enviar"></td>
  68. </tr>
  69. </table>
  70. </form>
  71.  
  72.  
  73. página: contato_envia.php
  74.  
  75. <?php
  76.  
  77. function retorna($erro) {
  78.  
  79. echo "<script language='JavaScript'>";
  80.  
  81. echo "window.open('contato.php?erro=" . $erro . "','_self')";
  82.  
  83. echo "</script>";
  84.  
  85. }
  86.  
  87. if (!empty($_POST['frm_nome'])) {
  88. $nome = $_POST['frm_nome'];
  89. }
  90. else {
  91. retorna(nome);
  92. exit;
  93. }
  94. if (!empty($_POST['frm_email'])) {
  95. $email = $_POST['frm_email'];
  96. }
  97. else {
  98. retorna(email);
  99. exit;
  100. }
  101. if (!empty($_POST['frm_telefone'])) {
  102. $telefone = $_POST['frm_telefone'];
  103. }
  104. else {
  105. $telefone = "";
  106. }
  107. if (!empty($_POST['frm_telefone2'])) {
  108. $telefone_pre = $_POST['frm_telefone2'];
  109. $telefone_pre = "(" . $telefone_pre . ")";
  110. }
  111. else {
  112. $telefone_pre = "";
  113. }
  114. if (!empty($_POST['frm_assunto'])) {
  115. $assunto = $_POST['frm_assunto'];
  116. }
  117. else {
  118. retorna(assunto);
  119. exit;
  120. }
  121. if (!empty($_POST['frm_texto'])) {
  122. $texto = $_POST['frm_texto'];
  123. }
  124. else {
  125. retorna(texto);
  126. exit;
  127. }
  128. ?>
  129.  
  130. <?php
  131.  
  132. $mensagem = "Mensagem de ". $nome . "&nbsp;&lt;" . $email . "&gt" . "<br>" . $telefone_pre . "&nbsp;&nbsp;" . $telefone ."<br><br>" . $texto;
  133.  
  134. $headers .= " $email <$email>rn";
  135. $headers .= "MIME-Version: 1.0rn";
  136. $headers .= "Content-type: text/html; charset=iso-8859-1rn";
  137.  
  138.  
  139. mail("email_que_recebe",$assunto,$mensagem,"From: ".$headers)
  140. or die ("<p align='center'><font size='3'>Erro ao enviar o email</font><br><br>
  141. <a href='contato.php?erro=0'>clique aqui para voltar</a></p>");
  142.  
  143. ?>
  144.  
  145. <link href="frm_estilo.css" rel="stylesheet" type="text/css">
  146.  
  147. <br><br><br><br>
  148.  
  149. <p align="center"><font size="3">Seu pedido foi enviado com sucesso!</font><br><br>
  150. <a href="contato.php?erro=0">clique aqui para voltar</a></p>

You Might Also Like

0 comentários

Popular Posts

Formulário de Contacto