代码之家  ›  专栏  ›  技术社区  ›  Vecta

PHPSend邮件问题

  •  1
  • Vecta  · 技术社区  · 14 年前

    我正在用PHP编写sendmail脚本。它主要基于用户在菜单中选择的内容下拉列表菜单如下所示:

    <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>
        ...
    

    我需要评估什么选项已经从这个菜单中选择,并发送程序的标题在电子邮件中的人。但是,一半的程序需要发送到一个地址,另一半发送到另一个地址。返回程序名称和评估所选内容以查看它应发送到的地址的最佳方法是什么?

    有没有比为每个可能的值编写if语句更简单的方法? 谢谢!

    2 回复  |  直到 14 年前
        1
  •  2
  •   botmonster    14 年前

    带有电子邮件的简单数组

    <?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']);
    

        2
  •  1
  •   rhino    14 年前

    我想你可以用程序名创建两个数组,然后检查用户选择的数组。

    // $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
    }