Php表单根据用户下拉选择发送邮件

时间:2017-09-02 12:15:18

标签: php email

我有一个php联系表单,我想在其上添加一些代码,但不知道如何。所以基本上我有这个在线预约的表格。在我的下拉选择“部门”我有多个选择。如何让php表单通过用户选择将请求发送到接收方电子邮件地址。例如,如果有人选择管理员部门。如果有人选择帐户部门,则应将电子邮件发送到admin @域。电子邮件应该转到帐户@域。这是我的php表单代码:

Edge

然后我在我的表格上有这个:

enter code here
<?php
/*

Variable 
$dzName : Contact Person Name
$dzEmail : Contact Person Email
$dzMessage : Contact Person Message
$dzRes : response holder
$dzOtherField : Form other aditional fields


$dzMailSubject : Mail Subject.
$dzMailMessage : Mail Body
$dzMailHeader : Mail Header
$dzEmailReceiver : Contact receiver email address
$dzEmailFrom : Mail Form title
$dzEmailHeader : Mail headers
*/

$dzEmailTo = "reciever@gmail.com;   // Receiver Email Address                         "reciever@gmail.com" 
$dzEmailFrom   = "Website form";

function pr($value)
{
echo "<pre>";
print_r($value);
echo "</pre>";
}


#### Appoinment Form Script ####
if(!empty($_POST) && $_POST['dzToDo'] == 'Appoinment')
{
$dzName = trim(strip_tags($_POST['dzName']));
$dzEmail = trim(strip_tags($_POST['dzEmail']));
$dzMessage = strip_tags($_POST['dzMessage']);   
$dzRes = "";
if(!filter_var($dzEmail, FILTER_VALIDATE_EMAIL)) 
{
    $dzRes['status'] = 0;
    $dzRes['msg'] = 'Adresa Email gresita.';
    echo json_encode($dzRes);
    exit;
}



$dzMailSubject = 'Formular programare online';
$dzMailMessage  =   "
                    Programare online: <br><br>
                    Name: $dzName<br/>
                    Email: $dzEmail<br/>
                    Message: $dzMessage<br/>
                    ";
$dzOtherField = "";
if(!empty($_POST['dzOther']))
{
    $dzOther = $_POST['dzOther'];
    $message = "";
    foreach($dzOther as $key => $value)
    {
        $fieldName = ucfirst(str_replace('_',' ',$key));
        $fieldValue = ucfirst(str_replace('_',' ',$value));
        $dzOtherField .= $fieldName." : ".$fieldValue."<br>";
    }
}
$dzMailMessage .= $dzOtherField; 

$dzEmailHeader      = "MIME-Version: 1.0\r\n";
$dzEmailHeader      .= "Content-type: text/html; charset=iso-8859-1\r\n";
$dzEmailHeader      .= "From:$dzEmailFrom <$dzEmail>";
$dzEmailHeader      .= "Reply-To: $dzEmail\r\n"."X-Mailer:                           PHP/".phpversion();
if(mail($dzEmailTo, $dzMailSubject, $dzMailMessage, $dzEmailHeader))
{
    $dzRes['status'] = 1;
    $dzRes['msg'] = 'Va multumim pentru mesaj. Va vom raspunde la cerere     in cel mai scurt timp posibil.';
}
else
{
    $dzRes['status'] = 0;
    $dzRes['msg'] = 'Eroare! Va rugam incercati din nou!.';
}
echo json_encode($dzRes);
exit;
}   
#### Appoinment Form Script End ####

1 个答案:

答案 0 :(得分:1)

首先,创建一个像这样的电子邮件ID数组:

$emailList = array('admin' => 'admin@domain', 
                'accounts' => 'accounts@domain'
                'management' => 'management@domain',
                'sdut' => 'sdut@domain'
            );

上述数组中的键应与下拉列表的value属性完全匹配

现在,当用户提交表单时,使用$_POST['dzOther']['Departament']值来获取用户的响应并将其用作从$emailList数组中获取相应电子邮件ID的密钥,并相应地发送电子邮件

$dzEmailTo = $emailList[$_POST['dzOther']['Departament']];