Error Email PHP Enconding

1

I have this web page where I have a form with which to send messages to the gmail of the owner of the page, to contact him. ..

link

Each time I send a test message the topic of the message comes out without encoding .

This is the code ... I do not know what the error

may be

Thanks for your help in advance

<?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 = utf8_encode('Página oficial de Jesús González de la Vega'); // Subject 
        $this->subject = "=?ISO-8859-15?Q?".imap_8bit('Página oficial de Jesús González de la Vega')."?=";

        $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 .= '<p>Por favor, introduce tu nombre</p>';
            $this->response_status = 0;
        }

        // Check email
        if(!$this->email)
        {
            $this->response_html .= '<p>Por favor, introduce una dirección de correo</p>';
            $this->response_status = 0;
        }

        // Check valid email
        if($this->email && !$this->validateEmail())
        {
            $this->response_html .= '<p>Por favor, introduce una dirección de correo válida</p>';
            $this->response_status = 0;
        }

        // Check message length
        if(!$this->message || strlen($this->message) < $this->message_min_length)
        {
            $this->response_html .= '<p>Por favor, introduce un mensaje. Debe tener al menos '.$this->message_min_length.' carácteres</p>';
            $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 = '<p>¡Gracias!</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();

?>
    
asked by Antonio 11.01.2017 в 14:51
source

1 answer

1
  • You could try coding the subject in utf8 .

    $this->subject = "=?UTF-8?B?" .
        base64_encode('Página oficial de Jesús González de la Vega') . "?=";
    
  • Or you can also set the character encoding conversion using iconv_set_encoding ( before doing anything).

    iconv_set_encoding("internal_encoding", "UTF-8");
    
answered by 11.01.2017 / 15:11
source