Home › Forums › Back End › php mail with gmail HELP › Reply To: php mail with gmail HELP
August 18, 2013 at 6:17 pm
#147301
helainefpsantos
Participant
I`m having troubles with the validating fields.
It always shows the error message if i didnt complete de fields.
Here is the code :
name = stripslashes($details[‘Nome’]);
$this->phone = stripslashes($details[‘Telefone’]);
$this->email = trim($details[‘Email’]);
$this->subject = ‘Fale Conosco – Kaloma’; // Subject
$this->message = stripslashes($details[‘Menssagem’]);
$this->email_admin = $email_admin;
$this->message_min_length = $message_min_length;
$this->response_status = 1;
$this->response_html = '';
}
private function validateEmail(){
$regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';
if($this->email == '') {
return false;
} else {
$string = preg_replace($regex, '', $this->email);
}
return empty($string) ? true : false;
}
private function validateFields(){
// Check name
if(!$this->name)
{
$this->response_html .= '<p>Favor preencher o campo NOME</p>';
$this->response_status = 0;
}
// Check email
if(!$this->email)
{
$this->response_html .= '<p>Favor preencher o campo EMAIL</p>';
$this->response_status = 0;
}
// Check valid email
if($this->email && !$this->validateEmail())
{
$this->response_html .= '<p>Favor preencher o campo com email válido</p>';
$this->response_status = 0;
}
// Check message length
if(!$this->message || strlen($this->message) message_min_length)
{
$this->response_html .= '<p>Favor digitar sua mensagem. Ela deverá conter no mínimo '.$this->message_min_length.' characters</p>';
$this->response_status = 0;
}
}
private function sendEmail(){
$mail = mail($this->email_admin, $this->subject, $this->message,
"From: ".$this->name." email.">\r\n"
."Reply-To: ".$this->email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
$this->response_status = 1;
$this->response_html = '<p>Obrigado!</p>';
}
}
function sendRequest(){
$this->validateFields();
if($this->response_status)
{
$this->sendEmail();
}
$response = array();
$response['status'] = $this->response_status;
$response['html'] = $this->response_html;
echo json_encode($response);
}
}
$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();
?>