根据输入选择值发送电子邮件

时间:2014-03-20 14:16:48

标签: php arrays forms

我需要通过添加下拉菜单来更改现有表单,其中我将有两个输入及其值。目的是在选择地址1 时将表单发送到 recipient_one ,或者在选择地址2 时将表单发送到 recipient_two 。当没有选择任何内容时,地址1必须是默认值。

以下是添加的HTML:

<form method="post" action="./index.php" enctype="multipart/form-data">
  <fieldset class="elist">  

        <legend>Select shop:</legend>

                 <select name="shop">

                            <option name="address-chosen" value="Tammsaare" >Tammsaare</option>
                            <option name="address-chosen" value="Ülemiste"  >Ülemiste</option>

                 </select>

  </fieldset>
</form>

和PHP:

    $action = isset($_POST['action']) ? $_POST['action'] : null;

$page = null;
$pages = array('info', 'en');
if( isset($_GET['page']) && in_array($_GET['page'], $pages) ) {
    $page = $_GET['page'];
}

if( !in_array($page, $pages) ) {
    $page = '';
}

$mail_sent = false;
if( $action == 'add' ) {

    //Test if it is a shared client
    if (!empty($_SERVER['HTTP_CLIENT_IP'])){
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    //Is it a proxy address
    }elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
        $ip = $_SERVER['REMOTE_ADDR'];
    }

        $message = '';
        $message .= 'Name: '.safe($_POST['name'])."\r\n";
        $message .= 'E-mail: '.safe($_POST['email'])."\r\n";
        $message .= 'Phone: '.safe($_POST['telephone'])."\r\n";
        $message .= 'Mark: '.safe($_POST['mark'])."\r\n";
        $message .= 'Model: '.safe($_POST['model'])."\r\n";
        $message .= 'Shop: '.safe($_POST['address-chosen'])."\r\n";
        $message .= "Wants newsletter: ".$soovib_uudiskirja = isset($_POST['newsletter']) ? "Yes" : "No";
        $message .= "\r\n";
        $message .= "\r\n";
        $message .= "\r\n";
        $message .= 'Aeg: '.date('d.m.Y H:i')."\r\n";
        $message .= 'IP: '.$ip."\r\n";

    $mail_data = array(
        'to_email' => 'email@mail.com',
        'from_email' => 'email@mail.com',
        'from_name' => 'Stock Cars',
        'subject' => 'Reservation',
        'message' => $message,

    );
    mail_send($mail_data);


    $mail_sent = true;
}


function safe( $name ) {
   return( str_ireplace(array( "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:" ), "", $name ) );
}


function mail_send($arr)
{
    if (!isset($arr['to_email'], $arr['from_email'], $arr['subject'], $arr['message'])) {
        throw new HelperException('mail(); not all parameters provided.');
    }

    $to            = empty($arr['to_name']) ? $arr['to_email'] : '"' . mb_encode_mimeheader($arr['to_name']) . '" <' . $arr['to_email'] . '>';
    $from        = empty($arr['from_name']) ? $arr['from_email'] : '"' . mb_encode_mimeheader($arr['from_name']) . '" <' . $arr['from_email'] . '>';

    $headers    = array
    (
        'MIME-Version: 1.0',
        'Content-Type: text/plain; charset="UTF-8";',
        'Content-Transfer-Encoding: 7bit',
        'Date: ' . date('r', $_SERVER['REQUEST_TIME']),
        'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['SERVER_NAME'] . '>',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
        'X-Mailer: PHP v' . phpversion(),
        'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'],
    );

    mail($to, '=?UTF-8?B?' . base64_encode($arr['subject']) . '?=', $arr['message'], implode("\n", $headers));}

    if (isset($_GET['page'])) {} 

所以问题是如何重建阵列?

1 个答案:

答案 0 :(得分:1)

根据表单值设置电子邮件变量:

//default is email 1
$email='email@mail.com';
if(isset($_POST['shop']) && $_POST['shop']=='Ülemiste') {$email='email2@mail2.com';}
$mail_data = array(
        'to_email' => $email,
        'from_email' => $email,
        'from_name' => 'Stock cars',
        'subject' => 'Reservation',
        'message' => $message,

    );

根据您的编辑进行编辑 - 不确定变音符号将如何影响