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.

4

3 回答 3

0

您可以在电子邮件中放置 HTML 标签,前提是您在其中包含一些标题。这可以让您将表格放入电子邮件中。看这里

于 2013-08-08T15:42:54.270 回答
0

您需要使用 HTML标记您的电子邮件正文。

由于这是一封电子邮件,我建议您使用<table>s来构建表单。

我看到您正在访问 php 数组,例如$fields{"Name"}. 把它转换成$fields['name']你在上面所做的。使用小写键名是为了提高可读性,'而不是"为了提高性能。您可以在此处阅读有关此内容的更多信息。

关于结构,请参阅此示例(样式属性是内联样式的示例)

<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>

注意:仅在电子邮件 html 中使用内联样式,因为大多数电子邮件阅读器无法正确解析外部 css。在此处此处阅读有关电子邮件和表格的更多信息。

对于发送电子邮件,我建议您开始使用SwiftMailer。它是一个 PHP 邮件类,使使用 php 发送邮件变得容易,并为附件等提供了方便的功能。要实现它,您需要一些高级知识,但文档会帮助您。

祝你好运!

于 2013-08-08T15:56:37.627 回答
0

如果您需要使标签右对齐,那么您可以在生成消息正文时使用 PHP 填充您的字符串。

遍历你的变量,并使用str_pad函数:

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

您仍然可以以纯文本形式发送电子邮件,而不必求助于 HTML。

于 2013-08-08T16:02:47.280 回答