0

I'm aware there is a lot of topics like this one, but I wasn't yet able to find a satisfying answer.

PHP on a shared hosting where I have my webpage deletes backslashes (by using magic_quotes() or something like that maybe?) from expressions that are soon to be sent using mail(), so it changes this:

<?php
$email_to = "myemail@address.com";
$email_subject = "Message from your webpage";

$name = $_POST['name'];
$email_from = $_POST['email'];
$subject = $_POST['subject'];
$message= $_POST['message'];

if($subject) {
die(); // $subject is a honeypot
}

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
    die();
}

$email_message = "";
$email_message .= "Name: ".$name;
$email_message .= "Email: ".$email_from;
$email_message .= "Message: ".$message;

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
$headers .= "From: " . $name . " <" . $email_from . ">" . "\r\n"; 
$headers .= "Reply-To: " . $email_from;

@mail($email_to, $email_subject, $email_message, $headers);
?>

into this:

$headers  = 'MIME-Version: 1.0' . "rn";
$headers .= 'Content-type: text/html; charset=utf-8' . "rn";
$headers .= 'X-Mailer: PHP/'.phpversion()."rn";
$headers .= "From: " . $name . " <" . $email_from . ">" . "rn"; 
$headers .= "Reply-To: " . $email_from;

Such emails don't have an author and without proper charset, they look terrible. I tried many different functions, but none seemed to work. Does anyone know how to solve it?

EDIT: Ive put thw whole code in right now. Alse, var_export($headers) shows exactly whats been put in it string(136) "MIME-Version: 1.0rnContent-type: text/html; charset=utf-8rnX-Mailer: PHP/5.6.6rnFrom: dasda rnReply-To: asdasd@dasda.cz"

J4Y4G3
  • 1
  • 1

2 Answers2

1

Update

Do not use their webpage editor for editing code.

There should be a code editor.

do they use cPanel as the control panel? If so look for the code editor, it is there.

Or use FTP and edit on your local workstation. This is the preferred method.

I use FileZilla for FTP and Notepad++ as an editor, they work very well together.

end of update


You need to show the whole code as to how your are getting the "into this"
I'd like to see the var_dump if that is not what you are doing.

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion()."\r\n";
$headers .= "From: " . $name . " <" . $email_from . ">" . "\r\n"; 
$headers .= "Reply-To: " . $_POST["email"] . "\r\n";

var_export($headers);
Misunderstood
  • 5,534
  • 1
  • 18
  • 25
0

You should use PHP's native PHP_EOL as it is platform-independent (check this question for reference). Try to modify your code like this:

$headers  = 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=utf-8' . PHP_EOL;
$headers .= 'X-Mailer: PHP/'.phpversion() . PHP_EOL;
$headers .= "From: " . $name . " <" . $email_from . ">" . PHP_EOL; 
$headers .= "Reply-To: " . $_POST["email"] . PHP_EOL;

EDIT:

As @misunderstood pointed PHP_EOL would not be good solution, as e-mail headers should always be ended with sequence.

I think, I found workaround for this. Instead of \r\n try to use:

mb_convert_encoding('&#x000D;&#x000A;', 'UTF-8', 'HTML-ENTITIES')

http://en.wikipedia.org/wiki/Newline

Community
  • 1
  • 1
Paweł Tomkiel
  • 1,974
  • 2
  • 21
  • 39
  • He needs CRLF not EOL. `PHP_EOL` will give CRLF on a Windows platform, and just LF on Linux. – Misunderstood Apr 17 '15 at 12:26
  • As i read [PHP mail docs](http://php.net/function.mail), you're right and there's no way to use any constant for that. It may be even `define('CRLF',"\r\n")`, but it doesn't resolve slash stripping problem. – Paweł Tomkiel Apr 17 '15 at 12:43
  • You should not need a workaround, `"\r\n"` should (does) work. What I do not understand is where they got stripped. Where did you find "rn"? Based on the code you posted the only place you could see the "rn" is in the received email. – Misunderstood Apr 17 '15 at 13:00
  • Both `PHP_EOL` and `mb_convert_encoding(' ', 'UTF-8', 'HTML-ENTITIES')` seem to fix the problem in terms of appearing properly in `var_export($headers)` but in both cases the emails stopped coming. – J4Y4G3 Apr 17 '15 at 13:05
  • Seems they get stripped as soon as I edit the file in the hostings webpage editor or upload it anew – J4Y4G3 Apr 17 '15 at 13:06