Created
March 17, 2014 16:16
-
-
Save eliasfaical/9602454 to your computer and use it in GitHub Desktop.
wp-mail-smtp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( $_SERVER['REQUEST_METHOD'] == "POST" ){ | |
send_contact_form(); # envia o form | |
} else { | |
?> | |
<form action="" method="post" id="form-contato" class="form"> | |
<div class="col-md-4"> | |
<div class="input-group"><input type="text" name="nome" class="form-control" placeholder="Nome"></div> | |
<div class="input-group"><input type="text" name="email" class="form-control" placeholder="E-mail"></div> | |
<div class="input-group"><input type="text" name="telefone" class="form-control" placeholder="Telefone" id="telefone" maxlength="15"></div> | |
<div class="input-group"><input type="text" name="cpf_cnpj" class="form-control" placeholder="CPF ou CNPJ"></div> | |
<div class="input-group"><input type="text" name="assunto" class="form-control" placeholder="Assunto"></div> | |
<?php | |
echo $_SESSION['msg']; | |
unset( $_SESSION['msg'] ); | |
?> | |
</div> | |
<div class="col-md-4"> | |
<div class="input-group"><textarea name="mensagem" class="form-control" placeholder="Mensagem"></textarea></div> | |
<div class="input-grroup"><input type="submit" value="Enviar" class=""></div> | |
</div> | |
</form> | |
<?php } ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PHP mailer - precisa do plugin WP-mail-smtp | |
function send_contact_form(){ | |
require(ABSPATH . WPINC . '/class-phpmailer.php'); | |
require(ABSPATH . WPINC . '/class-smtp.php'); | |
date_default_timezone_set('America/Sao_Paulo'); | |
$str_nome = trim($_POST['nome']); | |
$str_email = trim($_POST['email']); | |
$str_telefone = trim($_POST['telefone']); | |
$str_cpf_cnpj = trim($_POST['cpf_cnpj']); | |
$str_assunto = trim($_POST['assunto']); | |
$str_mensagem = trim($_POST['mensagem']); | |
$siteurl = trailingslashit( get_option('home') ); | |
$str_conteudo = " | |
<p>Foi feito um contato atrás do site {$siteurl}</p> | |
<strong>Nome: </strong>{$str_nome} | |
<br /><br /> | |
<strong>E-mail: </strong>{$str_email} | |
<br /><br /> | |
<strong>Telefone: </strong>{$str_telefone} | |
<br /><br /> | |
<strong>CPF/CNPJ: </strong>{$str_cpf_cnpj} | |
<br /><br /> | |
<strong>Assunto: </strong>{$str_assunto} | |
<br /><br /> | |
<strong>Mensagem: </strong>{$str_mensagem} | |
"; | |
$mail = new PHPMailer(true); | |
$mail->IsSMTP(); | |
$mail->SMTPDebug = false; | |
$mail->SMTPAuth = true; | |
$mail->Host = get_option('smtp_host'); | |
$mail->Port = get_option('smtp_port'); | |
$mail->Username = get_option('smtp_user'); | |
$mail->Password = get_option('smtp_pass'); | |
$mail->From = get_option('smtp_user'); | |
$mail->FromName = get_option('blogname'); | |
$mail->Subject = "Contato - " . get_option('blogname'); | |
$mail->Body = $str_conteudo; | |
$mail->CharSet = "UTF-8"; | |
$mail->IsHTML(true); | |
$mail->AddAddress( get_option('smtp_user'), $siteurl); | |
# sending... | |
if ($mail->Send()) { | |
$mail->ClearAllRecipients(); | |
$_SESSION['msg'] = '<div class="alert alert-success">Mensagem enviado com sucesso!</div>'; | |
$_POST = array(); | |
wp_redirect( site_url( 'contato') ); | |
exit; | |
} | |
else { | |
$mail->ClearAllRecipients(); | |
$_SESSION['msg'] = '<div class="alert alert-danger">Erro ao enviar a mensagem!</div>'; | |
$_POST = array(); | |
wp_redirect( site_url( 'contato') ); | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment