PHP表单根据下拉选项转到不同的电子邮件地址[关闭]

so I basically have zero experience using PHP, and I need to create a PHP form, that depending on the users' selection, will go to different e-mail addresses.

For example the drop down would have options like: Service, Parts, Human Resources, etc.

And each of those options, when selected, will allow the entire submission of "name, email, phone number, etc." to go to that particular department's e-mail address.

I saw a few answers for things similar to this, but with my limited knowledge of PHP, I am having a lot of trouble creating this.

Could anyone help me out please??

Thanks!

you could get the value of the email from your select field and use it when sending the mail like this:

$mail['to'] = $_POST['mail']; 
//make sure to filter this using regex/etc.. 
//to make sure this is a valid email and is coming from your own form.

mail($mail['to'], $mail['subject'], $mail['body'], $mail['headers'])

i'll leave the filtering of the data to you, but you should get the idea.

EDIT:

make sure that you filter $_POST['mail']; because if you did not, this would result from other people submitting emails to your script which is a huge security vulnerability. possible solutions are using nonce or 1 time use tokens etc. try searching up for mail header injection for more info.

Use the mail() function and choose an appropriate email address based on posted params. Note that allowing unknown users to directly send mails through your server is probably not a great idea: what if there is a security hole in how mail() is implemented? Why don't you put the relative email addresses on the website and show only the right one depending on what entry user selected from the dropdown menu?

You should do something like that:

<?php
// Check if form has been submitted
if (isset($_POST["dep"])) {
if ($_POST["dep"] == 1) {
$email = "sales@example.com";
}
elseif ($_POST["dep"] == 2) {
$email = "support@example.com";
}
// Send the email
mail($email, "The subject", "The Body, "Some headers (optional)")
}
?>
<select name="dep">
<optoin value="1">Sales</option>
<optoin value="2">Support</option>
</select>