Jump to content

Recommended Posts

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 );

Link to comment
https://forums.phpfreaks.com/topic/270038-problem-with-sending-mail/
Share on other sites

you can use str_replace function to replace /r/n with < br / > or just flat out remove the line break ;D

 

//**allow line breaks**//
$message = str_replace("/r/n" , "<br />", $_POST[service]);
//** remove line breaks**//
$message =str_replace("r/n" , "" , $_POST[service]);

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).

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.