Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End php mail with gmail HELP Reply To: php mail with gmail HELP

#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-&gt;response_html .= '<p>Favor preencher o campo NOME</p>';
        $this-&gt;response_status = 0;
    }

    // Check email
    if(!$this-&gt;email)
    {
        $this-&gt;response_html .= '<p>Favor preencher o campo EMAIL</p>';
        $this-&gt;response_status = 0;
    }

    // Check valid email
    if($this-&gt;email &amp;&amp; !$this-&gt;validateEmail())
    {
        $this-&gt;response_html .= '<p>Favor preencher o campo com email válido</p>';
        $this-&gt;response_status = 0;
    }

    // Check message length
    if(!$this-&gt;message || strlen($this-&gt;message) message_min_length)
    {
        $this-&gt;response_html .= '<p>Favor digitar sua mensagem. Ela deverá conter no mínimo '.$this-&gt;message_min_length.' characters</p>';
        $this-&gt;response_status = 0;
    }
}

private function sendEmail(){
    $mail = mail($this-&gt;email_admin, $this-&gt;subject, $this-&gt;message,
         "From: ".$this-&gt;name." email."&gt;\r\n"
        ."Reply-To: ".$this-&gt;email."\r\n"
    ."X-Mailer: PHP/" . phpversion());

    if($mail)
    {
        $this-&gt;response_status = 1;
        $this-&gt;response_html = '<p>Obrigado!</p>';
    }
}


function sendRequest(){
    $this-&gt;validateFields();
    if($this-&gt;response_status)
    {
        $this-&gt;sendEmail();
    }

    $response = array();
    $response['status'] = $this-&gt;response_status;   
    $response['html'] = $this-&gt;response_html;

    echo json_encode($response);
}

}

$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();

?>