为什么在 PHP 电子邮件中的每个撇号之前都会出现反冲?

Why does a backlash appear before every apostrophe in a PHP email?

由于某些原因,当通过 PHP mail() 发送 HTML 格式的电子邮件时,每个 (') 撇号前都会出现 () 反冲;功能。这不是错字或类似的东西。我检查了其他符号,它只出现在撇号中。这太奇怪了。它发生在表单 textarea 的参数 $message 中(注意它不是全部内容,只要我知道我的正则表达式不允许在其他任何地方使用撇号)。这可能与 UTF-8 编码有关吗?这太奇怪了。有没有其他人发生过这种情况,有什么解决办法吗?不是什么大问题,就是不正常而已。

 $to = 'example@email.com';
            $subject = "Hey, you've got a new message!";
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
            $headers .= 'From: <example@email.com>' . "\r\n";
            $content = "<html><head><style>
               .bold {
                  font-weight: bold;
               }
               .contiguous {
                  display: inline-block;
               }
               .correct-spacing {
                  white-space: pre-wrap;
               }
               .grey {
                  color: #373737;
               }
               .lilac {
                  color: #7b68ee;
               }
               </style></head><body>
               <div class='bold contiguous lilac'>From:&nbsp;</div><div class='contiguous grey'>$name</div><br/>
               <div class='bold contiguous lilac'>E-mail:&nbsp;</div><div class='contiguous grey'>$email</div><br/>
               <div class='bold contiguous lilac'>Phone:&nbsp;</div><div class='contiguous grey'>$phone</div><br/><br/>
               <div class='correct-spacing grey'>$message</div>
               </body></html>";
            mail($to, $subject, $content, $headers);
            echo 'Your message has been sent. Thank you!';

收到的电子邮件示例:

From: Name Example

E-mail: example@email.com

Phone: +123456789

Hi there,

This is a test. Let\'s hope it works.

Regards

您已启用 magic_quotes_gpc;此功能在 5.3 中已弃用并在 5.4 中完全删除,但旧版本仍会默认启用它。

阅读this article了解如何在无法升级安装的情况下在现有环境中禁用魔术引号。

您还可以使用 stripslashes() 在发送消息前删除反斜杠。

$clean_message = stripslashes ($content);
mail($to, $subject, clean_message, $headers);