PHP / HTML联系表单根据下拉列表发送到特定电子邮件

时间:2015-08-25 20:10:23

标签: php jquery html email contact-form

编辑:

我有一个使用HTML / PHP的联系表单。我希望用户能够从下拉菜单中选择一个部门,并将电子邮件发送到该部门的电子邮件地址。

我的问题是:我在PHP的哪个位置根据用户下拉列表选项指定信息发送到的电子邮件地址。我假设它来自代码的这一部分:

HTML

            <label class="custom-select">
                <select name="department">
                    <option>Technical Issues</option>
                    <option>Charities</option>
                    <option>General Inquiries</option>
                    <option>Other</option>
                </select><span><!-- fake select handler --></span>
            </label>
            </div>
            <div class="column-100">

                <textarea name="message" placeholder="Type your message here..."></textarea>

            </div>
            <div class="column-100 center">
                <input type="submit" value="Send" data-error="Fix errors" data-processing="Sending..." data-success="Thank you!">
            </div>
            <footer class="notification-box"></footer>
        </form>

</div>

PHP

define('FROM_EMAIL', '');

// Recipient's e-mail. To this e-mail messages will be sent.
// e.g.: john@example.com
// multiple recipients e.g.: john@example.com, andy@example.com
define('TO_EMAIL', 'info@beetheswarm.com');

/**
 * Function for sending messages. Checks input fields, prepares message and sends it.
 */
function sendMessage() {
    // Variables init

提前致谢。 :)

2 个答案:

答案 0 :(得分:4)

您必须修改HTML以传递值,以便我们知道选择了哪个:

<select name="department">
   <option value="technical">Technical Issues</option>
   <option value="charities">Charities</option>
   <option value="general">General Inquiries</option>
   <option value="other">Other</option>
</select>

在表单处理器中提取值:

$dept = $_POST['department'];

然后切换语句以获取正确的电子邮件:

switch($dept) {
   case 'technical':
       $to = "technical@company.com";
       break;

   //Rest of email cases
}

// Mail it!
$result = mail($to, $title, $message, $headers);

答案 1 :(得分:1)

我在PHP中设置了一些逻辑,将TO_EMAIL变量更改为相应的电子邮件。 (PHP的第6行)

switch ( $_POST['department'] ) {
    case "Technical Issues":
        define('TO_EMAIL', 'technical@beetheswarm.com');
    break;
    case "Charities":
        define('TO_EMAIL', 'charity@beetheswarm.com');
    break;
    case "General Inquiries":
        define('TO_EMAIL', 'general@beetheswarm.com');
    break;
    default:
    case "Other":
        define('TO_EMAIL', 'info@beetheswarm.com');
    break;
}

这不起作用吗?