ChetB Posted October 29, 2012 Share Posted October 29, 2012 I'm trying to have a form on my website where people can fill it out to request a quote. It will enter the quote request into a database and then send me an email with the details. My code below enters all the information into the database fine. However, the emails are not looking too good. Here is an example of what an email looks like when the visitor uses line breaks and ' in the fields: This is just a simple test. Nothing but a test.\r\n\r\nYep, this is just a test.\r\nAin\'t that cool? $name="$_POST[name]"; $email="$_POST[email]"; $phone="$_POST[phone]"; $message="$_POST[service]"; $ip=$_SERVER['REMOTE_ADDR']; $name = stripslashes($name); $email = stripslashes($email); $phone = stripslashes($phone); $message = stripslashes($message); $name = mysql_real_escape_string($name); $email = mysql_real_escape_string($email); $phone = mysql_real_escape_string($phone); $message = mysql_real_escape_string($message); // Make sure all required fields are used if(!$name){ die("Please go back and tell us your name"); } if(!$email){ die("Please go back and tell us your email address"); } if(!$message){ die("Please go back and tell us what type of service you are looking for"); } // Now send the email here $message = wordwrap($message, 90); $to = 'Name <email@email.com>'; $subject = 'Subject Here'; $message2 = "$name\n$email\n$phone\n\n$message\n\n======================================\nThis quote request was sent from:\n$_SERVER[HTTP_REFERER]"; 'Reply-To: $email' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $headers .= 'Content-type: text/html' . "\r\n"; mail( "$to", "$subject", $message2, "From: $name <$email>", $headers ); Quote Link to comment https://forums.phpfreaks.com/topic/270038-problem-with-sending-mail/ Share on other sites More sharing options...
darkfreaks Posted October 30, 2012 Share Posted October 30, 2012 you can use str_replace function to replace /r/n with < br / > or just flat out remove the line break //**allow line breaks**// $message = str_replace("/r/n" , "<br />", $_POST[service]); //** remove line breaks**// $message =str_replace("r/n" , "" , $_POST[service]); Quote Link to comment https://forums.phpfreaks.com/topic/270038-problem-with-sending-mail/#findComment-1388593 Share on other sites More sharing options...
sumpygump Posted October 30, 2012 Share Posted October 30, 2012 I think the problem would be that using mysql_real_escape_string is actually preparing a string to be inserted into a database in SQL, so it is converting your linebreaks into a literal "\r\n" or "\\r\\n" so it doesn't equate to an actual carriage return and line break in the email. Similar problem with the single quotes which are getting the backslashes added. I would comment out or delete the lines that call mysql_real_escape_string() on the values prior to sending the email (but keep them for whatever code is inserting them into the database, which isn't shown). Quote Link to comment https://forums.phpfreaks.com/topic/270038-problem-with-sending-mail/#findComment-1388595 Share on other sites More sharing options...
Muddy_Funster Posted October 30, 2012 Share Posted October 30, 2012 As sumpygump said, you are using the mysql_real_escape_string() in the totally wrong place. mysql_real_escape_string() is only used to make strings safe to run through queries. What you probably should see are double quotes at the start and end of your message as well. However, darkfeeks is also right, as you are sending your message as html, you won't get line breaks from that, it will just dissapear, because html doesn't use standard text control char's to format it's layout. If you are sending an html email you should be coding the content in html. Quote Link to comment https://forums.phpfreaks.com/topic/270038-problem-with-sending-mail/#findComment-1388652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.