1

For some reason, a () backlash appear before every (') apostrophe when sending a HTML formatted email through the PHP mail(); function. It's not a typo or anything similar. I have checked with other symbols and it only occurs with the apostrophe. It's so weird. It happens within the parameter $message from the form textarea (Note it's not the whole content, as long as I know as my regex doesn't allow apostrophes anywhere else). Could this have to do with the UTF-8 encoding? It's just so weird. Has it ever happened to anyone else and is there any solution to it? It's not a big problem, it's just not a usual one.

 $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!';

Example of received email:

From: Name Example

E-mail: example@email.com

Phone: +123456789

Hi there,

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

Regards

Lekstadt
  • 578
  • 3
  • 19

1 Answers1

1

You have magic_quotes_gpc enabled; this feature was deprecated in 5.3 and altogether removed in 5.4, but older versions would still have it enabled by default.

Read this article about how you can disable magic quotes in your existing environment if you can't upgrade your installation.

You can also use stripslashes() to remove the backslashes before sending the message.

$clean_message = stripslashes ($content);
mail($to, $subject, clean_message, $headers);
Hyder B.
  • 10,900
  • 5
  • 51
  • 60