0

I have contact form but it's everything is excellent except letters čšćžđ. I change unicode of html and everything, but maybe problem is in the php. I have couple of forms one for ordering and it's working excellent with this letters but this one is killing me :). Can you maybe take a look and see problem beacuse I'm blind on this form.

    <?php   
    // Your Email
    $recipient = "#";

    // Check $recipient
    if($recipient === '') {
        returnAndExitAjaxResponse(
            constructAjaxResponseArray(
                FALSE,
                'RECIPIENT_IS_NOT_SET',
                array('error_message'=> 'RECIPIENT email address is not set. Please configure the script.')
            )
        );
    }

    // Check for empty required field
    if(!isset($_POST["email"]) || !isset($_POST["fname"]) || !isset($_POST["message"])) {
        returnAndExitAjaxResponse(
            constructAjaxResponseArray(
                FALSE,
                'MISSING_REQUIRED_FIELDS',
                array('error_message'=> 'MISSING_REQUIRED_FIELDS should not be occurred.')
            )
        );
    }

    // Sanitize input
    $fname  = filter_var($_POST["fname"], FILTER_SANITIZE_STRING);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = filter_var($_POST["message"], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);

    // If non required fields are empty
    if ( empty($lname) ){
        $lname = "Molimo Unesite Vaše Ime i Prezime.";
    }

    // Headers
    $headers = 'Mail od: '.$fname.' <'.$email.'>' . "\r\n";
    $headers .= 'Reply-To: '.$email.'' . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion();

    // Subject
    $subject = "Nova poruka od kontakte forme HR Bocce";

    // Build Message
    $email_content = "Ime i prezime: $fname\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Poruka:\n$message\n\n\n";
    $email_content .= "CLIENT IP:\n".get_client_ip()."\n";

// Check if sent
try {
    $sendmailResult = mail($recipient, $subject, $email_content, $headers);
    if( $sendmailResult === TRUE ) {
        returnAndExitAjaxResponse(
            constructAjaxResponseArray(
                TRUE
            )
        );
    } else {
        returnAndExitAjaxResponse(
            constructAjaxResponseArray(
                FALSE,
                'ERROR_AT_PHPMAIL',
                array('error_information'=> error_get_last() )
            )
        );
    }
} catch (Exception $_e) {
    returnAndExitAjaxResponse(
        constructAjaxResponseArray(
            TRUE,
            'ERROR_AT_PHPMAIL',
            array('error_message'=> $_e->getMessage())
        )
    );
}

/*
    Construct ajax response array
    Input: Result (bool), Message (optional), Data to be sent back in array
*/
function constructAjaxResponseArray ($_response, $_message = '', $_json = null) {
    $_responseArray = array();
    $_response = ( $_response === TRUE ) ? TRUE : FALSE;
    $_responseArray['response'] = $_response;
    if(isset($_message)) $_responseArray['message'] = $_message;
    if(isset($_json)) $_responseArray['json'] = $_json;

    return $_responseArray;
}
/*
    Returns in the Gframe ajax format.
    Input: data array processed by constructAjaxResponseArray ()
    Outputs as a html stream then exits.
*/
function returnAndExitAjaxResponse ($_ajaxResponse) {
    if(!$_ajaxResponse){
        $_ajaxResponse = array('response'=>false,'message'=>'Unknown error occurred.');
    }
    header("Content-Type: application/json; charset=utf-8");
    echo json_encode($_ajaxResponse);
    die();
}


// Function to get the client IP address
function get_client_ip() {
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    } else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else if(isset($_SERVER['HTTP_X_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    } else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    } else if(isset($_SERVER['HTTP_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    } else if(isset($_SERVER['REMOTE_ADDR'])) {
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    } else {
        $ipaddress = 'UNKNOWN';
    }
    return $ipaddress;
}
?>
Be Behind
  • 47
  • 10
  • Are you sure the main headers are set correctly? the html side I mean, maybe you can post it here. – Marco Sep 20 '16 at 15:16
  • I am pretty sure it is not your php code that is wrong, but at least one components in your tool chain that does _not_ use unicode (UTF) character encoding. Check this post: http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – arkascha Sep 20 '16 at 15:16
  • I pass really trough all chain and everything looks alright. I just need to tell you one fact that i forgot. On gmail (when i use gmail account everything is fine), when I use private mail then letters are in the mess(unicode problem). – Be Behind Sep 20 '16 at 16:39
  • Problem was in php and I founded after good night sleeping. If anybody is interested I can post a line of the code that's missing in my orginal code. – Be Behind Oct 17 '16 at 17:31
  • @BeBehind would love to find out what was missing – rory-h Feb 23 '17 at 05:34
  • @rory-h hey the problem was in header part. This is the old part of code,` $headers = 'Mail od: '.$fname.' <'.$email.'>' . "\r\n"; $headers .= 'Reply-To: '.$email.'' . "\r\n"; $headers .= 'X-Mailer: PHP/' . phpversion();` , and the correct version was `// Headers $headers = 'From: '.$fname.' <'.$email.'>' . "\r\n"; $headers .= 'Reply-To: '.$email.'' . "\r\n"; $headers .= 'X-Mailer: PHP/' . phpversion().'' . "\r\n"; $headers .= "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/plain;charset=UTF-8" . "\r\n";` – Be Behind Mar 02 '17 at 13:33

0 Answers0