【问题标题】:PHP/HTML Contact Form send to specific email based on dropdownPHP/HTML 联系表格根据下拉列表发送到特定的电子邮件
【发布时间】:2015-11-19 16:46:15
【问题描述】:

已编辑:

我有一个使用 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

提前致谢。 :)

【问题讨论】:

  • 那么你的问题是什么?在你说“我该怎么做?”之前,请记住,这样的问题太宽泛了,因此会被封闭。因此,如果到目前为止您已经编写了任何代码,我强烈建议您将其添加到您的问题中。您应该阅读“how do I ask a good question?”。

标签: php jquery html email contact-form


【解决方案1】:

您将不得不修复您的 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 语句来获取正确的电子邮件:

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

   //Rest of email cases
}

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

【讨论】:

  • 谢谢!那工作得很好。我之前没有使用过 switch 语句,所以谢谢你解释一切。
【解决方案2】:

我会在 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;
}

这样不行吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多