Andy17 Posted October 28, 2008 Share Posted October 28, 2008 Hey guys, I have a small problem with my e-mail script that sends an HTML mail. The sent text is the content a user enters into a text field. So, when the user presses Enter in the text field to make spaces, I would like these spaces to be shown in the mail. I have tried using nl2br() but without success. I have the following code (I obviously have more, but the code below is the relevant part): <?php $mail = mysql_real_escape_string(trim(strip_tags(htmlspecialchars($_POST['receivermail'], ENT_QUOTES)))); $subject = mysql_real_escape_string(trim(strip_tags(htmlspecialchars($_POST['subject'], ENT_QUOTES)))); $message = mysql_real_escape_string(trim(strip_tags(htmlspecialchars($_POST['mailtext'], ENT_QUOTES)))); $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "from: [email protected]"; mail($mail, $subject, $message, $headers); ?> If I type in "a [space] b [space] c", it will look like this in the mail: a\r\nb\r\nc I guess the problem is in the headers? I'm just not sure what to change there since I'm new to that part. So basically I just want a line break to write <br />. Thank you. Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/ Share on other sites More sharing options...
Andy17 Posted October 29, 2008 Author Share Posted October 29, 2008 Bump. Also, how would I make the sender's name appear like this: mydomain.com instead of [email protected]? It does not work to just rewrite it with my current code. Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-677466 Share on other sites More sharing options...
Andy17 Posted October 29, 2008 Author Share Posted October 29, 2008 Bump. Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-677672 Share on other sites More sharing options...
Andy17 Posted October 30, 2008 Author Share Posted October 30, 2008 Bump. Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-678535 Share on other sites More sharing options...
DeanWhitehouse Posted October 30, 2008 Share Posted October 30, 2008 nl2br should do it <?php $mail = htmlspecialchars($_POST['receivermail'], ENT_QUOTES); $mail = mysql_real_escape_string($mail); $mail = trim($mail); $mail = strip_tags($mail); $subject = htmlspecialchars($_POST['subject'], ENT_QUOTES); $subject = mysql_real_escape_string($subject); $subject = trim($subject); $subject = strip_tags($subject); $message = htmlspecialchars($_POST['mailtext'], ENT_QUOTES); $message = mysql_real_escape_string($message); $message = trim($message); $message = strip_tags($message); $message = nl2br($message); $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: Dizzit.net<[email protected]>"; mail($mail, $subject, $message, $headers); ?> You have to do it after you use htmlentities or it gets converted to a safe form Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-678545 Share on other sites More sharing options...
Andy17 Posted October 30, 2008 Author Share Posted October 30, 2008 Hi Blade280891 and thank you for your reply. Unfortunately, I already tried this before posting here (sorry for not mentioning how I used nl2br()) and it does not work. Each space, which should be "<br />" displays as "\r\n" in the sent e-mail. Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-678744 Share on other sites More sharing options...
DeanWhitehouse Posted October 30, 2008 Share Posted October 30, 2008 Echo it out onto the page , before sending and post the source code. Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-678815 Share on other sites More sharing options...
Andy17 Posted October 31, 2008 Author Share Posted October 31, 2008 I'm not entirely sure what you mean because I do not want to show the e-mail on the website when a user presses the send button. I would just like it to send it. Anyways, here is the code: <?php if (isset($_POST['submitbutton'])) { $mail = htmlspecialchars($_POST['receivermail'], ENT_QUOTES); $mail = mysql_real_escape_string($mail); $mail = trim($mail); $mail = strip_tags($mail); $subject = htmlspecialchars($_POST['subject'], ENT_QUOTES); $subject = mysql_real_escape_string($subject); $subject = trim($subject); $subject = strip_tags($subject); $message = htmlspecialchars($_POST['mailtext'], ENT_QUOTES); $message = mysql_real_escape_string($message); $message = trim($message); $message = strip_tags($message); $message = nl2br($message); $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: dizzit.net<[email protected]>"; mail($mail, $subject, $message, $headers); } ?> <form name="sendmail" method="post"> Reciever's e-mail address: <input type="text" name="receivermail" id="receivermail" value="[email protected]" onfocus="if (this.value == '[email protected]') {this.value = '';}" onblur="if (this.value == '') {this.value = '[email protected]';}" /> Subject: <input type="text" name="subject" id="subject" value="Please write a descriptive subject" onfocus="if (this.value == 'Please write a descriptive subject') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Please write a descriptive subject';}" /> Message: <textarea name="mailtext" id="mailtext" style="width: 400px; height: 200px;" onfocus="if (this.value == 'Write your message here...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Write your message here...';}" />Write your message here...</textarea> <input type="submit" name="submitbutton" value="Send" /> </form> Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-679504 Share on other sites More sharing options...
Andy17 Posted November 1, 2008 Author Share Posted November 1, 2008 bump Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-679911 Share on other sites More sharing options...
Andy17 Posted November 1, 2008 Author Share Posted November 1, 2008 Bump. No one reads page 3. Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-680150 Share on other sites More sharing options...
DeanWhitehouse Posted November 1, 2008 Share Posted November 1, 2008 Do what i said. To view the source code right click on the page then view source. Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-680226 Share on other sites More sharing options...
Andy17 Posted November 1, 2008 Author Share Posted November 1, 2008 Do what i said. To view the source code right click on the page then view source. Oh, sorry, I just misunderstood you. I echo'd it and it looked like this: this\r\n\r\nis\r\n\r\na\r\n\r\ntest\r\n\r\nfor\r\n\r\nBlade280891 Where it should have looked like this: this<br /><br />is<br /><br />a<br /><br />test<br /><br />for<br /><br />Blade280891 So, for some reason, "\r\n" is printed instead of "<br />". I noticed the \r\n in the header and was wondering if I would have to change something there. Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-680246 Share on other sites More sharing options...
DeanWhitehouse Posted November 1, 2008 Share Posted November 1, 2008 Ok, one last test echo the $message out after each change e.g. $message = htmlspecialchars($_POST['mailtext'], ENT_QUOTES); echo "Stage 1: ".$message;//do this after every change. $message = mysql_real_escape_string($message); $message = trim($message); $message = strip_tags($message); $message = nl2br($message); Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-680250 Share on other sites More sharing options...
Andy17 Posted November 2, 2008 Author Share Posted November 2, 2008 Ok it appears that the $message = mysql_real_escape_string($message); line caused the problem (for some unknown reason), so I moved this line to after the mail is sent. Now the line breaks work, but the following code still does escapes: <?php echo $_POST['mailtext']; // Stage 1 $message = htmlspecialchars($_POST['mailtext'], ENT_QUOTES); // Stage 2 $message = trim($message); // Stage 3 $message = strip_tags($message); // Stage 4 $message = nl2br($message); // Stage 5 ?> This means that it puts a \ in front of every ', even though I don't do mysql_real_escape_string() anywhere in my code. This already happens at stage 1 and I tried to remove the ENT_QUOTES but nothing changed. This is very strange. Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-680484 Share on other sites More sharing options...
DeanWhitehouse Posted November 2, 2008 Share Posted November 2, 2008 That means magic quotes are on Look at this to disable http://uk2.php.net/manual/en/security.magicquotes.disabling.php Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-680733 Share on other sites More sharing options...
Andy17 Posted November 3, 2008 Author Share Posted November 3, 2008 Thanks a lot, you nailed it. Link to comment https://forums.phpfreaks.com/topic/130482-solved-sending-html-mail-from-php/#findComment-681163 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.