user1812111 user1812111 - 2 years ago 61
PHP Question

php mail partially working with bunch of code in sent email

The above code works fine, but sent email looks little weird. All data is coming under one variable with code, making me divide information from code. Please find the image below.![enter image description here][1]

Answer Source

Foreword: What you posted as code, I was unable to reproduce exactly as per your attached image. Consult my footnote about this.

You wrote: "but sent email looks little weird". You will need to elaborate on that and where your "sent" section is.

However, from what I can tell by your script (which works fine when tested), is that you have both your HTML form and PHP inside the same file and is sending empty data as soon as the page is loaded; that I know for certain.

  • Seeing action="" in your form tells me that you are using it as a single file and isn't pointing to a second and external file.

You have two choices.

  • Either you split them as two files.
  • Use a conditional statement wrapping your entire executable code, such as isset() for the submit button I.e.: if(isset($_POST['submit_button'])){...} and !empty() for the inputs.

    I.e.: if(!empty($_POST['onward_date'])){...}

Sidenote: You have a misplaced </span> tag which will trigger a stray tag warning in a web browser's source (as per Firefox).

Also, you should use type="submit" instead of type="button" as this will produce two buttons. Least, it has while testing with Firefox.

Use this as a seperate file, and your PHP as file_2.php as an example filename:

<form action="file_2.php" method="post" id="booking_form">
<input type="radio" name="trip" id="one_way" value="one_way" checked="checked" /> ONE WAY
<input type="radio" name="trip" id="return" value="return"  /> RETURN
<br /><br />
<div id="first_line">
From: <input id="boarding" name="boarding" type="text" maxlength="40" class="user_input" size="18" value="New York" />
To: <input id="landing" name="landing" type="text" maxlength="40" class="user_input" size="18"  />
</div><br />
<div id="second_line">Onward: <input id="onward_date" name="onward_date" type="text" maxlength="40" class="user_input" size="18" />
<span id="hide">Return: <input id="return_date" name="return_date" type="text" maxlength="40" class="user_input" size="18"/></span></div><br />
<div id="third_line">Sur Name: <input id="sur_name" name="sur_name" type="text" maxlength="40" class="user_input" size="18" /></div><br />
<div id="four_line">Given Name: <input id="given_name" name="given_name" type="text" maxlength="40" class="user_input" size="18" /></div><br />
<div id="five_line">Contact Number: <input id="contact_number" name="contact_number" type="text" maxlength="40" class="user_input" size="18" /></div><br />
<div id="six_line">Email: <input id="email_id" name="email_id" type="text" maxlength="40" class="user_input" size="18" /></div><br />
<div id="seven_line"><input id="submit_button" name="submit_button" type="submit" value="Submit" /></div>

Now, you have a missing semi-colon for mail($to,$subject, $body, $headers) in your PHP.

It's best that you close it mail($to,$subject, $body, $headers);

If you want to add a message that is has been sent, do:

if(mail($to,$subject, $body, $headers)){

    echo "Message sent";

You may also want to add error reporting during testing.

Add error reporting to the top of your file(s) which will help find errors.

ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.


However, here is an (partial) example of mail headers showing the From: and empty message:

From: <>
Message-Id: <xxx>
Date: Thu, 30 Apr 2015 20:15:36 -0400
X-OutGoing-Spam-Status: No, score=-0.7
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
X-AntiAbuse: Primary Hostname -
X-AntiAbuse: Original Domain -
X-AntiAbuse: Originator/Caller UID/GID - [1234 56789] / [11 22]
X-AntiAbuse: Sender Address Domain -

Sur Name: 
Given Name: 
Contact Number: 
Onward Date: 
Return Date: 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download