无法让我的PHP Form Mailer工作

时间:2013-04-06 14:17:51

标签: php forms

我是PHP脚本的新手,所以当我决定在我的网站上有一个“订购单”时,我开始使用“现成的”(即Google搜索并以其他人的代码开头)表单并对其进行编辑。但是,我无法发送任何电子邮件给我。我的表单代码看起来像这样(当我的REP点足够高时,我会发布一个图像):

<form action="form.php">
Name: <input type="text" name="Name" value="" size="60"> <font color=red>*</font><br>
Address: <input type="text" name="Address" value="" size="58"> <font color=red>*</font><br>
E-mail: <input type="text" name="Email" value="" size="60"> <font color=red>*</font><br><br>
Campaign No: <input type="text" name="BookNumber" value="" size="2"> <font color=red>*</font><br><br>
<table border=1><tr><td>Page No. </td><td>Product No. </td><td>Product Name </td><td>Shade/Fragrance </td><td>QTY </td><td>Unit Price </td><td>Total Price </td></tr>
<tr><td>
    <input type="text" name="Item1Page" value="" size="3">
</td><td>
    <input type="text" name="Number1" value="" size="5">
</td><td>
    <input type="text" name="Product1" value="" size="50">
</td><td>
    <input type="text" name="Fragrance1" value="" size="15">
</td><td>
    <input type="text" name="QTY1" value="" size="2">
</td><td>
    <input type="text" name="Uprice1" value="" size="6">
</td><td>
    <input type="text" name="Tprice1" value="" size="6">
</td></tr>
<tr><td>
    <input type="text" name="Item2Page" value="" size="3">
</td><td>
    <input type="text" name="Number2" value="" size="5">
</td><td>
    <input type="text" name="Product2" value="" size="50">
</td><td>
    <input type="text" name="Fragrance2" value="" size="15">
</td><td>
    <input type="text" name="QTY2" value="" size="2">
</td><td>
    <input type="text" name="Uprice2" value="" size="6">
</td><td>
    <input type="text" name="Tprice2" value="" size="6">
</td></tr>
<tr><td>
    <input type="text" name="Item3Page" value="" size="3">
</td><td>
    <input type="text" name="Number3" value="" size="5">
</td><td>
    <input type="text" name="Product3" value="" size="50">
</td><td>
    <input type="text" name="Fragrance3" value="" size="15">
</td><td>
    <input type="text" name="QTY3" value="" size="2">
</td><td>
    <input type="text" name="Uprice3" value="" size="6">
</td><td>
    <input type="text" name="Tprice3" value="" size="6">
</td></tr>
<tr><td>
    <input type="text" name="Item4Page" value="" size="3">
</td><td>
    <input type="text" name="Number4" value="" size="5">
</td><td>
    <input type="text" name="Product4" value="" size="50">
</td><td>
    <input type="text" name="Fragrance4" value="" size="15">
</td><td>
    <input type="text" name="QTY4" value="" size="2">
</td><td>
    <input type="text" name="Uprice4" value="" size="6">
</td><td>
    <input type="text" name="Tprice4" value="" size="6">
</td></tr>
<tr><td>
    <input type="text" name="Item5Page" value="" size="3">
</td><td>
    <input type="text" name="Number5" value="" size="5">
</td><td>
    <input type="text" name="Product5" value="" size="50">
</td><td>
    <input type="text" name="Fragrance5" value="" size="15">
</td><td>
    <input type="text" name="QTY5" value="" size="2">
</td><td>
    <input type="text" name="Uprice5" value="" size="6">
</td><td>
    <input type="text" name="Tprice5" value="" size="6">
</td></tr>
<tr><td>
    <input type="text" name="Item6Page" value="" size="3">
</td><td>
    <input type="text" name="Number6" value="" size="5">
</td><td>
    <input type="text" name="Product6" value="" size="50">
</td><td>
    <input type="text" name="Fragrance6" value="" size="15">
</td><td>
    <input type="text" name="QTY6" value="" size="2">
</td><td>
    <input type="text" name="Uprice6" value="" size="6">
</td><td>
    <input type="text" name="Tprice6" value="" size="6">
</td></tr>
<tr><td>
    <input type="text" name="Item7Page" value="" size="3">
</td><td>
    <input type="text" name="Number7" value="" size="5">
</td><td>
    <input type="text" name="Product7" value="" size="50">
</td><td>
    <input type="text" name="Fragrance7" value="" size="15">
</td><td>
    <input type="text" name="QTY7" value="" size="2">
</td><td>
    <input type="text" name="Uprice7" value="" size="6">
</td><td>
    <input type="text" name="Tprice7" value="" size="6">
</td></tr>
<tr><td>
    <input type="text" name="Item8Page" value="" size="3">
</td><td>
    <input type="text" name="Number8" value="" size="5">
</td><td>
    <input type="text" name="Product8" value="" size="50">
</td><td>
    <input type="text" name="Fragrance8" value="" size="15">
</td><td>
    <input type="text" name="QTY8" value="" size="2">
</td><td>
    <input type="text" name="Uprice8" value="" size="6">
</td><td>
    <input type="text" name="Tprice8" value="" size="6">
</td></tr>
<tr><td>
    <input type="text" name="Item9Page" value="" size="3">
</td><td>
    <input type="text" name="Number9" value="" size="5">
</td><td>
    <input type="text" name="Product9" value="" size="50">
</td><td>
    <input type="text" name="Fragrance9" value="" size="15">
</td><td>
    <input type="text" name="QTY9" value="" size="2">
</td><td>
    <input type="text" name="Uprice9" value="" size="6">
</td><td>
    <input type="text" name="Tprice9" value="" size="6">
</td></tr>
<tr><td>
    <input type="text" name="Item10Page" value="" size="3">
</td><td>
    <input type="text" name="Number10" value="" size="5">
</td><td>
    <input type="text" name="Product10" value="" size="50">
</td><td>
    <input type="text" name="Fragrance10" value="" size="15">
</td><td>
    <input type="text" name="QTY10" value="" size="2">
</td><td>
    <input type="text" name="Uprice10" value="" size="6">
</td><td>
    <input type="text" name="Tprice10" value="" size="6">
</td></tr>
<tr><td>
    <input type="text" name="Item11Page" value="" size="3">
</td><td>
    <input type="text" name="Number11" value="" size="5">
</td><td>
    <input type="text" name="Product11" value="" size="50">
</td><td>
    <input type="text" name="Fragrance11" value="" size="15">
</td><td>
    <input type="text" name="QTY11" value="" size="2">
</td><td>
    <input type="text" name="Uprice11" value="" size="6">
</td><td>
    <input type="text" name="Tprice11" value="" size="6">
</td></tr>
<tr><td colspan=6 align=right>Order Processing Charge</td><td align=right>$0.80</td></tr>
</table>


<br><br>
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>

我的PHP脚本如下所示。我假设(我使用了模板并更改了它)我不小心删除了错误的行或者没有正确定义变量:

<?php
if(isset($_POST['email_out'])) {
    if(!isset($_POST['Name']) ||
        !isset($_POST['Email']) ||
        !isset($_POST['Address'])) 
    $email = $_POST['Name']; // required
    $email = $_POST['Address']; // required
    $email = $_POST['Email']; // required
    $email = $_POST['BookNumber'];     
    $email = $_POST['Item1Page'];     
    $email = $_POST['Number1'];
    $email = $_POST['Product1'];
    $email = $_POST['Fragrance1'];
    $email = $_POST['QTY1'];
    $email = $_POST['Uprice1'];
    $email = $_POST['Tprice1'];
    $email = $_POST['Item2Page'];     
    $email = $_POST['Number2'];
    $email = $_POST['Product2'];
    $email = $_POST['Fragrance2'];
    $email = $_POST['QTY2'];
    $email = $_POST['Uprice2'];
    $email = $_POST['Tprice2'];
    $email = $_POST['Item3Page'];     
    $email = $_POST['Number3'];
    $email = $_POST['Product3'];
    $email = $_POST['Fragrance3'];
    $email = $_POST['QTY3'];
    $email = $_POST['Uprice3'];
    $email = $_POST['Tprice3'];
    $email = $_POST['Item4Page'];     
    $email = $_POST['Number4'];
    $email = $_POST['Product4'];
    $email = $_POST['Fragrance4'];
    $email = $_POST['QTY4'];
    $email = $_POST['Uprice4'];
    $email = $_POST['Tprice4'];
    $email = $_POST['Item5Page'];     
    $email = $_POST['Number5'];
    $email = $_POST['Product5'];
    $email = $_POST['Fragrance5'];
    $email = $_POST['QTY5'];
    $email = $_POST['Uprice5'];
    $email = $_POST['Tprice5'];
    $email = $_POST['Item6Page'];     
    $email = $_POST['Number6'];
    $email = $_POST['Product6'];
    $email = $_POST['Fragrance6'];
    $email = $_POST['QTY6'];
    $email = $_POST['Uprice6'];
    $email = $_POST['Tprice6'];
    $email = $_POST['Item7Page'];     
    $email = $_POST['Number7'];
    $email = $_POST['Product7'];
    $email = $_POST['Fragrance7'];
    $email = $_POST['QTY7'];
    $email = $_POST['Uprice7'];
    $email = $_POST['Tprice7'];
    $email = $_POST['Item8Page'];     
    $email = $_POST['Number8'];
    $email = $_POST['Product8'];
    $email = $_POST['Fragrance8'];
    $email = $_POST['QTY8'];
    $email = $_POST['Uprice8'];
    $email = $_POST['Tprice8'];
    $email = $_POST['Item9Page'];     
    $email = $_POST['Number9'];
    $email = $_POST['Product9'];
    $email = $_POST['Fragrance9'];
    $email = $_POST['QTY9'];
    $email = $_POST['Uprice9'];
    $email = $_POST['Tprice9'];
    $email = $_POST['Item10Page'];     
    $email = $_POST['Number10'];
    $email = $_POST['Product10'];
    $email = $_POST['Fragrance10'];
    $email = $_POST['QTY10'];
    $email = $_POST['Uprice10'];
    $email = $_POST['Tprice10'];
    $email = $_POST['Item11Page'];     
    $email = $_POST['Number11'];
    $email = $_POST['Product11'];
    $email = $_POST['Fragrance11'];
    $email = $_POST['QTY11'];
    $email = $_POST['Uprice11'];
    $email = $_POST['Tprice11'];
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$name)) {
    $error_message .= 'The Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$address)) {
    $error_message .= 'The address you entered does not appear to be valid.<br />';
  }
    $email_message = "Form details below.\n\n";
    $email_message .= "Name: ".clean_string($Name)."\n";
    $email_message .= "Address: ".clean_string($Address)."\n";
    $email_message .= "Email: ".clean_string($Email)."\n";
    $email_message .= "BookNumber: ".clean_string($BookNumber)."\n";
    $email_message .= "Item1Page: ".clean_string($Item1Page)."\n";
    $email_message .= "Number1: ".clean_string($Number1)."\n";
    $email_message .= "Product1: ".clean_string($Product1)."\n";
    $email_message .= "Fragrance1: ".clean_string($Fragrance1)."\n";
    $email_message .= "QTY1: ".clean_string($QTY1)."\n";
    $email_message .= "Uprice1: ".clean_string($Uprice1)."\n";
    $email_message .= "Tprice1: ".clean_string($Tprice1)."\n";
    $email_message .= "Item2Page: ".clean_string($Item2Page)."\n";
    $email_message .= "Number2: ".clean_string($Number2)."\n";
    $email_message .= "Product2: ".clean_string($Product2)."\n";
    $email_message .= "Fragrance2: ".clean_string($Fragrance2)."\n";
    $email_message .= "QTY2: ".clean_string($QTY2)."\n";
    $email_message .= "Uprice2: ".clean_string($Uprice2)."\n";
    $email_message .= "Tprice2: ".clean_string($Tprice2)."\n";
    $email_message .= "Item3Page: ".clean_string($Item3Page)."\n";
    $email_message .= "Number3: ".clean_string($Number3)."\n";
    $email_message .= "Product3: ".clean_string($Product3)."\n";
    $email_message .= "Fragrance3: ".clean_string($Fragrance3)."\n";
    $email_message .= "QTY3: ".clean_string($QTY3)."\n";
    $email_message .= "Uprice3: ".clean_string($Uprice3)."\n";
    $email_message .= "Tprice3: ".clean_string($Tprice3)."\n";
    $email_message .= "Item4Page: ".clean_string($Item4Page)."\n";
    $email_message .= "Number4: ".clean_string($Number4)."\n";
    $email_message .= "Product4: ".clean_string($Product4)."\n";
    $email_message .= "Fragrance4: ".clean_string($Fragrance4)."\n";
    $email_message .= "QTY4: ".clean_string($QTY4)."\n";
    $email_message .= "Uprice4: ".clean_string($Uprice4)."\n";
    $email_message .= "Tprice4: ".clean_string($Tprice4)."\n";
    $email_message .= "Item5Page: ".clean_string($Item5Page)."\n";
    $email_message .= "Number5: ".clean_string($Number5)."\n";
    $email_message .= "Product5: ".clean_string($Product5)."\n";
    $email_message .= "Fragrance5: ".clean_string($Fragrance5)."\n";
    $email_message .= "QTY5: ".clean_string($QTY5)."\n";
    $email_message .= "Uprice5: ".clean_string($Uprice5)."\n";
    $email_message .= "Tprice5: ".clean_string($Tprice5)."\n";
    $email_message .= "Item6Page: ".clean_string($Item6Page)."\n";
    $email_message .= "Number6: ".clean_string($Number6)."\n";
    $email_message .= "Product6: ".clean_string($Product6)."\n";
    $email_message .= "Fragrance6: ".clean_string($Fragrance6)."\n";
    $email_message .= "QTY6: ".clean_string($QTY6)."\n";
    $email_message .= "Uprice6: ".clean_string($Uprice6)."\n";
    $email_message .= "Tprice6: ".clean_string($Tprice6)."\n";
    $email_message .= "Item7Page: ".clean_string($Item7Page)."\n";
    $email_message .= "Number7: ".clean_string($Number7)."\n";
    $email_message .= "Product7: ".clean_string($Product7)."\n";
    $email_message .= "Fragrance7: ".clean_string($Fragrance7)."\n";
    $email_message .= "QTY7: ".clean_string($QTY7)."\n";
    $email_message .= "Uprice7: ".clean_string($Uprice7)."\n";
    $email_message .= "Tprice7: ".clean_string($Tprice7)."\n";
    $email_message .= "Item8Page: ".clean_string($Item8Page)."\n";
    $email_message .= "Number8: ".clean_string($Number8)."\n";
    $email_message .= "Product8: ".clean_string($Product8)."\n";
    $email_message .= "Fragrance8: ".clean_string($Fragrance8)."\n";
    $email_message .= "QTY8: ".clean_string($QTY8)."\n";
    $email_message .= "Uprice8: ".clean_string($Uprice8)."\n";
    $email_message .= "Tprice8: ".clean_string($Tprice8)."\n";
    $email_message .= "Item9Page: ".clean_string($Item9Page)."\n";
    $email_message .= "Number9: ".clean_string($Number9)."\n";
    $email_message .= "Product9: ".clean_string($Product9)."\n";
    $email_message .= "Fragrance9: ".clean_string($Fragrance9)."\n";
    $email_message .= "QTY9: ".clean_string($QTY9)."\n";
    $email_message .= "Uprice9: ".clean_string($Uprice9)."\n";
    $email_message .= "Tprice9: ".clean_string($Tprice9)."\n";
    $email_message .= "Item10Page: ".clean_string($Item10Page)."\n";
    $email_message .= "Number10: ".clean_string($Number10)."\n";
    $email_message .= "Product10: ".clean_string($Product10)."\n";
    $email_message .= "Fragrance10: ".clean_string($Fragrance10)."\n";
    $email_message .= "QTY10: ".clean_string($QTY10)."\n";
    $email_message .= "Uprice10: ".clean_string($Uprice10)."\n";
    $email_message .= "Tprice10: ".clean_string($Tprice10)."\n";
    $email_message .= "Item11Page: ".clean_string($Item11Page)."\n";
    $email_message .= "Number11: ".clean_string($Number11)."\n";
    $email_message .= "Product11: ".clean_string($Product11)."\n";
    $email_message .= "Fragrance11: ".clean_string($Fragrance11)."\n";
    $email_message .= "QTY11: ".clean_string($QTY11)."\n";
    $email_message .= "Uprice11: ".clean_string($Uprice11)."\n";
    $email_message .= "Tprice11: ".clean_string($Tprice11)."\n";
mail("relay-hosting.secureserver.net", "Website form submission", $email_message, ""); 
}
?>

<html>
<head>
<meta http-equiv="refresh" content="5;URL=welcome.html">
</head>
<body>
Thankyou for your order.<br><br>This page will automatically refresh.
</body>
</html>

最后的一点点HTML是我的确认确认,并且是唯一可以正常工作的位。

GoDaddy要求电子邮件TO标签'relay-hosting.secureserver.net'接受并转发我的电子邮件(根据GoDaddy)作为其PHP处理的一部分。

在有人来之前说我可以在没有PHP的情况下使用直接HTML,我知道这可以做到。但是HTML会发布到电子邮件客户端,我想确保使用移动设备或不使用电子邮件客户端的人(例如通过浏览器使用Gmail)也可以使用此表单。

2 个答案:

答案 0 :(得分:0)

mail()函数的第一个参数是To:电子邮件地址,我在您的示例中看到主机名。

另外,考虑使用包含所有这些表单元素的数组,并在显示表单并获取其数据时迭代它 - 它会使代码更具可读性,我相信

答案 1 :(得分:0)

由于评论字段的功能有限,我在此处说明了我的更改。正如所建议的那样,我找到了一些书并做了一些阅读。我分别使用@DirkMcQuickly和@Alexey建议的新模板和数组。我的新代码如下所示(我删除了实际的电子邮件以保护隐私):

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

 $fields = array(); 
 $fields{"Name"} = "Name"; 
 $fields{"Address"} = "Address"; 
 $fields{"Phone"} = "Phone"; 
 $fields{"Email"} = "Email"; 
 $fields{"BookNumber"} = "Book Number"; 
 $fields{"Item1Page"} = "Page Number1"; 
 $fields{"Number1"} = "Product Number1"; 
 $fields{"Product1"} = "Product1"; 
 $fields{"Fragrance1"} = "Fragrance1"; 
 $fields{"Uprice1"} = "Unit Price1"; 
 $fields{"Tprice1"} = "Total Price1"; 
 $fields{"Item2Page"} = "Page Number2"; 
 $fields{"Number2"} = "Product Number2"; 
 $fields{"Product2"} = "Product2"; 
 $fields{"Fragrance2"} = "Fragrance2"; 
 $fields{"Uprice2"} = "Unit Price2"; 
 $fields{"Tprice2"} = "Total Price2"; 
 $fields{"Item3Page"} = "Page Number3"; 
 $fields{"Number3"} = "Product Number3"; 
 $fields{"Product3"} = "Product3"; 
 $fields{"Fragrance3"} = "Fragrance3"; 
 $fields{"Uprice3"} = "Unit Price3"; 
 $fields{"Tprice3"} = "Total Price3"; 
 $fields{"Item4Page"} = "Page Number4"; 
 $fields{"Number4"} = "Product Number4"; 
 $fields{"Product4"} = "Product4"; 
 $fields{"Fragrance4"} = "Fragrance4"; 
 $fields{"Uprice4"} = "Unit Price4"; 
 $fields{"Tprice4"} = "Total Price4"; 
 $fields{"Item5Page"} = "Page Number5"; 
 $fields{"Number5"} = "Product Number5"; 
 $fields{"Product5"} = "Product5"; 
 $fields{"Fragrance5"} = "Fragrance5"; 
 $fields{"Uprice5"} = "Unit Price5"; 
 $fields{"Tprice5"} = "Total Price5"; 
 $fields{"Item6Page"} = "Page Number6"; 
 $fields{"Number6"} = "Product Number6"; 
 $fields{"Product6"} = "Product6"; 
 $fields{"Fragrance6"} = "Fragrance6"; 
 $fields{"Uprice6"} = "Unit Price6"; 
 $fields{"Tprice6"} = "Total Price6"; 
 $fields{"Item7Page"} = "Page Number7"; 
 $fields{"Number7"} = "Product Number7"; 
 $fields{"Product7"} = "Product7"; 
 $fields{"Fragrance7"} = "Fragrance7"; 
 $fields{"Uprice7"} = "Unit Price7"; 
 $fields{"Tprice7"} = "Total Price7"; 
 $fields{"Item8Page"} = "Page Number8";  
 $fields{"Number8"} = "Product Number8"; 
 $fields{"Product8"} = "Product8"; 
 $fields{"Fragrance8"} = "Fragrance8"; 
 $fields{"Uprice8"} = "Unit Price8"; 
 $fields{"Tprice8"} = "Total Price8"; 
 $fields{"Item9Page"} = "Page Number9"; 
 $fields{"Number9"} = "Product Number9"; 
 $fields{"Product9"} = "Product9"; 
 $fields{"Fragrance9"} = "Fragrance9"; 
 $fields{"Uprice9"} = "Unit Price9"; 
 $fields{"Tprice9"} = "Total Price9"; 
 $fields{"Item10Page"} = "Page Number10"; 
 $fields{"Number10"} = "Product Number10"; 
 $fields{"Product10"} = "Product10"; 
 $fields{"Fragrance10"} = "Fragrance10"; 
 $fields{"Uprice10"} = "Unit Price10"; 
 $fields{"Tprice10"} = "Total Price10"; 
 $fields{"Item11Page"} = "Page Number11"; 
 $fields{"Number11"} = "Product Number11"; 
 $fields{"Product11"} = "Product11"; 
 $fields{"Fragrance11"} = "Fragrance11"; 
 $fields{"Uprice11"} = "Unit Price11"; 
 $fields{"Tprice11"} = "Total Price11"; 

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

 $headers2 = "From: fromaddress@address.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 you have any questions, feel free to contact us.";

 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: thankyou.html" );} 
 else 
 {print "We encountered an error sending your mail, please try again later."; } 
 }
}
 ?> 

这个新脚本甚至包含一封确认电子邮件,我决定使用一个专门的'thankyou'页面,其中包含一个链接,而不是像之前一样标记的嵌入式HTML标签。现在就开始享受吧!接下来我会看看我是否能够格式化输出。