0

I have set up an online order form and have the form output emailed to my wife's inbox. However, currently all values come out in a list format that is hard for her to follow.

The following is an order from our Web Order Form:

            Name: 
         Address: 
           Phone: 
           Email: 
     Book Number: 
    Page Number1: 
 Product Number1: 
        Product1: 
            QTY1: 
     Unit Price1: 
    Total Price1: 
    Page Number2: 
 Product Number2: 
        Product2: 
            QTY2: 
     Unit Price2: 
    Total Price2: 
    Page Number3: 
 Product Number3: 
        Product3: 
            QTY3: 
     Unit Price3: 
    Total Price3:

I want to format the email to look similar to the input form on the website. I have been trying to reverse engineer other people's examples with no luck. I would include snapshot of the form but my reputation isn't high enough. Essentially it is a table containing form fields corresponding to the data I need to collect (think order form). Below is a sample of the PHP code it is submitted to.

<?php 
 $from = $_REQUEST['Email'] ; 
 $to = "myemailaddress@gmail.com";
 $name = $_REQUEST['Name'] ; 
 $headers = "From: $from"; 
 $subject = "Online Order Submission"; 

 $fields = array(); 
 $fields{"Name"} = "Name"; 
 $fields{"Address"} = "Address"; 
 $fields{"Phone"} = "Phone"; 
 $fields{"Email"} = "Email"; 
 $fields{"Item1Page"} = "Page Number1"; 
 $fields{"Number1"} = "Product Number1"; 
 $fields{"Product1"} = "Product1"; 
 $fields{"QTY1"} = "QTY1";
 $fields{"Uprice1"} = "Unit Price1"; 
 $fields{"Tprice1"} = "Total Price1"; 
 $fields{"Item2Page"} = "Page Number2"; 
 $fields{"Number2"} = "Product Number2"; 
 $fields{"Product2"} = "Product2"; 
 $fields{"QTY2"} = "QTY2";
 $fields{"Uprice2"} = "Unit Price2"; 
 $fields{"Tprice2"} = "Total Price2"; 

// Variables removed for clarity 

 $fields{"Item11Page"} = "Page Number11"; 
 $fields{"Number11"} = "Product Number11"; 
 $fields{"Product11"} = "Product11"; 
 $fields{"QTY11"} = "QTY11";
 $fields{"Uprice11"} = "Unit Price11"; 
 $fields{"Tprice11"} = "Total Price11"; 

 $body = "The following is an order from our Web Order Form:\n\n"; foreach($fields as $a => $b){    
 $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 

 $headers2 = "From: myemailaddress@gmail.com"; 
 $subject2 = "Thank you for your order"; 
 $autoreply = "Thank you for your order. We will contact you when your order is ready for delivery.";

 if($from == '') {print "You have not entered an email, please go back and try again";} 
 else { 
 if($name == '') {print "You have not entered a name, please go back and try again";} 
 else { 
 $send = mail($to, $subject, $body, $headers); 
 $send2 = mail($from, $subject2, $autoreply, $headers2); 
 if($send) 
 {header( "Location: http://www.website.com/thankyou.html" );} 
 else 
 {print "We encountered an error sending your mail, please try again later."; } 
 }
}
 ?> 

Any help would be appreciated


UPDATE

I have tried a number of the below mentioned references and ammendments. The current edit looks like this:

<?php 
$from = $_REQUEST['Email'] ; 
$to = "myemailaddress@live.com";
$Name = $_REQUEST['Name'] ; 
$headers = "From: $from"; 
$headers .= "Content-type: text/html\r\n";
$subject = "Order Submission TEST"; 
$message = "<html><body><br><br>The following is an order from our TEST Order Form<br><br><br>";
$message .= "<table>";
$message .= "<tr><td>Name: </td><td>" . strip_tags($_POST['Name']) . "</td></tr>";
$message .= "<tr><td>Address: </td><td>" . strip_tags($_POST['Address']) . "</td></tr>";
$message .= "<tr><td>Phone No: </td><td>" . strip_tags($_POST['Phone']) . "</td></tr>";
$message .= "<tr><td>E-mail: </td><td>" . strip_tags($_POST['Email']) . "</td></tr>";
$message .= "</table>";
$message .= "<table border=1><tr><td>Page No.</td><td>Product No.</td><td>Product Name</td><td>QTY</td><td>Unit Price</td><td>Total Price</td></tr>";
$message .= "<tr><td>" . strip_tags($_POST['Item1Page']) . "</td><td>" . strip_tags($_POST['Number1']) . "</td><td>" . strip_tags($_POST['Product1']) . "</td><td>" . strip_tags($_POST['QTY1']) . "</td><td>" . strip_tags($_POST['Uprice1']) . "</td><td>" . strip_tags($_POST['Tprice1']) . "</td></tr>";
$message .= "<tr><td>" . strip_tags($_POST['Item2Page']) . "</td><td>" . strip_tags($_POST['Number2']) . "</td><td>" . strip_tags($_POST['Product2']) . "</td><td>" . strip_tags($_POST['QTY2']) . "</td><td>" . strip_tags($_POST['Uprice2']) . "</td><td>" . strip_tags($_POST['Tprice2']) . "</td></tr>";
$message .= "<tr><td>" . strip_tags($_POST['Item3Page']) . "</td><td>" . strip_tags($_POST['Number3']) . "</td><td>" . strip_tags($_POST['Product3']) . "</td><td>" . strip_tags($_POST['QTY3']) . "</td><td>" . strip_tags($_POST['Uprice3']) . "</td><td>" . strip_tags($_POST['Tprice3']) . "</td></tr>";
$message .= "<tr><td>" . strip_tags($_POST['Item4Page']) . "</td><td>" . strip_tags($_POST['Number4']) . "</td><td>" . strip_tags($_POST['Product4']) . "</td><td>" . strip_tags($_POST['QTY4']) . "</td><td>" . strip_tags($_POST['Uprice4']) . "</td><td>" . strip_tags($_POST['Tprice4']) . "</td></tr>";
$message .= "<tr><td>" . strip_tags($_POST['Item5Page']) . "</td><td>" . strip_tags($_POST['Number5']) . "</td><td>" . strip_tags($_POST['Product5']) . "</td><td>" . strip_tags($_POST['QTY5']) . "</td><td>" . strip_tags($_POST['Uprice5']) . "</td><td>" . strip_tags($_POST['Tprice5']) . "</td></tr>";
$message .= "<tr><td>" . strip_tags($_POST['Item6Page']) . "</td><td>" . strip_tags($_POST['Number6']) . "</td><td>" . strip_tags($_POST['Product6']) . "</td><td>" . strip_tags($_POST['QTY6']) . "</td><td>" . strip_tags($_POST['Uprice6']) . "</td><td>" . strip_tags($_POST['Tprice6']) . "</td></tr>";
$message .= "<tr><td>" . strip_tags($_POST['Item7Page']) . "</td><td>" . strip_tags($_POST['Number7']) . "</td><td>" . strip_tags($_POST['Product7']) . "</td><td>" . strip_tags($_POST['QTY7']) . "</td><td>" . strip_tags($_POST['Uprice7']) . "</td><td>" . strip_tags($_POST['Tprice7']) . "</td></tr>";
$message .= "<tr><td>" . strip_tags($_POST['Item8Page']) . "</td><td>" . strip_tags($_POST['Number8']) . "</td><td>" . strip_tags($_POST['Product8']) . "</td><td>" . strip_tags($_POST['QTY8']) . "</td><td>" . strip_tags($_POST['Uprice8']) . "</td><td>" . strip_tags($_POST['Tprice8']) . "</td></tr>";
$message .= "<tr><td>" . strip_tags($_POST['Item9Page']) . "</td><td>" . strip_tags($_POST['Number9']) . "</td><td>" . strip_tags($_POST['Product9']) . "</td><td>" . strip_tags($_POST['QTY9']) . "</td><td>" . strip_tags($_POST['Uprice9']) . "</td><td>" . strip_tags($_POST['Tprice9']) . "</td></tr>";
$message .= "<tr><td>" . strip_tags($_POST['Item10Page']) . "</td><td>" . strip_tags($_POST['Number10']) . "</td><td>" . strip_tags($_POST['Product10']) . "</td><td>" . strip_tags($_POST['QTY10']) . "</td><td>" . strip_tags($_POST['Uprice10']) . "</td><td>" . strip_tags($_POST['Tprice10']) . "</td></tr>";
$message .= "<tr><td>" . strip_tags($_POST['Item11Page']) . "</td><td>" . strip_tags($_POST['Number11']) . "</td><td>" . strip_tags($_POST['Product11']) . "</td><td>" . strip_tags($_POST['QTY11']) . "</td><td>" . strip_tags($_POST['Uprice11']) . "</td><td>" . strip_tags($_POST['Tprice11']) . "</td></tr>";
$message .= "<tr><td colspan=5 align=right>Order Processing Charge </td><td align=right>$2.50</td></tr>";
$message .= "<tr><td colspan=5 align=right>Order TOTAL </td><td align=right></td></tr>";
$message .= "</table></body></html>";
$headers2 = "From: myemailaddress@live.com "; 
$subject2 = "Thank you for your order "; 
$autoreply = "Thank you for your order. We will contact you when your order is ready for delivery. ";
$send = mail($to,$subject,$message,$headers); 
$send2 = mail($from,$subject2,$autoreply,$headers2); 
if($send) 
{header( "Location: thankyou.html" );} 
else 
{print "We encountered an error sending your mail, please try again later."; } 
?> 

I have borrowed a number of ideas and this incarnation has removed the array in favour of directly injecting the variables into the table. The $send2 line works fine since the "Thank you for your order" email is received and the redirect to thankyou.html works also. However, the email with the order is not received.

Andrew Heath
  • 103
  • 4
  • What has the **layout** of the mail to do with the backend **PHP Script**? – Markus Hofmann Aug 08 '13 at 15:42
  • The script generates the mail, hence why I am asking how to use it to format the email. – Andrew Heath Aug 08 '13 at 15:51
  • @AndrewHeath You could insert a few line breaks above `QTY1, QTY2, QTY3` and use an HTML header. Such as `$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";` Have a look at the `mail();` function on => [**PHP.net**](http://php.net/manual/en/function.mail.php) there's a lot of info in there. – Funk Forty Niner Aug 08 '13 at 15:57
  • @AndrewHeath Try this out `$fields{"Product1"} = "Product1" . "\r\n";` see if that helps. I would think that theoretically, this should put a space between `Product1:` and `QTY1:` – Funk Forty Niner Aug 08 '13 at 16:02
  • It seems adding `. "\r\n"` puts the variable's value on the next line instead of next to it in the output. I cannot find a way to format the output with HTML as putting it in the PHP Array gives a processing error. All examples I have found using HTML to format an email output have static text and do not have variables. – Andrew Heath Sep 09 '13 at 08:13
  • Voting to close this question as OP has shown little understanding of PHP or the problem at hand, as per the comments on my answer, and seems to be expecting someone to write the code for him, rather than guiding him in the right direction, which he has been. – Martin Bean Sep 09 '13 at 11:43

3 Answers3

0

You can put HTML tags in your email, provided you include some headers with it. This can allow you to put a form into your email. See here

imulsion
  • 8,820
  • 20
  • 54
  • 84
  • I will give it a go, however I have had troubles following such guides. I'm not sure if it is the fact I am using an array or I am not using my variables properly. – Andrew Heath Aug 08 '13 at 15:54
  • @AndrewHeath It's very simple, just use HTML tags as though you were writing a webpage :) – imulsion Aug 08 '13 at 15:55
  • I have tried to incorporate the example in your link. Refer to the Update above. – Andrew Heath Oct 19 '13 at 12:57
0

You need to markup your email body with HTML.

As this is an email message I recommend you to use <table>s for the structuring of your form.

I see you're accessing the php arrays like $fields{"Name"}. Convert this into $fields['name'] like you did on top. Use lowercase key-names for readability and ' instead of " for performance. You can read more about this here.

Regarding the structuring, see this example (the style attributes are examples for inline styling):

<html>
<body>
<table width="600" style="border:1px solid #333">
  <tr>
    <td align="center"><!-- head --></td>
  </tr>
  <Tr>
    <td align="center">
      <!-- email body -->

      <table align="center" width="300" border="0" cellspacing="0" cellpadding="0" style="border:1px solid #ccc;">
        <tr>
          <td><!-- key --></td>
          <td><!-- value --></td>
        </tr>
      </table></td>
  </tr>
</table>
</body>
</html>

Note: use only inline styling in email html as most email readers do not parse external css properly. Read more about emails and tables here and here.

For sending the email I recommend you start using SwiftMailer. It's a PHP mailing class that makes sending mails with php easy and provides handy features for attachments and such. To implement it, you require some advanced knowledge, but the docs will help you out.

Good luck!

Community
  • 1
  • 1
Markus Hofmann
  • 3,427
  • 4
  • 21
  • 31
0

If you need to have labels right-aligned, then you can pad your string with PHP when generating the message body.

Loop through your variables, and use the str_pad function:

<?php
foreach ($orderDetails as $key => $value) {
    $body.= str_pad($key, 20) . ': ' . $value;
}

You can still send emails as plain text then and not have to resort to HTML.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201