Correctly encode characters in a PHP mail form (“I

2019-01-29 12:15发布

问题:

I'm testing a PHP mail form, a very barebones one, found here:

<?php

    if(isset($_POST['submit']))

    {

        //The form has been submitted, prep a nice thank you message

        $output = '<h3>Thanks for your message</h3>';


        //Deal with the email

        $to = 'mymail@mail.com';

        $subject = 'you have a mail';



        $contactname = strip_tags($_POST['contactname']);

        $adress = strip_tags($_POST['adress']);

        $contactemail = strip_tags($_POST['contactemail']);

        $textmessage = strip_tags($_POST['textmessage']);



        $boundary =md5(date('r', time())); 



        $headers = "From: My Site\r\nReply-To: webmaster@mysite.com";





        $message = "Name: ".$contactname."\n";

        $message .= "Adress: ".$adress."\n";

        $message .= "E-mail: ".$contactemail."\n";

        $message .= "Message: ".$textmessage."\n";



        mail($to, $subject, $message, $headers);

    }

?>

The problem is I'm receiving an unwanted slash "\" everytime I write a single or a double quote in my message, so "I'm" appear as "I\'m" in my mailbox.

I know it have to do with the way PHP distinguishes code quotes from only lecture quotes, but I wouldn't know what to add in my form to get it properly running.

Any help is appreciated,

回答1:

you can try stripslashing your message , something like :

$message = stripslashes($message);


回答2:

The easiest thing to do is to turn magic quotes off in php.ini,

magic_quotes_gpc=false

If you can't do that, you need to remove slashes like this,

if (get_magic_quotes_gpc()) {
    foreach($_POST as $k => $v) {
       $_POST[$k] = stripslashes($v);
    }
}


标签: php email encode