带有重定向的PHP电子邮件表单

时间:2016-10-08 14:40:07

标签: php email

首先让我开始,我不太了解php和html。我想创建一个重定向到主页的PHP表单。任何时候我填写联系表格,它不会发送电子邮件,也不会在页面上做任何事情。
这是html:

<form action="contact-form.php" method="post" class="contact-form" id="contact-form">
<fieldset class="form-rows row">
    <div class="form-row column medium-6">
        <label for="contact-name">Your name</label>
        <input type="text" name="name" id="contact-name" placeholder="e.g. John Smith"> </div>
    <div class="form-row column medium-6">
        <label for="contact-email">Email address</label>
        <input type="email" name="email" id="contact-email" placeholder="e.g. john.smith@abc.com"> </div>
    <div class="form-row column medium-12 half">
        <label for="contact-phone">Phone number
            <br><small>We promise we will only use your phone number to contact you regarding this message.</small></label>
        <input type="tel" name="phone" id="contact-phone" class="half" placeholder="e.g. 123 456 7890">
    </div>
</fieldset>
<fieldset class="for-rows row">
    <div class="form-row column medium-12">
        <label for="contact-subject">Message subject</label>
        <select name="subject" id="contact-subject">
            <option value="">Please select one</option>
            <option value="1">General enquiry</option>
            <option value="2">I'd like to say thank you!</option>
            <option value="2">Something wasn't right with my order!</option>
        </select>
    </div>
</fieldset>
<fieldset class="form-rows row">
    <div class="form-row column">
        <label for="contact-message">Your message</label>
        <textarea name="message" id="contact-message" rows="8" placeholder="Enter message here &hellip;"></textarea>
    </div>
    <div class="form-row column">
        <input type="checkbox" name="newsletter" id="contact-newsletter" placeholder="">
        <label for="contact-newsletter">I would like to receive information about news, promotions, customer surveys, competitions, exclusive offers, events.</label>
    </div>
    <div class="form-row form-button column">
        <button type="submit" class="button-orange">Send Message</button>
    </div>
    <div class="column">
        <h3>Your information</h3>
            <p>We will use your information for dealing with your request and, if you agree, to contact you with information about news, promotions, customer surveys, competitions, exclusive offers, events and other information we think may be of interest to you. We will not share your information for marketing purposes with any third party.</p>
    </div>
</fieldset>
</form>

PHP:

<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_phone = $_POST['phone'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];

$mail_to = 'email@example.com';
 $subject = $field_subject;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
 $headers .= 'Reply-To: '.$field_email."\r\n";

 $mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
    window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
    window.location = 'contact.html';
</script>
<?php
}?>

1 个答案:

答案 0 :(得分:0)

你的重定向毫无意义,你应该像这样使用php标题:

b(9, std::make_array(2,3));

邮件功能看起来不错,我建议您使用phpmailer,因为邮件需要正确接受正确的标题。

你可以做var_dump($ _ POST);死();看看你是否正确接收了_POST。

也是mail()= true doesnt mean it reached its destination

你的问题可能是服务器而不是php

你在本地服务器上这样做吗?这不起作用,使用test mail server进行调试。

你是在主持人那里做的吗?检查php.ini权限,尝试使用简单的脚本if ($mail_status) { header('Location: index.html'); } else { header('Location: contact.html'); } 检查响应以及它是否实际传递。