Line breaks in $body tag

I deeply apologize if this is the dumbest question ever about PHP, but I have literally searched all day and couldn’t find an answer. Here is my code and then I will explain what I need:

<?php $mail_recipient = "[email protected]"; $mail_subject = "TELSCO Contact Us Submission"; $firstname = Trim(stripslashes($_REQUEST['firstname'])); $lastname = Trim(stripslashes($_REQUEST['lastname'])); $email = Trim(stripslashes($_REQUEST['email'])); $phone = Trim(stripslashes($_REQUEST['phone'])); $address = Trim(stripslashes($_REQUEST['address'])); $city = Trim(stripslashes($_REQUEST['city'])); $state = Trim(stripslashes($_REQUEST['state'])); $zip = Trim(stripslashes($_REQUEST['zip'])); $comments = Trim(stripslashes($_REQUEST['comments'])); $Body = ""; $Body .= $firstname; $Body .= $lastname; $Body .= $email; $Body .= $phone; $Body .= $address; $Body .= $city; $Body .= $state; $Body .= $zip; $Body .= $comments; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // send email $success = mail($mail_recipient, $mail_subject, $Body, $headers); if($success==true) echo "Your mail has been Sent Successfully"; else echo "Your mail has NOT Been Sent Successfully"; ?>

This is a very simple mail processing form. You’ll notice the $Body tags above output into an email that is one, long ugly string. I can’t for the life of me figure out how to put in line breaks. If you could please respond with a code example that works I would appreciate it.

My other question is that I want to redirect the user to an html page called thank_you.html instead of having the lame “Your mail has been Sent Successfully” message come up.

Again, I apologize for the simplicity of this, but I just couldn’t find anything on it and I searched for answers ALL DAY! Do I sound frustrated to you? Well … I should! :slight_smile:

Thanks so much for your help!

Les :^)

Since you use content-type = text/html (in the email headers), you should use
tag as line breaks. You just need to append this in your code, like this:
[php] $Body .= $firstname."
";
$Body .= $lastname."
";
$Body .= $email."
";
$Body .= $phone."
";
$Body .= $address."
";
$Body .= $city."
";
$Body .= $state."
";
$Body .= $zip."
";
$Body .= $comments;
[/php]

But if you use Content-type = text/plain - in this case you will need to use \n as new line:
[php] $Body .= $firstname."\n";
$Body .= $lastname."\n";
$Body .= $email."\n";
$Body .= $phone."\n";
$Body .= $address."\n";
$Body .= $city."\n";
$Body .= $state."\n";
$Body .= $zip."\n";
$Body .= $comments;
[/php]

As for second question - redirecting to success screen, you can replace this line in your code:
[php]echo “Your mail has been Sent Successfully”;[/php]
to this:
[php]header(“Location: thank_you.html”);
exit;[/php]

Thank you so much for your help. It seems we are 98% of the way there. I get redirected to the thank you page and I don’t get any errors, but no email is sent. Here’s what I have now:

<?php $mail_recipient = "[email protected]"; $mail_subject = "TELSCO Contact Us Submission"; $firstname = Trim(stripslashes($_REQUEST['firstname'])); $lastname = Trim(stripslashes($_REQUEST['lastname'])); $email = Trim(stripslashes($_REQUEST['email'])); $phone = Trim(stripslashes($_REQUEST['phone'])); $address = Trim(stripslashes($_REQUEST['address'])); $city = Trim(stripslashes($_REQUEST['city'])); $state = Trim(stripslashes($_REQUEST['state'])); $zip = Trim(stripslashes($_REQUEST['zip'])); $comments = Trim(stripslashes($_REQUEST['comments'])); $Body = ""; $Body .= $firstname."
"; $Body .= $lastname."
"; $Body .= $email."
"; $Body .= $phone."
"; $Body .= $address."
"; $Body .= $city."
"; $Body .= $state."
"; $Body .= $zip."
"; $Body .= $comments; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // send email header("Location: thank_you.html"); exit; ?>

Well, you replaced too much of your code :slight_smile: I asked to replace just one line in your code (where you echo’ing success message), but you also eliminated your lines with mail() function call, checking for $success, etc. Take your original code, and replace just one line mentioned above.

Sponsor our Newsletter | Privacy Policy | Terms of Service