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.
- página: index.php
- <?php
- if (!empty($_GET['erro'])) {
- $erro = $_GET['erro'];
- }
- else {
- $erro = "";
- }
- switch ($erro) {
- case "nome";
- echo "Nome não informado";
- break;
- case "email";
- echo "e-mail não informado";
- break;
- case "assunto";
- echo "assunto não informado";
- break;
- case "texto";
- echo "digite uma mensagem";
- break;
- default:
- echo "";
- break;
- }
- ?>
- <form method="post" action="contato_envia.php">
- <table width="300" border="0" bordercolor="#000000" cellpadding="0" cellspacing="0" align="center">
- <tr>
- <td width="60"></td><td width="240"></td>
- </tr>
- <tr>
- <td>Nome:</td><td><input type="text" size="40" maxlength="40" name="frm_nome"></td>
- </tr>
- <tr height="10">
- <td></td><td></td>
- </tr>
- <tr>
- <td>e-mail:</td><td><input type="text" size="40" maxlength="40" name="frm_email"></td>
- </tr>
- <tr height="10">
- <td></td><td></td>
- </tr>
- <tr>
- <td>Telefone:</td><td><input type="text" size="3" maxlength="3" name="frm_telefone2">
- <input type="text" size="31" maxlength="30" name="frm_telefone"></td>
- </tr>
- <tr height="10">
- <td></td><td></td>
- </tr>
- <tr>
- <td>assunto:</td><td><input name="frm_assunto" type="text" id="frm_assunto" size="40" maxlength="40" /></td>
- </tr>
- <tr height="10"><td></td><td></td></tr>
- <tr>
- <td>mensagem:</td><td><textarea cols="42" rows="10" name="frm_texto"></textarea></td>
- </tr>
- <tr height="10"><td></td><td></td></tr>
- <tr>
- <td></td>
- <td><input type="button" value="Limpar" onclick="window.open('contato.php?erro=0','_self')">
- <input type="submit" value="Enviar"></td>
- </tr>
- </table>
- </form>
- página: contato_envia.php
- <?php
- function retorna($erro) {
- echo "<script language='JavaScript'>";
- echo "window.open('contato.php?erro=" . $erro . "','_self')";
- echo "</script>";
- }
- if (!empty($_POST['frm_nome'])) {
- $nome = $_POST['frm_nome'];
- }
- else {
- retorna(nome);
- exit;
- }
- if (!empty($_POST['frm_email'])) {
- $email = $_POST['frm_email'];
- }
- else {
- retorna(email);
- exit;
- }
- if (!empty($_POST['frm_telefone'])) {
- $telefone = $_POST['frm_telefone'];
- }
- else {
- $telefone = "";
- }
- if (!empty($_POST['frm_telefone2'])) {
- $telefone_pre = $_POST['frm_telefone2'];
- $telefone_pre = "(" . $telefone_pre . ")";
- }
- else {
- $telefone_pre = "";
- }
- if (!empty($_POST['frm_assunto'])) {
- $assunto = $_POST['frm_assunto'];
- }
- else {
- retorna(assunto);
- exit;
- }
- if (!empty($_POST['frm_texto'])) {
- $texto = $_POST['frm_texto'];
- }
- else {
- retorna(texto);
- exit;
- }
- ?>
- <?php
- $mensagem = "Mensagem de ". $nome . " <" . $email . ">" . "<br>" . $telefone_pre . " " . $telefone ."<br><br>" . $texto;
- $headers .= " $email <$email>rn";
- $headers .= "MIME-Version: 1.0rn";
- $headers .= "Content-type: text/html; charset=iso-8859-1rn";
- mail("email_que_recebe",$assunto,$mensagem,"From: ".$headers)
- or die ("<p align='center'><font size='3'>Erro ao enviar o email</font><br><br>
- <a href='contato.php?erro=0'>clique aqui para voltar</a></p>");
- ?>
- <link href="frm_estilo.css" rel="stylesheet" type="text/css">
- <br><br><br><br>
- <p align="center"><font size="3">Seu pedido foi enviado com sucesso!</font><br><br>
- <a href="contato.php?erro=0">clique aqui para voltar</a></p>
0 comentários