特定电子邮件以在表单中选择的单选按钮为条件



感谢您提供这里的所有信息,但我似乎缺少一些东西来使其发送到每个特定的电子邮件地址。

这段代码的基础来自线程:根据单选按钮将PHP表单发送到不同的电子邮件

<form name="contact-form" method="post" action="contact.php">
  <div class="form_details">
    <input type="text" id="field_name" name="sender_name" class="text" value="name">
    <input type="text" id="field_email" name="sender_email" class="text" value="email" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email';}">                                     
    <input type="text" id="field_subject" name="sender_subject" class="text" value="subject" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Subject';}">                                     
    <textarea id="field_message" name="sender_message" value="Message" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Message';}"></textarea>
    <div class="clearfix"> </div>
    <div class="sub-button">
      <div style="float:left">
        <input type="radio" name="department" value="archery"> Archery
        <input type="radio" name="department" value="firearms"> Firearms
        <input type="radio" name="department" value="general"> General Inquiry
      </div>
    <div class="g-recaptcha" data-sitekey="6LeydxcTAAAAACBd4beoLDgtu0LIqzTWOJnvfsH-"></div>
      <input type="submit" name="send_message" value="Submit">
    </div>
  </div>
</form>
<?php
  // Assigning data from the $_POST array to variables     
  $name = $_POST['sender_name'];     
  $mail_from = $_POST['sender_email'];     
  $phone = $_POST['sender_phone'];
  $sender_subject = $_POST['sender_subject'];          
  $message = $_POST['sender_message'];     
  $radio = isset($_POST['archery']) ? $_POST['firearms'] : $_POST['general'];
  if (isset($_POST['archery'])) {
    $department = 'info1@info.com';
  } elseif (isset($_POST['firearms'])) {
    $department = 'info2@info.com';
  } elseif (isset($_POST['general'])) {
    $department = 'info3@info.com';
  } else {
    $department = 'info4@info.com';
  }
  // Construct email subject     
  $subject = 'SC Website Message ';     
  // Construct email body     
  $body_message .= 'From: ' . $name . "rn";     
  $body_message .= 'E-mail: ' . $mail_from . "rn";
  $body_message .= 'Subject: ' . $sender_subject . "rn";     
  $body_message .= 'Message: ' . $message;     
  // Construct email headers     
  $headers = 'From: ' . $mail_from . "rn";     
  $headers .= 'Reply-To: ' . $mail_from . "rn";     
  $mail_sent = mail($mail_to, $subject, $body_message, $headers);     
  if ($mail_sent == true){ ?>         
    <script language="javascript" type="text/javascript">         
      alert('Thank you for your message, we will be in contact shortly.');         
      window.location = 'contact.html';         
    </script>     
  <?php } else { ?>     
    <script language="javascript" type="text/javascript">         
      alert('Message not sent. Please, notify the site administrator     info1@info.com');         
      window.location = 'contact.html';     
    </script>     
  <?php     
  } 
?>
<form name="contact-form" method="post" action="demo.php">
<div class="form_details">
    <input type="text" id="field_name" name="sender_name" class="text" value="name">
    <input type="text" id="field_email" name="sender_email" class="text" value="email" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email';}">                                     
    <input type="text" id="field_subject" name="sender_subject" class="text" value="subject" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Subject';}">                                     
    <textarea id="field_message" name="sender_message" value="Message" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Message';}"></textarea>
    <div class="clearfix"> </div>
    <div class="sub-button">
    <div style="float:left">
<input type="radio" name="department" value="archery"> Archery
<input type="radio" name="department" value="firearms"> Firearms
<input type="radio" name="department" value="general"> General Inquiry
</div>
<div class="g-recaptcha" data-sitekey="6LeydxcTAAAAACBd4beoLDgtu0LIqzTWOJnvfsH-"></div>
<input type="submit" name="send_message" value="Submit">
</div>
</div>
</form>
<?php
// Assigning data from the $_POST array to variables     
if(isset($_POST['sender_name']) && isset($_POST['sender_email'] ) && isset($_POST['sender_phone']) && isset($_POST['sender_subject']) && isset($_POST['sender_message'])){
$name = $_POST['sender_name'];     
$mail_from = $_POST['sender_email'];     
$phone = $_POST['sender_phone'];
$sender_subject = $_POST['sender_subject'];          
$message = $_POST['sender_message'];    
/**********************Change Radio button name *******************/
$radio=null;
if(isset($_POST['department'])){
    $radio = $_POST['department'];
} 
if ($radio=='archery') {
  $department = 'info1@info.com';
} elseif ($radio=='firearms') {
  $department = 'info2@info.com';
} elseif ($radio=='general') {
  $department = 'info3@info.com';
} else {
  $department = 'info4@info.com';
}

// Construct email subject     
$subject = 'SC Website Message ';     
// Construct email body     
$body_message .= 'From: ' . $name . "rn";     
$body_message .= 'E-mail: ' . $mail_from . "rn";
$body_message .= 'Subject: ' . $sender_subject . "rn";     
$body_message .= 'Message: ' . $message;     
// Construct email headers     
$headers = 'From: ' . $mail_from . "rn";     
$headers .= 'Reply-To: ' . $mail_from . "rn";     
// As flow mail sent to department
$mail_to=$department;
$mail_sent = mail($mail_to, $subject, $body_message, $headers);     
if ($mail_sent == true){ ?>         
  <script language="javascript" type="text/javascript">         
    alert('Thank you for your message, we will be in contact shortly.');         
    window.location = 'contact.html';         
  </script>     
<?php } else { ?>     
  <script language="javascript" type="text/javascript">         
    alert('Message not sent. Please, notify the site administrator     info1@info.com');         
    window.location = 'contact.html';     
  </script>     
<?php     
}
} 
?>

编辑:

.HTML

<form name="contact-form" method="POST" action="contact.php">
<div class="form_details">
<input type="text" id="field_name" name="sender_name" class="text" placeholder="name"/>
<input type="text" id="field_email" name="sender_email" class="text" value="email" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email';}"/>                                     
<input type="text" id="field_subject" name="sender_subject" class="text" value="subject" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Subject';}"/>                                     
<textarea id="field_message" name="sender_message" placeholder="Message" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Message';}"></textarea>
<div class="clearfix"> </div>
<div class="sub-button">
<div style="float:left">
<input type="radio" name="department" value="archery"/> Archery
<input type="radio" name="department" value="firearms"/> Firearms
<input type="radio" name="department" value="general"/> General Inquiry
</div>
<div class="g-recaptcha" data-sitekey="6LeydxcTAAAAACBd4beoLDgtu0LIqzTWOJnvfsH-"></div>
<input type="submit" name="send_message" value="Submit"/>
</div>
</div>
</form>

.PHP

    <?php
    if (isset($_POST["send_message"])) {
    $name = $_POST["sender_name"];
    $mail_from = $_POST["sender_email"];
    //$phone = $_POST["sender_phone"];
    $sender_subject = $_POST["sender_subject"];
    $message = $_POST["sender_message"];
    $radio = $_POST["department"];
    if ($radio == "archery") {
        $department = "info1@info.com";
    } elseif ($radio == "firearms") {
        $department = "info2@info.com";
    } elseif ($radio == "general") {
        $department = "info3@info.com";
    } else {
        $department = "info4@info.com";
    }
    // Construct email subject     
    $subject = 'SC Website Message ';     
    // Construct email body   
    $body_message = "";  
    $body_message .= 'From: ' . $name . "rn";     
    $body_message .= 'E-mail: ' . $mail_from . "rn";
    $body_message .= 'Subject: ' . $sender_subject . "rn";     
    $body_message .= 'Message: ' . $message;     
    // Construct email headers     
    $headers = 'From: ' . $mail_from . "rn";     
    $headers .= 'Reply-To: ' . $mail_from . "rn";     
    // As flow mail sent to department
    $mail_to=$department;
    $mail_sent = mail($mail_to, $subject, $body_message, $headers);
    if ($mail_sent == true){ ?>         
    <script language="javascript" type="text/javascript">         
    alert('Thank you for your message, we will be in contact shortly.');         
    window.location = 't1.php';         
    </script>     
    <?php } else { ?>     
    <script language="javascript" type="text/javascript">         
    alert('Message not sent. Please, notify the site administrator     info1@info.com');         
    window.location = 't1.php';     
    </script>     
   <?php     
    }
    }
    ?>

最新更新