emersonbroga
8/31/2012 - 6:43 PM

Emerson Carvalho.com >> Formulário de contato em php + ajax (snippet 1)

Emerson Carvalho.com >> Formulário de contato em php + ajax (snippet 1)

<?php

$toName  = 'Emerson Carvalho';
$toEmail = 'abc@abc.com.br';
	

$name    = '';
$email 	 = '';
$phone   = '';
$subject = '';
$message = '';

$return  = array( 'status' => null, 'msg' => null);

if(isset($_POST['form']) && empty($_POST['form'])){
	
	
	$name    = trim($_POST['name']);
	$email 	 = trim(strtolower($_POST['email']));
	$phone   = trim($_POST['phone']);
	$subject = trim($_POST['subject']);
	$message = $_POST['message'];
	
	$async = ( isset($_POST['type'])  && $_POST['type'] == 'async' );
	
	
	//Format message
	$content = '<h1>Contato Site</h1>';
	$content.= sprintf('<strong>Nome</strong>: %s<br/>', $name);
	$content.= sprintf('<strong>Email</strong>: %s<br/>', $email);
	$content.= sprintf('<strong>Telefone</strong>: %s<br/>', $phone);
	$content.= sprintf('<strong>Assunto</strong>: %s<br/>', $subject);
	$content.= sprintf('<strong>Mensagem</strong>:<br/> %s<br/>', $message);
	$content.= sprintf('<small>enviado em: %s</small><br/>', date('d/m/Y - H:i:s'));
	
	if(isset($_SERVER['HTTP_REFERER']))
		$content.= sprintf('<small>%s</small><br/>', $_SERVER['HTTP_REFERER']);
	
	
	$newline = "\r\n";
	
	// To send HTML mail, the Content-type header must be set
	$headers  = 'MIME-Version: 1.0' . $newline;
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . $newline;

	// Additional headers
	$headers .= sprintf('To: %s <%s>', $toName, $toEmail ) . $newline;
	$headers .= sprintf('From: %s <%s>', $name, $email ) . $newline;
	
	// Mail it
	$sent = mail($toEmail, $subject, $content, $headers);
	if($sent){
		
		$name    = '';
		$email 	 = '';
		$message = '';
		$phone   = ''; 
		
		$return['status'] = 'ok';
		$return['msg']    = 'Contato enviado com sucesso';
		
	}else{
		$return['status'] = 'error';
		$return['msg']    = 'Contato enviado com sucesso';
	}
	
	if( $async )
	{
		echo json_encode($return);
		die();
	}

}

?>