简体   繁体   中英

PHP—Send Mail Question

I'm writing a sendmail script in PHP at the moment. It's largely based around what a user selects in a menu dropdown.The menu looks like this:

<select name="program">
    <option value="Red Program">Red Program</option>
    <option value="Green Program">Green Program</option>
    <option value="Yellow Program">Yellow Program</option>
    <option value="Blue Program">Blue Program</option>
    <option value="Orange Program">Orange Program</option>
    ...

I'll need to evaluate what option has been selected from this menu and send the title of the program to someone in an email. However, half of the programs will need to be sent to one address, the other half to a different address. What's the best way to both return the name of the program and evaluate what has been selected to see what address it should be sent to?

Is there an easier way than writing an if statement for every possible value? Thanks!

Simple array with emails

<?php
$programs = array(
  "Red Program" => "test_1@example.com",
  "Green Program" => "test_2@example.com",
);
if(isset($programs[$_POST['program']]))
    mail($programs[$_POST['program']], $_POST['program']);

But it's better to use ids in form values instead names

I think you may create two arrays with the programs' names and then check in which array the user's selection has been found.

// $selection is the user's selection

$FirstEmailAdress=array("Red Program", "Green Program", "Yellow Program");
$SecondEmailAddress=array("Blue Program", "Orange Program");

function CheckEmail ($selection) {
    foreach ($FirstEmailAddress as $value) {
        if ($value==$selection) return "first@email.address";
        // if the selection is found in the first array,
        // the function stops and returns the first address
    }
    return "second@email.address";
    // otherwise, it returns the second one
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM