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@email.com'; // 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";
       }
    }
    ?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.