-1

我的发送联系人 php 文件有问题。我希望根据访客选择的主题发送电子邮件,例如:Padicure Booking 主题电子邮件将发送至 booking@example.com,而一般查询主题将发送至 info@example.com

下面是我的php代码。

<?php
function emailswitch( $key ) {
    $to = array(
        'Pedicure Price Inquiry' => 'enquiry@gmail.com',
        'Manicure Price Inquiry' => 'enquiry@gmail.com',
        'Booking Time' => 'info@gmail.com'
    );
    $default = 'info@gmail.com';
    return isset( $_POST['field_?'] ) && !empty($to[ $_POST['field_?'] ])  ? $to[ $_POST['field_?'] ]: $default;  $from = $_POST['email'];
}
$from_name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['category']; 

// collect data
$body = "";
foreach($_POST as $key => $val)
{
    if($key != 'captcha')
        $body .= ucfirst($key).": ".$val."\r\n";
}

// construct MIME PLAIN Email headers
$header = "MIME-Version: 1.0\n";
$header .= "Content-type: text/plain; charset=utf-8\n";
$header .= "From: $from_name <$email> \r\nReply-To: $from_name <$email> \r\nReturn-Path: <$email> \r\n";

// send email
$to = emailswitch( $subject );
$mail_sent = mail($to, $subject, $body, $header);
?>

以下是我的表单代码:

<div id="contact_form">
                    <form action="sendContact.php" method="post" onsubmit="return sendContact();">
                        <p><label id="lname" for="name">Full name:</label>
                        <input id="name" class="text" name="name" onblur="input_blur('name');" onfocus="input_focus('name');" type="text" /></p>
                        <p><label id="lemail" for="email">Email address:</label>
                        <input id="email" class="text" name="email" onblur="input_blur('email');" onfocus="input_focus('email');" type="text" /></p>
                        <div class="x"></div>
                        <p id="email-error" class="error">You must enter your email address!</p>
                        <p><label id="lcategory" for="category">Category:</label>
                        <select id="category" name="category" onblur="input_blur('category');" onfocus="input_focus('category');">
                        <option value="Pedicure Price Inquiry">Pedicure Price Inquiry</option>
                        <option value="Manicure Price Inquiry">Manicure Price Inquiry</option>
                        <option value="Booking Time">Booking Time</option></select></p>
                        <p><label id="lmessage" for="message">Message:</label>
                        <textarea id="message" name="message" onblur="input_blur('message');" onfocus="input_focus('message');" rows="" cols=""></textarea></p>
                        <div class="x"></div>
                        <p id="message-error" class="error">You must enter your message!</p>
                        <p><label id="lcaptcha" for="captcha"></label>
                        <input id="captcha" class="text" name="captcha" onblur="input_blur('captcha');" onfocus="input_focus('captcha');" type="text" /></p>
                        <div class="x"></div>
                        <p id="captcha-error" class="error">Are you sure about your calculations?</p>
                        <script type="text/javascript">
                            generate_captcha('lcaptcha');
                        </script>
                        <div class="x"></div>
                        <input class="submit" name="send_contact" type="submit" value="Send" /><input class="submit" type="reset" value="Reset" />
                    </form>
                </div>
                <div id="message_sent" style="display: none;">
                    <h1>Your message has been sent</h1>
                    <p>We&#39;ll contact you as soon as possible.</p>
                    <p>You can now <a href="./">go back</a> to home page.</p>
                </div>
                <!-- end of contact form --></div>

但是电子邮件没有发送出去......我想知道我的php文件中有什么问题导致表单无法触发所述电子邮件......

4

2 回答 2

0

$to没有在外部上下文中定义。

$mail_sent = mail(emailswitch(), $subject, $body, $header);
于 2013-03-21T14:35:01.280 回答
0

首先,将参数传递给您的函数,而不是尝试$_POST直接使用:

function emailswitch( $key ) {
    $to = array(
        'Pedicure Price Inquiry' => 'info@example.com',
        'Manicure Price Inquiry' => 'info@example.com',
        'Booking Time' => 'booking@example.com'
    );
    $default = 'booking@example.com';
    return (!empty($to[$key]))?$to[$key]:$default;
}

顺便说一句:里面的那个$from元素emailswitch永远不会被调用,也不应该在函数内部。

其次,确保从电子邮件调用中调用该函数:

$to = emailswitch( $subject );
$mail_sent = mail($to, $subject, $body, $header);  
于 2013-03-21T15:22:28.820 回答