0

i have made my contact form and it's working good except that when someone send me a message it comes without any format like the image below:

e-mail without any style, just row text

this is my .php code that i use:

    $formdata = array (
       'name' => $name,
       'city' => $city,
       'message' => $message
    );

    if ( !( $formerrors ) ) :
        $to  = "me@sipledomain.com";// input my name address to get mail to
        $subject = "From $name";
        $message =  json_encode($formdata);

        if ( mail( $to, $subject, $message ) ):
           $msg = "Thanks for filling out the form, i will contact you soon";
        else:
           $msg = "Problem sending the message";
        endif; // mail form data
    endif; // check for form errors
 endif; //form submitted

thanks in advance

Tepken Vannkorn
  • 9,648
  • 14
  • 61
  • 86
AhmedBinNasser
  • 2,735
  • 1
  • 24
  • 24

2 Answers2

1

json_encode() encodes your array into a single line of text designed to be decoded later, not for reading by humans.

Instead I would build your email message yourself by writing your own HTML or by giving it line breaks. You could do it programmatically by parsing/iterating through your array.

Eg:

$message = 'Name: '.$formdata['name'].'<br />'.$formdata['city'].'<br />'.'...';


If you really want to encode into JSON, you will need to do parse the JSON after you encode and do the same thing.

You might want to look into a flag when you call json_encode() called JSON_PRETTY_PRINT which will keep whitespace. More info: http://www.php.net/manual/en/json.constants.php

In use: $message = json_encode($formdata, JSON_PRETTY_PRINT);


For playing with JSON I like to use tools like http://jsonmate.com/ that formats JSON into a neat tree.

Jared
  • 2,978
  • 4
  • 26
  • 45
0

If you want, send the email as formatted HTML by wrapping everything in standard HTML tags. Otherwise, PHP sends messages as unformatted, so use \n to break lines etc.

scrowler
  • 24,273
  • 9
  • 60
  • 92