如何使用表格下拉菜单控制电子邮件收件人?

时间:2019-03-08 15:09:10

标签: php html forms

我不熟悉设置表单。我已经建立了一个表单并将其发送给多个人,但是现在我需要将其发送给依赖于下拉列表的多个人。我到处搜寻,无法正常运作。我敢肯定这是由于我缺乏php知识。我希望该表格发送给他们选择的每个经销商一些电子邮件。因此,如果他们填写表格,然后选择“经销商二”,它将转到一组电子邮件;如果他们选择“经销商二”,它将转到另一组电子邮件。有人可以帮我吗?

    <?php

// Set email variables


$email_to = 'ryan@example.com, ryan@example2.com, mike@example.com';


$email_subject = 'Request More Information';

// Set required fields
$required_fields = array('name','email');

// set error messages
$error_messages = array(
    'name' => 'Please enter a Name to proceed.',
    'email' => 'Please enter a valid Email Address to continue.',
);
// Where to redirect after form is processed. 
$url = 'http://www.example.com/confirmation.html'; 

// Set form status
$form_complete = FALSE;

// configure validation array
$validation = array();


//.....................................................................
//dropdown email list code:



//.....................................................................



// check form submittal
if(!empty($_POST)) {
    // Sanitise POST array
    foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));

    // Loop into required fields and make sure they match our needs
    foreach($required_fields as $field) {       
        // the field has been submitted?
        if(!array_key_exists($field, $_POST)) array_push($validation, $field);

        // check there is information in the field?
        if($_POST[$field] == '') array_push($validation, $field);

        // validate the email address supplied
        if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
    }

    // basic validation result
    if(count($validation) == 0) {
        // Prepare our content string
        $email_content = 'New Website Comment: ' . "\n\n";

        // simple email content
        foreach($_POST as $key => $value) {
            if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
        }

        // if validation passed ok then send the email

        $email = explode(',', $email_to);

        foreach($email as $e) {
            $e = trim($e);
            mail($e, $email_subject, $email_content);
        }        

        // Update form switch
        $form_complete = TRUE;
    }
}

function validate_email_address($email = FALSE) {
    return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}

function remove_email_injection($field = FALSE) {
   return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>




<label for="name">Name:</label>
        <input type="name" id="name" name="name" size="25" maxlength="60" required autofocus placeholder="Name">
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" size="25" maxlength="60" required placeholder="Email">
        <label for="address">Address:</label>
        <input type="address" id="address" name="address" size="25" maxlength="120" placeholder="Address">
        <label for="city">City:</label>
        <input type="city" id="city" name="city" size="25" maxlength="20" placeholder="City">
        <label for="tel">Telephone:</label>
        <input type="tel" id="tel" name="tel" size="25" maxlength="18" placeholder="Telephone"> 

<!--surround the select box with a "custom-select" DIV element. Remember to set the width:-->
        <div class="custom-select" style="width:80%;">
        <label for="select">Select A Dealer</label>
        <select name="select_a_dealer" required="required" id="select_a_dealer" form="request_a_quote" >
            <option value="0">Select A Dealer</option>
            <option value="1">Dealer One - City One</option>
            <option value="2">Dealer Two - City Two</option>
            <option value="3">Dealer Three - City Three</option>

</div>

        <input type="submit" class="btn btn-danger" style="border-radius; .3rem; font-size:14px; padding:.5rem; margin:10px; width:30%; display:block">
        <input type="reset" class="btn btn-danger" style="border-radius; .3rem; font-size:14px; padding:.5rem; margin:10px; width:30%; display:block">





    </form>

1 个答案:

答案 0 :(得分:0)

在代码中构造一个数组,该数组包含要用于经销商1,经销商2等的地址,然后使用表格上select_a_dealer选择的值简单地索引到该数组中。

$dealersto = [
              1=>['joe@example.com','sam@example.com'],
              2=>['bob@example.com','sue@example.com']
             ];

在发送邮件的代码中,使用select_a_dealer的发布值作为数组中的键,以设置电子邮件的to地址。