---
title: "PHP写的提交表单到E-mail邮箱程序"
date: 2010-03-08T14:16:39.000Z
tags: ["E-mail", "PHP", "php mail", "smtp", "邮箱"]
categories: ["技术"]
canonical: https://www.zhaojian.net/php-submit-form-to-email/
author: 赵健
---

最近网站改版，想弄一个在线提交表单到邮箱的程序，找了很多都用不了，最后自己研究了一个。


```
<html>
<body>
<?php
function spamcheck($field)
  {
  //filter_var() sanitizes the e-mail 
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);

  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
  else
    {
    return FALSE;
    }
  }

if (isset($_REQUEST['w_14']))
  {//if "email" is filled out, proceed

  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST['w_14']);
  $w_1_check = $_REQUEST['w_1'];
  $w_8_check = $_REQUEST['w_8'];
  $w_13_check = $_REQUEST['w_13'];
  if ($mailcheck==FALSE)
    {
    echo "<script language=\"JavaScript\">\r\n"; 
    echo " alert(\"请您认真填写 E-Mail，以便工作人员与您取得联系。谢谢合作！\");\r\n"; 
    echo " history.back();\r\n"; 
    echo "</script>"; 
    }
          if ($w_1_check=='')
    {
    echo "<script language=\"JavaScript\">\r\n"; 
    echo " alert(\"请您认真填写公司名称，以便工作人员与您取得联系。谢谢合作！\");\r\n"; 
    echo " history.back();\r\n"; 
    echo "</script>"; 
    }
          if ($w_8_check=='')
    {
    echo "<script language=\"JavaScript\">\r\n"; 
    echo " alert(\"请您认真填写姓名，以便工作人员与您取得联系。谢谢合作！\");\r\n"; 
    echo " history.back();\r\n"; 
    echo "</script>"; 
    }         
    	 if ($w_13_check=='')
    {
    echo "<script language=\"JavaScript\">\r\n"; 
    echo " alert(\"请您认真填写QQ/MSN号码，以便工作人员与您取得联系。谢谢合作！\");\r\n"; 
    echo " history.back();\r\n"; 
    echo "</script>"; 
    }
  else
    {//send email
    $email = $_REQUEST['w_14'] ; 
    $subject = $_REQUEST['w_1'] . " " . $_REQUEST['w_8'] ;
    $message = "公司名称 :". $_REQUEST['w_1'] . "\r\n" . "网页语种 :". $_REQUEST['w_2_1'] . "/" . $_REQUEST['w_2_2'] . "/" . $_REQUEST['w_2_3'] . "/" . $_REQUEST['w_2_4'] . "/" . $_REQUEST['w_2_5'] . "/" . "\r\n" . "需要借鉴的站点 :". $_REQUEST['w_3'] . "\r\n" . "主要业务介绍 :". $_REQUEST['w_4'] . "\r\n" . "建站费用预算 :". $_REQUEST['w_5'] . "-" . $_REQUEST['w_6'] . "\r\n" . "要求建站完成时间 :". "希望完工的日期" . $_REQUEST['w_7'] . "\r\n" . "                  希望完工的天数" . $_REQUEST['w_7_1'] . "\r\n" . "网站建设其他要求 :". $_REQUEST['w_7_2'] . "\r\n" . "=============================================================" . "\r\n" . "姓名 :". $_REQUEST['w_8'] . "\r\n" . "性别 :". $_REQUEST['w_9'] . "\r\n" . "电话 :". $_REQUEST['w_11'] . "\r\n" . "手机 :". $_REQUEST['w_12'] . "\r\n" . "QQ/MSN :". $_REQUEST['w_13'] . "\r\n" . "电子邮箱 :". $_REQUEST['w_14'] . "\r\n" . "Skype :". $_REQUEST['w_15'] . "\r\n" . "邮编 :". $_REQUEST['w_16'] . "\r\n" . "详细地址 :". $_REQUEST['w_17'] . "\r\n";
    mail("admin@zhaojian.net", "$subject 填写的网站建设意向表。",
    $message, "From: $email");
    echo "<script language=\"JavaScript\">\r\n";   
    echo "alert(\"信息提交成功！请等待工作人员与您取得联系。自动返回首页\");\r\n";   
    echo "location.replace(\"http://www.zhaojian.net/\");\r\n";   
    echo "</script>";
    }
  }
else
  {//if "email" is not filled out, display the form
  echo "<form id='form1' action='http://www.zhaojian.net/b/b.php' method='post' name='form1'>
    <table class='table_1' cellspacing='0' cellpadding='0' width='675' border='0'>
        <tbody>
            <tr>
                <th colspan='2'>网站建设申请表</th>
            </tr>
            <tr>
                <td class='input_1' width='125'>公司名称</td>
                <td class='input_2' width='550'><input class='input_text' style='width: 300px' name='w_1' type='text' /></td>
            </tr>
            <tr>
                <td class='input_1' width='125'>网页语种</td>
                <td class='input_2' width='550'><input type='checkbox' checked='checked' name='w_2_1' value='中文简体' /> <label for='w_2_1'>中文简体</label> <input type='checkbox' name='w_2_2' value='中文繁体' /> <label for='w_2_2'>中文繁体</label> <input type='checkbox' name='w_2_3' value='英文' /> <label for='w_2_3'>英文</label> <input type='checkbox' name='w_2_4' value='日文' /> <label for='w_2_4'>日文</label> <input type='checkbox' name='w_2_５' value='其它语言' /> <label for='w_2_5'>其它语言</label></td>
            </tr>
            <tr>
                <td class='input_1' width='125'>希望借鉴的站点</td>
                <td class='input_2' width='550'><input style='width: 300px' name='w_3' type='text' /></td>
            </tr>
            <tr>
                <td class='input_1' width='125'>主要业务介绍</td>
                <td class='input_2' width='550'><textarea class='input_text' style='width: 300px' name='w_4'></textarea></td>
            </tr>
            <tr>
                <td class='input_1' width='125'>建站费用预算</td>
                <td class='input_2' width='550'><input class='input_text' name='w_5' type='text' /> -- <input class='input_text' name='w_6' type='text' /> 元</td>
            </tr>
            <tr>
                <td class='input_1' width='125'>要求建站完成时间</td>
                <td class='input_2' width='550'><input class='input_text' name='w_7' value='希望完工的日期' type='text' /> <input class='input_text' name='w_7_1' value='希望完工的天数' type='text' /> 天</td>
            </tr>
            <tr>
                <td class='input_1' width='125'>网站建设其它要求</td>
                <td class='input_2' width='550'><textarea class='input_text' style='width: 300px' name='w_7_2'></textarea></td>
            </tr>
        </tbody>
    </table>
    <table class='table_1' cellspacing='0' cellpadding='0' width='675' border='0'>
        <tbody>
            <tr>
                <th colspan='4'>联系人信息</th>
            </tr>
            <tr>
                <td class='input_1' width='129'>姓　　名</td>
                <td class='input_2' width='202'><input name='w_8' type='text' /></td>
                <td class='input_1' width='96'>性　　别</td>
                <td class='input_2' width='246'><input type='radio' checked='checked' name='w_9' value='小姐' />小姐 <input type='radio' name='w_9' value='先生' />先生</td>
            </tr>
            <tr>
                <td class='input_1' align='left'>电　　话</td>
                <td class='input_2'><input class='input_text' name='w_11' type='text' /></td>
                <td class='input_1'>手　　机</td>
                <td class='input_2'><input name='w_12' type='text' /></td>
            </tr>
            <tr>
                <td class='input_1'>QQ / MSN</td>
                <td class='input_2'><input class='input_text' name='w_13' type='text' /></td>
                <td class='input_1'>电子邮箱</td>
                <td class='input_2'><input name='w_14' type='text' /></td>
            </tr>
            <tr>
                <td class='input_1'>Skype</td>
                <td class='input_2'><input class='input_text' name='w_15' type='text' /></td>
                <td class='input_1'>邮　　编</td>
                <td class='input_2'><input name='w_16' type='text' /></td>
            </tr>
            <tr>
                <td class='input_1'>详细地址</td>
                <td class='input_2'><input name='w_17' type='text' /></td>
                <td class='input_1'>公司地址</td>
                <td class='input_2'><input name='w_172' type='text' /></td>
            </tr>
        </tbody>
    </table>

    <table class='table_1' cellspacing='0' cellpadding='0' width='675' border='0'>
        <tbody>
            <tr>
                <td class='input_2'>
                <div align='center'><input id='send_mail_agent' type='submit' name='w_18' value='Submit' /></div>
                </td>
            </tr>
        </tbody>
    </table>
  </form>";
  }
?>

</body>
</html>
```