联系表格无法发送电子邮件

时间:2015-06-11 08:18:42

标签: php email contact-form

在用有效信息填写联系表格并点击提交后,我收到了成功的消息!但是,在检查我的收件箱时,我不会收到电子邮件。 有什么问题?

P.S。我检查了我的网络服务器。它发送邮件但这个联系表格失败了。

Contact Form

<section class="span8 ">
    <div class="col-right">
        <hr>
        <h4>General Enquire or Apply</h4>

        <div id="message-contact"></div>
        <form method="post" action="assets/contact.php" id="contactform">
            <div class="row">
                <div class="span3">
                    <label>Name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" id="name_contact">
                </div>
                <div class="span3">
                    <label>Last name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" id="lastname_contact">
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Email <span class="required">* </span></label>
                    <input type="email" id="email_contact" class="span3 ie7-margin">
                </div>
                <div class="span3">
                    <label>Phone <span class="required">* </span></label>
                    <input type="text" id="phone_contact" class="span3 ie7-margin">
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Select a department</label>
                    <select id="subject_contact" class="span3">
                        <option value="Administration">Administration</option>
                        <option value="Admissions">Admissions</option>
                        <option value="Courses">Courses</option>
                        <option value="Apply">Apply</option>
                    </select>
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Message <span class="required">*</span></label>
                    <textarea rows="5" id="message_contact" class="span6"></textarea>
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label><span class="required">*</span> Are you human? 3 + 1 =</label>
                    <input type="text" id="verify_contact" class="span2">
                </div>
                <div class="button-align span3">
                    <input type="submit" id="submit-contact" value="Submit" class=" button_medium">
                </div>
            </div>
            <hr>
        </form>

        <h4>Plan a visit</h4>
        <div id="message-visit"></div>
        <form method="post" action="assets/visit.php" id="visit">
            <div class="row">
                <div class="span3">
                    <label>Name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" id="name_visit">
                </div>
                <div class="span3">
                    <label>Last name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" id="lastname_visit">
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Email <span class="required">* </span></label>
                    <input type="email" id="email_visit" class="span3 ie7-margin">
                </div>
                <div class="span3">
                    <label>Phone <span class="required">* </span></label>
                    <input type="text" id="phone_visit" class="span3 ie7-margin">
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <div id="datetimepicker" class="input-append">
                        <label>Select a date <span class="required">* </span></label>
                        <input type="text" class=" dateinput" id="date_visit" readonly>
                        <span class="add-on"><i data-time-icon="icon-time" data-date-icon="icon-calendar"></i></span>
                    </div>
                </div>
                <div class="span3">
                    <label><span class="required">*</span> Are you human? 3 + 1 =</label>
                    <input type="text" id="verify_visit" class="span2">
                </div>
            </div>
            <!-- end row-->
            <input type="submit" id="submit-visit" value="Submit" class=" button_medium">
        </form>

    </div><!-- end col right-->
    </section>



contact.php

<?php

if(!$_POST) exit;

// Email verification, do not edit.
function isEmail($email_contact ) {
    return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email_contact ));
}

if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

$name_contact     = $_POST['name_contact'];
$lastname_contact    = $_POST['lastname_contact'];
$email_contact    = $_POST['email_contact'];
$phone_contact   = $_POST['phone_contact'];
$subject_contact  = $_POST['subject_contact'];
$message_contact = $_POST['message_contact'];
$verify_contact   = $_POST['verify_contact'];

if(trim($name_contact) == '') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i> You must enter your Name.</div>';
    exit();
} else if(trim($lastname_contact ) == '') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i>You must enter your Last name.</div>';
    exit();
} else if(trim($email_contact) == '') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i>Please enter a valid email address.</div>';
    exit();
} else if(!isEmail($email_contact)) {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i> You have enter an invalid e-mail address, try again.</div>';
    exit();
    } else if(trim($phone_contact) == '') {
    echo '<div class="error_message">Please enter a valid phone number.</div>';
    exit();
} else if(!is_numeric($phone_contact)) {
    echo '<div class="error_message">Phone number can only contain numbers.</div>';
    exit();
}
if(trim($subject_contact) == '') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i> Please select a department.</div>';
    exit();
} else if(trim($message_contact) == '') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i> Please enter your message.</div>';
    exit();
} else if(!isset($verify_contact) || trim($verify_contact) == '') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i> Please enter the verification number.</div>';
    exit();
} else if(trim($verify_contact) != '4') {
    echo '<div class="error_message"><i class=" icon-warning-sign"></i> The verification number you entered is incorrect.</div>';
    exit();
}

if(get_magic_quotes_gpc()) {
    $message_contact = stripslashes($message_contact);
}


//$address = "HERE your email address";
$address = "staff@domain.com";


// Below the subject of the email
$e_subject = 'You\'ve been contacted by ' . $name_contact . '.';

// You can change this if you feel that you need to.
$e_body = "You have been contacted by $name_contact $lastname_contact with attention to $subject_contact, with additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$message_contact\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $lastname_contact via email, $email_contact or via phone $phone_contact";

$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

$headers = "From: $email_contact" . PHP_EOL;
$headers .= "Reply-To: $email_contact" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

if(mail($address, $e_subject, $msg, $headers)) {

    // Success message
    echo "<div id='success_page' style='padding:20px'>";
    echo "<strong >Email Sent.</strong>";
    echo "Thank you <strong>$name_contact</strong>,<br> your message has been submitted. We will contact you shortly.";
    echo "</div>";

} else {

    echo 'ERROR!';

}
?>

2 个答案:

答案 0 :(得分:0)

您需要为每个控制器添加name属性。

实施例

<input type="text" class="span3 ie7-margin" id="name_contact">

替换为

<input type="text" class="span3 ie7-margin" name="name_contact" id="name_contact">

[name =“name_contact”]已添加

=====

如果您收到成功消息但未发送邮件,请检查邮件服务器日志 C:\ XAMPP \阿帕奇\ mailoutput

如果mail()返回true,那么php job就完成了。有服务器端问题。

答案 1 :(得分:0)

您必须使用&#34; name&#34;指定输入名称。参数,而不是id。所以替换&#34; id&#34;用&#34;名称&#34;。

替换此

<form method="post" action="assets/contact.php" id="contactform">
            <div class="row">
                <div class="span3">
                    <label>Name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" id="name_contact">
                </div>
                <div class="span3">
                    <label>Last name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" id="lastname_contact">
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Email <span class="required">* </span></label>
                    <input type="email" id="email_contact" class="span3 ie7-margin">
                </div>
                <div class="span3">
                    <label>Phone <span class="required">* </span></label>
                    <input type="text" id="phone_contact" class="span3 ie7-margin">
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Select a department</label>
                    <select id="subject_contact" class="span3">
                        <option value="Administration">Administration</option>
                        <option value="Admissions">Admissions</option>
                        <option value="Courses">Courses</option>
                        <option value="Apply">Apply</option>
                    </select>
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Message <span class="required">*</span></label>
                    <textarea rows="5" id="message_contact" class="span6"></textarea>
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label><span class="required">*</span> Are you human? 3 + 1 =</label>
                    <input type="text" id="verify_contact" class="span2">
                </div>
                <div class="button-align span3">
                    <input type="submit" id="submit-contact" value="Submit" class=" button_medium">
                </div>
            </div>
            <hr>
        </form>

<form method="post" action="assets/contact.php" id="contactform">
            <div class="row">
                <div class="span3">
                    <label>Name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" name="name_contact"><!-- Replaced id with name -->
                </div>
                <div class="span3">
                    <label>Last name <span class="required">* </span></label>
                    <input type="text" class="span3 ie7-margin" name="lastname_contact"><!-- Replaced id with name -->
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Email <span class="required">* </span></label>
                    <input type="email" name="email_contact" class="span3 ie7-margin"><!-- Replaced id with name -->
                </div>
                <div class="span3">
                    <label>Phone <span class="required">* </span></label>
                    <input type="text" name="phone_contact" class="span3 ie7-margin"><!-- Replaced id with name -->
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Select a department</label>
                    <select name="subject_contact" class="span3"><!-- Replaced id with name -->
                        <option value="Administration">Administration</option>
                        <option value="Admissions">Admissions</option>
                        <option value="Courses">Courses</option>
                        <option value="Apply">Apply</option>
                    </select>
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label>Message <span class="required">*</span></label>
                    <textarea rows="5" name="message_contact" class="span6"></textarea><!-- Replaced id with name -->
                </div>
            </div>
            <div class="row">
                <div class="span3">
                    <label><span class="required">*</span> Are you human? 3 + 1 =</label>
                    <input type="text" name="verify_contact" class="span2"><!-- Replaced id with name -->
                </div>
                <div class="button-align span3">
                    <input type="submit" name="submit-contact" value="Submit" class=" button_medium"><!-- Replaced id with name -->
                </div>
            </div>
            <hr>
        </form>

关于在php中发送电子邮件的方式,请在此处查看这个非常详细的答案:PHP mail form doesn't complete sending e-mail