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



我是设置表单的新手。我设置了一个表单并将其发送给多个人,但现在我需要将其发送给依赖于下拉列表的多个人。我已经四处搜索,无法让它工作。我确信这是由于我缺乏 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: ' . "nn";
// 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、经销商 2 等的地址,然后使用在窗体上选择的select_a_dealer选择的值简单地索引到该数组中。

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

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

最新更新