Sam Skirrow
Sam Skirrow

Reputation: 3697

PHP email form is coming through without spaces between words and no special characters

I have built a form that sends using the php mail function via AJAX. My problem is when my emails come through, all spaces are replaced by '+' and special characters are replaced by things like '%40'.

Here is my AJAX script:

<script>
    jQuery(document).ready(function($) {
        $('.builderForm').each(function() {
            $(this).on("submit", function(event) {
                event.preventDefault();
                var formData = $(this).serialize();
                var loading = '<div id="overlay"><img id="loading" src="<?php echo LAYERS_FORM_BUILDER__PLUGIN_URL;?>assets/images/loading.gif"></div>';
                $(loading).appendTo('body');
                $.ajax({
                        type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
                        url         : '<?php echo LAYERS_FORM_BUILDER__PLUGIN_URL;?>includes/class-send-form.php', // the url where we want to POST
                        data        : {'formData' :formData}, // our data object
                        dataType    : 'json', // what type of data do we expect back from the server
                        encode      : true
                    })
                    .done(function(data) {
                        // log data to the console so we can see
                        //console.log(data); 
                        setTimeout(function(){$('#overlay').hide();}, 4000);
                        $('#overlay').remove();
                        $('.successMsg').html("Message Was Sent Successfully!!");
                        setTimeout(function(){$('.successMsg').hide();}, 5000);
                        // Reset form after submission
                        $(".builderForm")[0].reset();
                    });

                event.preventDefault();

            });
        });
    });
</script>

And my PHP mail handler code:

$layers_email_address = '';
$layers_email_subject = '';

// if there are any errors in our errors array, return a success boolean of false
if (!empty($errors)) {
// if there are items in our errors array, return those errors
    $data['success'] = false;
    $data['errors'] = $errors;

} else {
    parse_str($_REQUEST['formData'], $formFields)
//$formFields = explode('&', $_REQUEST['formData']);
$html='<html><body>';
foreach($formFields as $key => $value) {
    $fields = explode('=', $key);
    $field = $fields[0];
    $value = $fields[1];
    $html .= '<p><label>' . $key . ' :</label> ' . $value . '</p>';
}
$html .='</body></html>';
$to = "[email protected]";
$subject = "Form Submission";
$txt = $html;
$headers = "From: <[email protected]>". "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1"."\r\n";
mail($to,$subject,$txt,$headers);

// if there are no errors process our form, then return a message

// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
}

// return all our data to an AJAX call
echo json_encode($data);

How can I make it so that my emails come through as pain text, rather than special characters being ommitted?

Issue presented with multiple checkbox data sending, here is the code to dynamically create checkboxes (it sits in a foreach statement and takes each line of the "select-options" text area in the widget to populate each checkbox

if ($item['input_type'] == 'checkbox') { ?>
    <div class="options_container">
        <?php foreach(explode("\n", $item['select_options']) as $select_option) { ?>
        <input type="checkbox" name="<?php echo $item['input_name']; ?>" value="<?php echo preg_replace('/\s+/','', $select_option); ?>" <?php if( $this->check_and_return( $item, 'required') ) { echo 'required'; } ?>><span class="checkbox_option"><?php echo $select_option; ?></span>
        <?php } ?>
    </div>
<?php }

Edit: New code for php mail handler:

$layers_email_address = '';
$layers_email_subject = '';

// if there are any errors in our errors array, return a success boolean of false
if (!empty($errors)) {
// if there are items in our errors array, return those errors
    $data['success'] = false;
    $data['errors'] = $errors;

} else {
    parse_str($_REQUEST['formData'], $formFields)

$html = '<html><body>';
foreach ($formFields as $key => $value) {
    $html .= '<p><label>' . $key . ' :</label> ' . $value . '</p>';
}
$html .= '</body></html>';
$to = '[email protected]';
$subject = "Form Submission";
$txt = $html;
$headers = "From: <[email protected]>" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1" . "\r\n";
mail($to, $subject, $txt, $headers);

// if there are no errors process our form, then return a message

// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
}

// return all our data to an AJAX call
echo json_encode($data);

Upvotes: 4

Views: 144

Answers (1)

Zagonine
Zagonine

Reputation: 2313

Function parse_str() is what you looking for.


Use parse_str($_REQUEST['formData'], $formFields)] instead of $formFields = explode('&', $_REQUEST['formData']);

This will parse your string $_REQUEST['formData'] for create an array with key => value in the variable $formFields.

Documentation of the function here.

EDIT:

For the foreach working you will need to do this :

foreach ($formFields as $key => $value) {
    $html .= '<p><label>' . $key . ' :</label> ' . $value . '</p>';
}

Upvotes: 2

Related Questions