PHP自助电子邮件表单不起作用

时间:2014-03-11 21:18:48

标签: php html email

所以我一直在尝试使用PHP脚本在填写页面表单时向自己发送电子邮件。该页面的一个例子可以在这里找到:
http://tonybenwhite.byethost8.com/WSAWebpage/Action.html
托管服务允许PHP,我的Web开发讲师说PHP看起来正确,但电子邮件不会发送。这是PHP脚本和HTML表单:

<?php
    if(isset($_POST['send'])){
        $to_address="my@email.com";
        $subject="WSA Day of Action Entry";
        $firstname=$_POST['firstname'];
        $lastname=$_POST['lastname'];
        $email=$_POST['email'];
        $street1=$_POST['street1'];
        $street2=$_POST['street2'];
        $city=$_POST['city'];
        $state=$_POST['state'];
        $zip=$_POST['zip'];
        $initials=$_POST['initials'];
        $message="Name: " .$firstname." ".$lastname."\n";
        $message .="Email: " .$email."\n";
        $message .="Street: " .$street1."\n";
        $message .="Street: " .$street2."\n";
        $message .="City: " .$city."\n";
        $message .="State: " .$state."\n";
        $message .="Zip Code: " .$zip."\n";
        $message .="Initials: " .$initials."\n";
        $headers = 'From: '.$email."\r\n".
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        'X-Mailer: PHP/' .phpversion();
        mail($to_address, $subject, $message, $headers);
    }
?>


    <form name="infoForm" method="post" action="email.php">
        <fieldset class="formfield">
            <p class="form">
                Are you a registered voter in the state of Washington?<br>
                <input type="checkbox" id="yesBox"/> Yes<br><br>

                First Name: <input type="text" id="firstName" name="firstName"><br>
                Last Name: <input type="text" id="lastName" name="lastName"><br><br>

                Email: <input type="text" id="email" name="email" size="30"><br><br>
                Street Address: &nbsp;&nbsp;<input type="text" id="street1" name="street1" size="30"><br>
                <input type="text" id="street2" name="street2" size="30"><br>
                City: <input type="text" id="city" name="city" size="30"><br>
                State: <input type="text" id="state" name="state" size="30"><br>
                Zip Code: <input type="text" id="zip" name="zip" size="30"><br><br>
                Initials: <input type="text" id="initials" name="initials" size="3"><br><br>

                <input type="submit" name="send" class="inputButton" id="send" value="Submit" disabled=true/> <input type="reset" name="resetFields" class="inputButton" id="reset" value="Reset"/><label id="disableLabel"> <i><font size="2px">Please confirm you are a WA State Voter!</font></i></label>
            </p>
        </fieldset>
    </form>

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:0)

修改:您提供的链接确实让JS与您的提交按钮配合使用。

忽略最初作为删除线发布的内容,但继续阅读通过该内容。

我不知道这是你的完整代码和实际代码,是否有JS来检查是否所有字段都填满了,但这个disabled=true在:

<input type="submit" name="send" class="inputButton" id="send" value="Submit" disabled=true/>

提交按钮无法点击,因为提交为disabled /设置为true

这不会阻止邮件完成其工作,但是您的某些表单元素与POST变量不匹配。

例如:

$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];

以及name="firstName"name="lastName"

这些是区分大小写的,因此它们应分别更改为name="firstname"name="lastname"

在两个单独的文件中使用以下内容已成功发送和接收。我想知道你是否在同一个文件中使用了整个代码。

如果是,请使用action=""代替email.php 如果您的文件未命名为email.php

PHP

<?php
    if(isset($_POST['send'])){
        $to_address="my@email.com";
        $subject="WSA Day of Action Entry";
        $firstname=$_POST['firstname'];
        $lastname=$_POST['lastname'];
        $email=$_POST['email'];
        $street1=$_POST['street1'];
        $street2=$_POST['street2'];
        $city=$_POST['city'];
        $state=$_POST['state'];
        $zip=$_POST['zip'];
        $initials=$_POST['initials'];
        $message="Name: " .$firstname." ".$lastname."\n";
        $message .="Email: " .$email."\n";
        $message .="Street: " .$street1."\n";
        $message .="Street: " .$street2."\n";
        $message .="City: " .$city."\n";
        $message .="State: " .$state."\n";
        $message .="Zip Code: " .$zip."\n";
        $message .="Initials: " .$initials."\n";
        $headers = 'From: '.$email."\r\n".
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        'X-Mailer: PHP/' .phpversion();
        mail($to_address, $subject, $message, $headers);
    }
?>

HTML表单:

<form name="infoForm" method="post" action="email.php">
    <fieldset class="formfield">
        <p class="form">
            Are you a registered voter in the state of Washington?<br>
            <input type="checkbox" id="yesBox"/> Yes<br><br>

            First Name: <input type="text" id="firstname" name="firstname"><br>
            Last Name: <input type="text" id="lastname" name="lastname"><br><br>

            Email: <input type="text" id="email" name="email" size="30"><br><br>
            Street Address: &nbsp;&nbsp;<input type="text" id="street1" name="street1" size="30"><br>
            <input type="text" id="street2" name="street2" size="30"><br>
            City: <input type="text" id="city" name="city" size="30"><br>
            State: <input type="text" id="state" name="state" size="30"><br>
            Zip Code: <input type="text" id="zip" name="zip" size="30"><br><br>
            Initials: <input type="text" id="initials" name="initials" size="3"><br><br>

            <input type="submit" name="send" class="inputButton" id="send" value="Submit" />
            <input type="reset" name="resetFields" class="inputButton" id="reset" value="Reset"/>
            <label id="disableLabel"> <i><font size="2px">Please confirm you are a WA State Voter!</font></i></label>
        </p>
    </fieldset>
</form>