表单数据已发布但未通过php邮件发送()

时间:2013-10-17 16:14:36

标签: forms post php

我正在创建一个工作申请表,其中有一个下拉框,用户可以从中选择一个城市并继续填写表格。此处的目标取决于用户选择的城市,然后提交表单,它将被发送到分配给该选项值的电子邮件地址。我已经成功地在“名称,电子邮件和消息”这样的简单表单字段上完成了这项工作。但是,由于我实际上已经开始创建表单和更多表单字段,因此它不再起作用。我使用了print_r($_POST);进行了几项测试。提交表单后,它将显示已发送的所有表单值,但不会使用mail()函数邮寄这些结果。 php代码如下:

    <?php


//Sanatize field inputs

if(isset($_POST['post'])) {
    // check reCAPTCHA information
        require_once('recaptchalib.php');

        $privatekey = "6LcuhsgSAAAAAJ1Xlct4dmqOT2SnlhOuwAEImAXH ";
        $resp = recaptcha_check_answer ($privatekey,
        $_SERVER["REMOTE_ADDR"],
        $_POST["recaptcha_challenge_field"],
        $_POST["recaptcha_response_field"]);
 // if CAPTCHA is correctly entered!                       
if ($resp->is_valid) {
            $headers = "From: $email\r\n";
            $headers = "Reply-To: $email\r\n";
            $headers = "MIME-Version: 1.0\r\n";
        $headers = "subject: Application\r\n";
            $headers = "Content-Type: text/html; charset=utf-8\r\n";
        $cities = $_POST['cities'];               
        $lastname    = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING); 
        $firstname  = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
        $middle = filter_var($_POST['middle'], FILTER_SANITIZE_STRING);
        $street = filter_var($_POST['street'], FILTER_SANITIZE_STRING);
        $city   = filter_var($_POST['city'], FILTER_SANITIZE_STRING);
        $state  = filter_var($_POST['state'], FILTER_SANITIZE_STRING);
        $zip    = filter_var($_POST['zip'], FILTER_SANITIZE_STRING);
        $email   = filter_var($_POST['email'], FILTER_SANITIZE_STRING); 
        $hphone = filter_var($_POST['hphone'], FILTER_SANITIZE_STRING);
        $cphone = filter_var($_POST['cphone'], FILTER_SANITIZE_STRING);
        $social = filter_var($_POST['social'], FILTER_SANITIZE_STRING);
        $referral   = filter_var($_POST['referral'], FILTER_SANITIZE_STRING);
        $position   = filter_var($_POST['position'], FILTER_SANITIZE_STRING);
        $pay    = filter_var($_POST['pay'], FILTER_SANITIZE_STRING);        

        $message = "
        New mail from:<br><br>
        Name: $firstname&nbsp;$lastname <br>
        Email: $email <br>
        Message: $position <br >
        $pay";
        //$to   = "testing@website.com";
            //Here I provide the email address for cities
        $email1 = "testin1g@website.com";
        $email2 = "testing2@website.com";
        $email3 = "testing3@website.com";
        //print_r($_POST);
            // Logic for getting the value of drop down and then sending mail.
        if ($cities == "NY"){
            $sendMail = mail($email1, $message, $headers);
                if($sendMail) {
                    echo 'Thank you ' .$firstname. ', the mail has been successfully sent!<br><br>';
                } else {
                     echo 'An error occured and the mail could not be sent.<br>
                    Please try again later.';
                }
        }
        elseif ($cities == "NC"){
            $sendMail = mail($email2, $message, $headers);
                if($sendMail) {
                    echo 'Thank you ' .$firstname. ', the mail has been successfully sent!<br><br>';
                } else {
                     echo 'An error occured and the mail could not be sent.<br>
                    Please try again later.';
                }
        } elseif ($cities == "TX"){
            $sendMail = mail($email3, $message, $headers);
            if($sendMail) {
                    echo 'Thank you ' .$firstname. ', the mail has been successfully sent!<br><br>';
                } else {
                     echo 'An error occured and the mail could not be sent.<br>
                    Please try again later.';
            }
        } elseif ($cities == "ME"){
            $sendMail = mail($email1, $message, $headers);
            if($sendMail) {
                    echo 'Thank you ' .$firstname. ', the mail has been successfully sent!<br><br>';
                } else {
                     echo 'An error occured and the mail could not be sent.<br>
                    Please try again later.';
            }
        }    
    } else {
        // handle the CAPTCHA being entered incorrectly
        echo 'You Did not enter the CAPTCHA in correctly. Please go back and do so.';
    }
}
session_destroy();  
?>

如上所示,我列出了几个电子邮件地址,然后使用if语句我告诉php如果要发送到该电子邮件地址的“cities = state”。然而,这是它被挂起的部分。它直接转到我的错误信息。

我的html表单:

<form method="post" action="../sendemail.php">

      <table cellpadding="0" cellspacing="0" width="900">
        <tr>

          <td width="800" bgcolor="White">

            <div id="copyContainerwide">
<h1>NexxLinx Career Opportunities</h1>
              <p>
               Thank you for your interest in a career with NexxLinx! We have many different opportunities for employment, which may vary depending 
               upon which center you are interested in applying for. For more information on what positions we have available, please contact your local
               NexxLinx centers, or visit us in person! </p>

 <br/>
 <p>To apply please select the center you would like to apply to:</p>

<select name="cities">
<option value="NY">Newburgh, NY</option>
<option value="NC">Jacksonville, NC</option>
<option value="TX" >Austin, TX</option>
<option value="ME">Orono, ME</option>
<option value="UT">South Jordan, UT</option>
</select>
<br/>
<p> TO THE APPLICANT: You must fully complete this application for it to be considered. Applications are active for ninety (90) days; 
              thereafter, you must personally renew the application to be considered for employment. We provide equal opportunities to all applicants 
              and employees without regard to race, color, religion, sex, national origin, age, or disability, in accordance with applicable federal 
              and state laws. Accommodation is available to applicants with a disabling condition, when applying, testing, or interviewing for a position.
              Please contact the Human Resource Department to request accommodation.</p>

 <fieldset>
 <legend>General Information </legend>
 <table>
    <tr>
        <td>Last Name:<br/>
            <input name="lastname" type="text" size="30" /></td>
        <td> First Name:<br/>
            <input name="firstname" type="text" size="30"/></td>
        <td> Middle Name:<br/>
             <input name="middle" type="text" size="30"/></td>
    </tr>
    </table>
    <table>
    <tr>
        <td>Street Address<br/>
        <input name="street" type="text" size="50" /></td>
        <td> City<br/>
        <input name="city" type="text" size="30" /></td>
        <td>State (abbr.)<br/>
         <input name="state" type="text" size="7"  /></td>
        <td> Zip Code <br/>
        <input name="zip" type="text" size="12"  /></td>
     </tr>
     </table>
     <table>
     <tr>
        <td>E-mail:<br/>
        <input name="email" type="email" size="50" /></td>
        <td>Home Phone:<br/>
        <input name="hphone" type="tel" size="15" /></td>
        <td>Cell Phone:<br/>
        <input name="cphone" type="tel" size="15" /></td>
        <td>Social Security Number<br/>
        <input name="social" type="text" size="12" /></td>
      </tr>
      </table>
      <table>
      <tr>
        <td>Referred By:</td>
        <td><input name="agency" type="checkbox" />Agency</td>
        <td><input name="advert" type="checkbox" />Advertisement</td>
        <td><input name="employee" type="checkbox" />Empoloyee</td>
        <td><input name="other" type="checkbox" />Other</td>
      </tr>
      <table>
      <tr>
        <td>Please list the name of the above agency, publication with the advertisement, employee, or other referall.<br/>
        <input name="referral" type="text" size="60" /></td>
       </tr>
       </table>
       <table>
        <tr>
        <td>Position Applied For:<br/>
        <input name="position" type="text" size="50"/></td>
        <td>Pay Desired:<br/>
        &#36;<input name="pay" type="text" size="20" /></td> 
        </tr>
        <tr>
        <td>
        <?php
    require_once('../recaptchalib.php');
    $publickey = "6LcuhsgSAAAAAHtXF5i5f5XEAZmjyAtYvHGGQlhf ";
    echo recaptcha_get_html($publickey);
?>
        </td>
        </tr>
        <tr>
        <td>
        <input type="submit" name="post" value="submit" />
</table>
 </fieldset>
 </div>
 </td>
 </tr>
 </table>
 </form>

感谢你给我的任何建议!

更新 终于有了这个工作!谢谢大家的帮助。出于某种原因,请纠正如果这是不对的,我将我的标题移到我所有其他变量的顶部,它现在就像一个魅力。仅供参考:我在WAMP本地工作,并配置了一个domeby sendmail,通过我的Gmail帐户发送。它使用非常简单的形式,但显然这个有点太高级,所以我最终上传并在我的实时服务器上完成测试。

可能有人可以帮助我的额外的东西是验证社会安全号码并在有人插入号码时显示*。可以用javascript完成吗?

1 个答案:

答案 0 :(得分:0)

为什么不将电子邮件地址分配给该区域的值如果您可以随时将其反转,如果您要将其插入数据库中

<select name="cities">
<option value="test1@yahoo.com">Newburgh, NY</option>
<option value="test2@yahoo.com">Jacksonville, NC</option>
<option value="test3@yahoo.com" >Austin, TX</option>
</select>

$to=$_POST['cities'];

mail('$to', '$subject', '$message);

如果您需要在数据库中插入位置,只需将其反转

    if($citiies==test1@yahoo.com)
{
//insert into database NY
}

这段代码更短,看起来更干净但是如果要存储的话,你需要再次使用它 数据库中的位置

它会更短,因为你不会比较值只是将它发送到值赋值是