Skip to content
Advertisement

Can we use multiple message fields in wp_mail() function?

I have a code in php and i want to use that code in my wordpress website. I have created a contact form included some field calculations (multiplication/addition) on the client side. But after the user fills the form, user submit the form for mail processing.

I am using wp_mail() function to receive mail from user. But the problem is that, i have multiple fields and the wp_mail function accepts only

wp_mail($to, $name, $message, $headers, $attachments);

I have following for example:

<?php
if (isset($_POST['submit'])) {
//user posted variables
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];
  $zip = $_POST['zip-code'];
  $f_name = $_POST['-fname'];
  $f_email = $_POST['f-email'];
  $address = $_POST['address'];
  $number = $_POST['number'];
}
?>

I am confused how wp_mail() will send all this information via mail. Is there a way to to send all this data using wp_mail() or any other option?

Full Code.

    <?php
    /**
     * Template Name: Order Form Page
     *
     * @package Angle
     * @subpackage Frontend
     * @since 0.1
     *
     * @copyright (c) 2014 Oxygenna.com
     * @license http://wiki.envato.com/support/legal-terms/licensing-terms/
     * @version 1.15.1
     */
    get_header();
    ?>


<head>

    <script>
    // UTILITY FUNCTIONS 
    function calculate() {
        var myBox1 = document.getElementById('myBoxVal').value;
        var myBox2 = document.getElementById('sparkle-num-pallets').value;
        var result = document.getElementById('result');
        var myResult = myBox1 * myBox2;
        result.value = myResult;


    }
    </script>
    <style>
    table,
    th,
    td {
        border: 1px solid gray;
        text-align: center;
        font-size: 14px;
    }
    </style>


</head>

<?php
    
      //response generation function
    
      $response = "";
    
      //function to generate response
      function my_contact_form_generate_response($type, $message){
    
        global $response;
    
        if($type == "success") $response = "<div class='success'>{$message}</div>";
        else $response = "<div class='error'>{$message}</div>";
    
      }
    
      //response messages
      $not_human       = "Human verification incorrect.";
      $missing_content = "Please provide all information.";
      $email_invalid   = "Invalid Email Address .";
      $message_unsent  = "Message was not sent. Try Again.";
      $message_sent    = "Thanks! Your message has been sent.";
      
    
      //user posted variables
    
      if(isset($_POST['submit'])){
      $name = $_POST['name'];
      $email = $_POST['email'];
      $bill_to = $_POST['Bill-to'];
      $ship_to = $_POST['Ship-to'];
      $date = $_POST['date'];
      $po = $_POST['PO'];
      $phone = $_POST['Phone-number'];

      $Amount_of_FC104 = $_POST['Amount-of-FC104'];
      $pro_FC104 = $_POST['FC104-QTY'];
    
      $Amount_of_H2020 = $_POST['Amount-of-H2020'];
      $pro_H2020 = $_POST['H2020-QTY'];
    
      $Amount_of_ST200 = $_POST['Amount-of-ST200'];
      $pro_ST200 = $_POST['ST200-QTY'];
    
      $Amount_of_UFO_1 = $_POST['Amount-of-UFO-1'];
      $pro_UFO_1 = $_POST['UFO-1-QTY'];
    
      $Amount_of_UFO_2 = $_POST['Amount-of-UFO-2'];
      $pro_UFO_2 = $_POST['UFO-2-QTY'];
    
      $Amount_of_LECB600 = $_POST['Amount-of-LECB600'];
      $pro_LECB600 = $_POST['LECB600-QTY'];
    
      $Amount_of_LEDB700 = $_POST['Amount-of-LEDB700'];
      $pro_LEDB700 = $_POST['LEDB700-QTY'];
    
      $Amount_of_LECB800 = $_POST['Amount-of-LECB800'];
      $pro_LECB800 = $_POST['LECB800-QTY'];
    
      $Amount_of_8089G = $_POST['Amount-of-8089G'];
      $pro_8089G = $_POST['8089G-QTY'];
    
      $Amount_of_LE1911 = $_POST['Amount-of-LE1911'];
      $pro_LE1911 = $_POST['LE1911-QTY'];
    
      $Amount_of_ME8342 = $_POST['Amount-of-ME8342'];
      $pro_ME8342 = $_POST['ME8342-QTY'];
    
      $Amount_of_ME8440 = $_POST['Amount-of-ME8440'];
      $pro_ME8440 = $_POST['ME8440-QTY'];
      
      $Amount_of_NV1455 = $_POST['Amount-of-NV1455'];
      $pro_NV1455 = $_POST['NV1455-QTY'];
    
      $Amount_of_LESM200 = $_POST['Amount-of-LESM200'];
      $pro_LESM200 = $_POST['LESM200-QTY'];
    
      $Amount_of_MAG_DOTZ = $_POST['Amount-of-MAG-DOTZ'];
      $pro_MAG_DOTZ = $_POST['MAG-DOTZ-QTY'];
    
      $Amount_of_MLB_01 = $_POST['Amount-of-MLB-01'];
      $pro_MLB_01 = $_POST['MLB-01-QTY'];
    
      $Amount_of_WW100 = $_POST['Amount-of-WW100'];
      $pro_WW100 = $_POST['WW100-QTY'];
    
      $Amount_of_WW200 = $_POST['Amount-of-WW200'];
      $pro_WW200 = $_POST['WW200-QTY'];
    
      $Amount_of_LEWS810_BLK = $_POST['Amount-of-LEWS810-BLK'];
      $pro_LEWS810_BLK = $_POST['LEWS810-BLK-QTY'];
    
      $Amount_of_73421 = $_POST['Amount-of-73421'];
      $pro_73421 = $_POST['73421-QTY'];
    
      $Amount_of_LPU900 = $_POST['Amount-of-LPU900'];
      $pro_LPU900 = $_POST['LPU900-QTY'];
    
      $Amount_of_LEF10 = $_POST['Amount-of-LEF10'];
      $pro_LEF10 = $_POST['LEF10-QTY'];
    
      $Amount_of_LEF1 = $_POST['Amount-of-LEF1'];
      $pro_LEF1 = $_POST['LEF1-QTY'];
    
      $Amount_of_LEF855 = $_POST['Amount-of-LEF855'];
      $pro_LEF855 = $_POST['LEF855-QTY'];
    
      $message .= "Name: " . $name . "n";
      $message .= "Email: " . $email . "n";
      $message .= "Ship To: " . $ship_to . "n";
      $message .= "Bill To: " . $bill_to . "n";
      $message .= "Phone Number: " . $phone . "n";
      $message .= "PO: " . $po . "n";
      $message .= "Date: " . $date . "n";

      $message .= "Amount-of-FC104: " . $$Amount_of_FC104 . "n";
      $message .= "FC104-QTY: " . $pro_FC104 . "n";
      $message .= "Amount-of-H2020: " . $$Amount_of_H2020 . "n";
      $message .= "H2020-QTY: " . $pro_H2020 . "n";
      $message .= "Amount-of-ST200: " . $$Amount_of_ST200 . "n";
      $message .= "ST200-QTY: " . $pro_ST200 . "n";
      $message .= "Amount-of-UFO-1: " . $$Amount_of_UFO_1 . "n";
      $message .= "UFO-1-QTY: " . $pro_UFO_1 . "n";
      $message .= "Amount-of-UFO-2: " . $$Amount_of_UFO_2 . "n";
      $message .= "UFO-2-QTY: " . $pro_UFO_2 . "n";
      $message .= "Amount-of-LECB600: " . $$Amount_of_LECB600 . "n";
      $message .= "LECB600-QTY: " . $pro_LECB600 . "n";
      $message .= "Amount-of-LEDB700: " . $$Amount_of_LEDB700 . "n";
      $message .= "LEDB700-QTY: " . $pro_LEDB700. "n";
      $message .= "Amount-of-LECB800: " . $$Amount_of_LECB800 . "n";
      $message .= "LECB800-QTY: " . $pro_LECB800 . "n";
      $message .= "Amount-of-8089G: " . $$Amount_of_8089G . "n";
      $message .= "8089G-QTY: " . $pro_8089G . "n";
      $message .= "Amount-of-LE1911: " . $$Amount_of_LE1911 . "n";
      $message .= "LE1911-QTY: " . $pro_LE1911 . "n";
      $message .= "Amount-of-ME8342: " . $$Amount_of_ME8342 . "n";
      $message .= "ME8342-QTY: " . $pro_ME8342 . "n";
      $message .= "Amount-of-ME8440: " . $$Amount_of_ME8440. "n";
      $message .= "ME8440-QTY: " . $pro_ME8440. "n";
      $message .= "Amount-of-NV1455: " . $$Amount_of_NV1455 . "n";
      $message .= "NV1455-QTY: " . $pro_NV1455 . "n";
      $message .= "Amount-of-LESM200: " . $$Amount_of_LESM200 . "n";
      $message .= "LESM200-QTY: " . $pro_LESM200 . "n";
      $message .= "Amount-of-MAG-DOTZ: " . $$Amount_of_MAG_DOTZ . "n";
      $message .= "MAG-DOTZ-QTY: " . $pro_MAG_DOTZ . "n";
      $message .= "Amount-of-MLB-01: " . $$Amount_of_MLB_01 . "n";
      $message .= "MLB-01-QTY: " . $pro_MLB_01 . "n";
      $message .= "Amount-of-WW100: " . $$Amount_of_WW100 . "n";
      $message .= "WW100-QTY: " . $pro_WW100 . "n";
      $message .= "Amount-of-WW200: " . $$Amount_of_WW200 . "n";
      $message .= "WW200-QTY: " . $pro_WW200 . "n";
      $message .= "Amount-of-LEWS810-BLK: " . $$Amount_of_LEWS810_BLK . "n";
      $message .= "LEWS810-BLK-QTY: " . $pro_LEWS810_BLK . "n";
      $message .= "Amount-of-73421: " . $$Amount_of_73421 . "n";
      $message .= "73421-QTY: " . $pro_73421 . "n";
      $message .= "Amount-of-LPU900: " . $$Amount_of_LPU900 . "n";
      $message .= "LPU900-QTY: " . $pro_LPU900 . "n";
      $message .= "Amount-of-LEF10: " . $$Amount_of_LEF10 . "n";
      $message .= "LEF10-QTY: " . $pro_LEF10 . "n";
      
      $message .= "Amount-of-LEF1: " . $$Amount_of_LEF1 . "n";
      $message .= "LEF1-QTY: " . $pro_LEF1 . "n";
      
      $message .= "Amount-of-LEF855: " . $$Amount_of_LEF855 . "n";
      $message .= "LEF855-QTY: " . $pro_LEF855 . "n";
      

    
    
      //php mailer variables
      $to = get_option('admin_email');
      $subject = "Someone sent a order form from ".get_bloginfo('name');
      $headers = 'From: '. $email . "rn" .
        'Reply-To: ' . $email . "rn";
    
    
     
    
          //validate email
          //if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
           // my_contact_form_generate_response("error", $email_invalid);
        // } //email is valid
         //  else //ready to go!
          //  {
              $sent = wp_mail('shahid.wpexpert@gmail.com', $subject, strip_tags($message), $headers);
              if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
              else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
            }
            //else if ($_POST['submitted']) {my_contact_form_generate_response("error", $missing_content);
        // }
        
      
    
       // }
    
    
    ?>


<style type="text/css">
.error {
    padding: 5px 9px;
    border: 1px solid red;
    color: red;
    border-radius: 3px;
}

.success {
    padding: 5px 9px;
    border: 1px solid green;
    color: green;
    border-radius: 3px;
}

form span {
    color: red;
}
</style>
<div class="container">
<div id="respond">

    <?php echo $response; ?></div>
<div class="container">

    <form class="foxycart" action="<?php get_permalink(); ?>" method="post" id="foxycart-order-form">
        
<tr>
<td><input type="hidden" name="submitted" value="1">
<input type="submit" value="Submit Order" /></td>
</tr>

    </table>

 








    </div>

</div>







</form>
</div>



</div>

<?php get_footer(); ?>

Advertisement

Answer

You can build up the message part by doing something like this:

<?php
if (isset($_POST['submit'])) {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $msg = $_POST['message'];
  $zip = $_POST['zip-code'];
  $f_name = $_POST['-fname'];
  $f_email = $_POST['f-email'];
  $address = $_POST['address'];
  $number = $_POST['number'];

  // Do any validation checking here...

  // If everything is ok, build your message with whatever fields you want to include, e.g.
  $message .= "Message: " . $msg . "n";
  $message .= "Name: " . $name . "n";
  $message .= "Address: " . $address . "n";
  $message .= "Zip Code: " . $zip . "n";
  $message .= "Phone: " . $number . "n";

  // set up your headers and anything else you need 

// send the email:
wp_mail($to, $name, $message, $headers, $attachments);
}

Don’t forget to do your validation, anti spam checking etc before you send the email!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement