3

可能重复:
正确编码 PHP 邮件表单中的字符(“I'm”变成“I\'m”)

我正在通过 PHP 发送一封电子邮件,文本在引号之前带有一个斜杠:

I'm变成I\'m

我的PHP:

$text_message = $_POST["mymessage"];       


$message="--PHP-mixed-$bound_text\r\n"      
            ."Content-Type: text/plain; charset=\"utf-8\"\r\n"
 ."Content-Transfer-Encoding: 7bit\r\n\r\n" 
       ."$text_message\r\n\r\n" 
    ."--PHP-mixed-$bound_text\r\n"  
            ."Content-Transfer-Encoding: base64\r\n"
            ."Content-Disposition: attachment; filename=\"$attachment\"\r\n"
."Content-Type: image/jpeg; name=\"$attachment\"\r\n\r\n"
 .chunk_split($file)
        ."\r\n\r\n"
            ."--PHP-mixed-$bound_text--\r\n\r\n";
}

如何在不收到额外斜线的情况下发送它?谢谢。乌里

4

3 回答 3

4

这是由 PHP 的魔术引号引起的,这些引号已被弃用,但不幸的是,默认情况下仍启用。在大多数情况下,您可以通过.htaccess文件甚至通过 webhoster 的控制面板禁用该功能。

如果这不可能,最安全的做法是get_magic_quotes_gpc()在盲目使用stripslashes(). 要取消转义所有$_POST[]变量,请使用:

if (get_magic_quotes_gpc()) {
    foreach($_POST as $k => $v) {
       $_POST[$k] = stripslashes($v);
    }
}
于 2012-02-29T23:22:57.457 回答
2

你应该检查你的 php.ini 文件中的magic_quotes,它最有可能在,你总是可以在 php 中检查这个选项并相应地处理字符串

if (get_magic_quotes_gpc()){
   $text_message = stripslashes($_POST["mymessage"]);
}else{
   $text_message = $_POST["mymessage"];
}

此外,\r\n您应该使用PHP_EOL它而不是使用它与所有操作系统兼容:例如\rlinux 不需要

于 2012-02-29T23:20:38.017 回答
1

我怀疑它归结为$_POST["mymessage"]. 如果你回显$_POST["mymessage"]到屏幕,你会得到什么?

一些webhosts会故意addslashes()将接收到的数据$_POST$_GET等作为防止SQL注入的基本保护。

如果正在这样做,你应该能够做到stripslashes($_POST["mymessage"])

于 2012-02-29T23:12:55.210 回答