Jump to content

newline not working properly - output not correct in email


Olivia_koo86

Recommended Posts

Hey, I have a PHP email script. It has newlines (\n) although this is not correctly displayed in emails. Instead of a 'new line', it displays the '\n' itself.

 

Below is my code.

 

 <?php
    if($_POST) {
       // Contact subject
       $subject ='Holiday Suggestion Request';
       // Details
       $email_body.= 'About Customer \n\n';
       $email_body.= 'Customer name : '.$_POST['name'].'\n';
       $email_body.= 'Customer email : '.$_POST['email'].'\n';
       $email_body.= 'Country : '.$_POST['country'].'\n';
       $email_body.= 'Budget : '.htmlspecialchars(stripslashes($_POST['budget'])).'\n\n';
       $email_body.= 'Arrival & Accomodation\n\n';
       $email_body.= 'Arrive TIme : '.$_POST['select2'].'\n';
       $email_body.= 'No. of Nights : '.$_POST['select3'].'\n';
       $email_body.= 'No. of people : '.$_POST['select4'].'\n';
       $email_body.= 'Children : '.$_POST['select'].'\n';
       $email_body.= 'Accomodation class : '.$_POST['select5'].'\n';
       
       $email_body.= 'Activities : ';
       if($_POST['activities_1'] !='') {
       $email_body .= $_POST['activities_1'].",";
       }
       if($_POST['activities_2'] !='') {
          $email_body .= $_POST['activities_2'].",";
       }
       if($_POST['activities_3'] !='') {
          $email_body .= $_POST['activities_3'].",";
       }
       if($_POST['activities_4'] !='') {
          $email_body .= $_POST['activities_4'].",";
       }
       if($_POST['activities_5'] !='') {
          $email_body .= $_POST['activities_5'].",";
       }
       if($_POST['activities_6'] !='') {
          $email_body .= $_POST['activities_6'].",";
       }
       if($_POST['activities_7'] !='') {
          $email_body .= $_POST['activities_7'].",";
       }
       if($_POST['activities_8'] !='') {
          $email_body .= $_POST['activities_8'].",";
       }
       if($_POST['activities_9'] !='') {
          $email_body .= $_POST['activities_9'].",";
       }
       $email_body = substr($email_body,0,-1);
       $email_body .='\n' ;
       
       $email_body.= 'What makes a perfect holiday?';
       if($_POST['checkbox1'] !='') {
       $email_body .= $_POST['checkbox1'].",";
       }
       if($_POST['checkbox2'] !='') {
       $email_body .= $_POST['checkbox2'].",";
       }
       if($_POST['checkbox3'] !='') {
       $email_body .= $_POST['checkbox3'].",";
       }
       if($_POST['checkbox4'] !='') {
       $email_body .= $_POST['checkbox4'].",";
       }
       if($_POST['checkbox5'] !='') {
       $email_body .= $_POST['checkbox5'].",";
       }
       if($_POST['checkbox6'] !='') {
       $email_body .= $_POST['checkbox6'].",";
       }
       $email_body = substr($email_body,0,-1);
       $email_body .='\n' ;
       
       $email_body.= 'Facilities you would like?';
       if($_POST['checkbox_1'] !='') {
       $email_body .= $_POST['checkbox_1'].",";
       }
       if($_POST['checkbox_2'] !='') {
       $email_body .= $_POST['checkbox_2'].",";
       }
       if($_POST['checkbox_3'] !='') {
       $email_body .= $_POST['checkbox_3'].",";
       }
       if($_POST['checkbox_4'] !='') {
       $email_body .= $_POST['checkbox_4'].",";
       }
       if($_POST['checkbox_5'] !='') {
       $email_body .= $_POST['checkbox_5'].",";
       }
       if($_POST['checkbox_6'] !='') {
       $email_body .= $_POST['checkbox_6'].",";
       }
       if($_POST['checkbox_7'] !='') {
       $email_body .= $_POST['checkbox_7'].",";
       }
       if($_POST['checkbox_8'] !='') {
       $email_body .= $_POST['checkbox_8'].",";
       }
       $email_body = substr($email_body,0,-1);
       $email_body .='\n message : \n'.$_POST['textfield3'].'' ;
       
       
       // Mail of sender
       $mail_from=$_POST['email'];
       // From
       $name = $_POST['name'];
       $header="from: $name <$mail_from>";

       // Enter your email address
       $to ='[email protected]'; // Here the mail address of reciever
  

       $send_contact=mail($to,$subject,$email_body,$header);

       // Check, if message sent to your email
       // display message "We've recived your information"
       if($send_contact){
       $out_put = "We've recived your contact information";
       }
       else {
       $out_put = "ERROR on sending mail";
       }
    }
    ?>

 

use "\n" instead of '\n'.

with single quotes, newline is not inserted properly.

 

Well to be clear, single quotes creates a string constant.  Because "\n" is an escape code that has to be replaced by php with a newline character, it requires interpolation (variable replacement) so, this is the reason you need the double quotes around it.  It's not because single quotes doesn't work right -- it's simply not intended to perform interpolation.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.