Author Topic: Line breaks in $body tag  (Read 209 times)

uncleles

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Line breaks in $body tag
« on: August 31, 2010, 04:24:41 PM »
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 = "uncleles@yahoo.com";
        $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!  :)

Thanks so much for your help!

Les  :^)

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 777
  • Karma: +3/-0
    • View Profile
    • PHP Help forum
Re: Line breaks in $body tag
« Reply #1 on: August 31, 2010, 04:37:30 PM »
Since you use content-type = text/html (in the email headers), you should use <br> tag as line breaks. You just need to append this in your code, like this:
PHP Code: [Select]
                    $Body .= $firstname."<br>";
                    
$Body .= $lastname."<br>";
                    
$Body .= $email."<br>";
                    
$Body .= $phone."<br>";
                    
$Body .= $address."<br>";
                    
$Body .= $city."<br>";
                    
$Body .= $state."<br>";
                    
$Body .= $zip."<br>";
                    
$Body .= $comments;


But if you use Content-type = text/plain - in this case you will need to use \n as new line:
PHP Code: [Select]
                    $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;


As for second question - redirecting to success screen, you can replace this line in your code:
PHP Code: [Select]
echo "Your mail has been Sent Successfully";
to this:
PHP Code: [Select]
header("Location: thank_you.html");
exit;
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!

uncleles

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Line breaks in $body tag
« Reply #2 on: August 31, 2010, 05:09:41 PM »
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 = "uncleles@yahoo.com";
        $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."<br>";
                    $Body .= $lastname."<br>";
                    $Body .= $email."<br>";
                    $Body .= $phone."<br>";
                    $Body .= $address."<br>";
                    $Body .= $city."<br>";
                    $Body .= $state."<br>";
                    $Body .= $zip."<br>";
                    $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;
   
            ?>

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 777
  • Karma: +3/-0
    • View Profile
    • PHP Help forum
Re: Line breaks in $body tag
« Reply #3 on: August 31, 2010, 05:31:18 PM »
Well, you replaced too much of your code  :) 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.
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!