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

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #145298
    SkyTown
    Participant

    Ok so here is my current php code for my contact form.

    This is the back end:

    <?php /* * Contact Form Class */
    
        header('Cache-Control: no-cache, must-revalidate');
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Content-type: application/json');
    
        $admin_email = '[email protected]'; // Your Email
        $message_min_length = 5; // Min Message Length
    
    
        class Contact_Form{
            function __construct($details, $email_admin, $message_min_length){
    
                $this->name = stripslashes($details['name']);
                $this->email = trim($details['email']);
                $this->subject = 'Contact from Your Website'; // Subject
                $this->message = stripslashes($details['message']);
    
                $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 .= '

    Please enter your name

    '; $this->response_status = 0; } // Check email if(!$this->email) { $this->response_html .= '

    Please enter an e-mail address

    '; $this->response_status = 0; } // Check valid email if($this->email && !$this->validateEmail()) { $this->response_html .= '

    Please enter a valid e-mail address

    '; $this->response_status = 0; } // Check message length if(!$this->message || strlen($this->message) < $this->message_min_length) { $this->response_html .= '

    Please enter your message. It should have at least '.$this->message_min_length.' characters

    '; $this->response_status = 0; } } private function sendEmail(){ $mail = mail($this->email_admin, $this->subject, $this->message, "From: ".$this->name." <".$this->email.">\r\n" ."Reply-To: ".$this->email."\r\n" ."X-Mailer: PHP/" . phpversion()); if($mail) { $this->response_status = 1; //$this->response_html = '

    Thank You!

    '; $this->response_html = $mail; } } 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(); ?>

    I’m getting a thank you, but the email never reaches my inbox. Can you guys help me out? The live page that this is used on can be found at : http://www.inthecloudscreative.com .

    #145420
    thechrisroberts
    Participant

    Many shared hosts restrict where email can come from – the from address has to be something hosted with them. You might modify your code to make from use a hosted domain and just include the person’s name and email address as part of the message.

    #145437
    SkyTown
    Participant

    Thanks. Thats exactly what was up. Thanks!

    #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-&gt;email_admin = $email_admin;
        $this-&gt;message_min_length = $message_min_length;
    
        $this-&gt;response_status = 1;
        $this-&gt;response_html = '';
    }
    
    
    private function validateEmail(){
        $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';
    
        if($this-&gt;email == '') { 
            return false;
        } else {
            $string = preg_replace($regex, '', $this-&gt;email);
        }
    
        return empty($string) ? true : false;
    }
    
    private function validateFields(){
        // Check name
        if(!$this-&gt;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();

    ?>

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.