发送电子邮件不发送其他字段PHP

时间:2013-08-30 06:26:55

标签: php html

所以这是我的PHP代码:

发送-email.php

<?php 
$ToEmail = 'email@mail.com';
$EmailSubject = 'Request Lawn Mowing Quote';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader. = "Reply-To: ".$_POST["email"]."\r\n";
$mailheader. = "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";

$MESSAGE_BODY. = "<br>Country/State: ".$_POST["states"]."";
$MESSAGE_BODY. = "<br>Suburb: ".$_POST["suburb"]."";
$MESSAGE_BODY. = "<br>Unit/Street No.: ".$_POST["unit"]."";
$MESSAGE_BODY. = "<br>Street Name: ".$_POST["street"]."";
$MESSAGE_BODY. = "<br>Phone: ".$_POST["phone"]."";
$MESSAGE_BODY. = "<br> Email: ".$_POST["email"]."";
$MESSAGE_BODY. = "<br> Service: ".$_POST["service"]."";
$MESSAGE_BODY. = "<br> Comment: ".nl2br($_POST["comment"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die("Failure");
?>

Message was sent successfully! <a href="index.html">Go back</a>

html

<form method="POST" action="send-email.php">
    <p>
        <label for="name">Name *</label>
        <input type="text" size="40" id="name" name="name" />
    </p>
    <p>
        <label for="state">Country / State *</label>
        <select id="states" name="states">
            <option value="Australian Capital Territory">Australian Capital Territory</option>
            <option value="New South Wales">New South Wales</option>
            <option value="Victoria">Victoria</option>
            <option value="Quensland">Quensland</option>
            <option value="South Australia">South Australia</option>
            <option value="Western Australia">Western Australia</option>
            <option value="Tasmania">Tasmania</option>
            <option value="Northern Australia">Northern Australia</option>
        </select>
    </p>
    <p>
        <label>Suburb *</label>
        <input type="text" name="suburb" size="40" />
    </p>
    <p>
        <label>Unit / Street No *</label>
        <input type="text" name="unit" size="40" />
    </p>
    <p>
        <label>Street Name *</label>
        <input type="text" name="street" size="40" />
    </p>
    <p>
        <label>Preferred Phone *</label>
        <input type="text" name="phone" size="40" />
    </p>
    <p>
        <label>Email *</label>
        <input type="text" name="email" size="40" />
    </p>
    <p><strong>Please tick the service you require *</strong>
    </p>
    <p>
        <label class="label-checkbox">
            <input type="checkbox" name="service" value="Mowing & Slashing, Workcover & Insurance Jobs"
            size="40" />Mowing & Slashing, Workcover & Insurance Jobs</label>
    </p>
    <p>
        <label class="label-checkbox">
            <input type="checkbox" name="service" value="Ride on Mowing" size="40"
            />Ride on Mowing</label>
    </p>
    <br />
    <br />
    <br />
    <p>
        <label class="label-checkbox">
            <input type="checkbox" name="service" value="Gardening, Weeding, Clean Up of Overgrown Gardens, Spraying, Mulch, Vegetable Beds"
            size="40" />Gardening, Weeding, Clean Up of Overgrown Gardens, Spraying, Mulch, Vegetable
            Beds</label>
    </p>
    <br />
    <br />
    <p>
        <label class="label-checkbox">
            <input type="checkbox" name="service" value="Rubbish Removal" size="40"
            />Rubbish Removal</label>
    </p>
    <br />
    <p>
        <label class="label-checkbox">
            <input type="checkbox" name="service" value="Single Storey Gutter Cleaning"
            size="40" />Single Storey Gutter Cleaning</label>
    </p>
    <br />
    <p>
        <label class="label-checkbox">
            <input type="checkbox" name="service" value="Double Storey Gutter Cleaning"
            size="40" />Double Storey Gutter Cleaning</label>
    </p>
    <br />
    <p>
        <label class="label-checkbox">
            <input type="checkbox" name="service" value="Pruning & Hedge Trimming, Small Trees/Shrubs Pruned & Removed, Chainsaw Jobs"
            size="40" />Pruning & Hedge Trimming, Small Trees/Shrubs Pruned & Removed, Chainsaw
            Jobs</label>
    </p>
    <br />
    <p>
        <label class="label-checkbox">
            <input type="checkbox" name="service" value="Lawn Care, Coring, Vertimowing, Dethatching"
            size="40" />Lawn Care, Coring, Vertimowing, Dethatching</label>
    </p>
    <br />
    <p>
        <label class="label-checkbox">
            <input type="checkbox" name="service" value="Landscaping, New Lawns, Irrigation, Planting, Artificial Turf"
            size="40" />Landscaping, New Lawns, Irrigation, Planting, Artificial Turf</label>
    </p>
    <br />
    <p>
        <label class="label-checkbox">
            <input type="checkbox" name="service" value="Flye Cleaning, Snow Removal, Water Blasting (NZ Only), Trash Packs (TAS only)"
            size="40" />Flye Cleaning, Snow Removal, Water Blasting (NZ Only), Trash Packs (TAS
            only)</label>
    </p>
    <br />
    <br />
    <p>
        <label>Comments *</label>
        <textarea cols="50" rows="5" type="text" id="comment"
        name="comment"></textarea>
    </p>
    <p>
        <button type="submit">Request A Free Quote</button>
        <button type="reset">Cancel</button>
    </p>
</form>

这有效,但并非所有字段都发送给收件人。像复选框字段(服务),名称,国家/州,郊区,街道名称和评论。请帮我弄清楚我的代码中遗漏或错误的内容。感谢。

2 个答案:

答案 0 :(得分:2)

请注意,复选框可以是数组。您应该在复选框字段中使用name="service[]"而不是name="service"。然后,在将输入内插到电子邮件中时,请使用implode(', ', $_POST['service']);,因为它将是一个数组。

print_r($_POST);时会发生什么?

答案 1 :(得分:0)

首先检查

echo "<pre>";
print_r($_POST);
echo "<pre>";

然后检查你必须定义

$MESSAGE_BODY = "";

如果您没有通过错误Undefined variable $MESSAGE_BODY

定义它

最后检查

echo "<pre>";
print_r($MESSAGE_BODY);
echo "<pre>";

修改mail();

的正上方添加此内容
$mailheader  = "MIME-Version: 1.0" . "\r\n";
$mailheader .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";