我们可以在 wp_mail() 函数中使用多个消息字段吗?

Posted

技术标签:

【中文标题】我们可以在 wp_mail() 函数中使用多个消息字段吗?【英文标题】:Can we use multiple message fields in wp_mail() function? 【发布时间】:2020-10-24 09:21:17 【问题描述】:

我在 php 中有一个代码,我想在我的 wordpress 网站中使用该代码。我在客户端创建了一个包含一些字段计算(乘法/加法)的联系表格。但是用户填写表单后,用户提交表单进行邮件处理。

我正在使用wp_mail() 函数来接收来自用户的邮件。但问题是,我有多个字段,而 wp_mail 函数只接受

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

我有以下例子:

<?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'];

?>

我很困惑wp_mail() 将如何通过邮件发送所有这些信息。 有没有办法使用wp_mail() 或任何其他选项发送所有这些数据?

完整代码。

    <?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 . "\r\n" .
        'Reply-To: ' . $email . "\r\n";
    
    
     
    
          //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(); ?>

【问题讨论】:

只需将所有字段添加到$message 的单个字符串中,然后将其传递给wp_mail 感谢您的回复。但我怎么能添加?请写一两个字符串。 这样的? $message = $_POST['message', 'f-name', 'f-email'];对吗? 【参考方案1】:

您可以通过执行以下操作来构建消息部分:

<?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);


在发送电子邮件之前不要忘记进行验证、反垃圾邮件检查等!

【讨论】:

当你说它“不工作”时,你能更具体一点吗?例如。是否给出错误,$message 是否有错误信息?如果没有发送,您是否检查了标题和来自电子邮件地址? 我听从了你的指示,但我没有工作。实际上串联导致问题。如果我消除连接并只使用 3 个字段,那么它就可以工作。 您在连接字段的位置有语法错误,例如: $message .= "Amount-of-FC104: " . $$Amount_of_FC104 . "\n"; 你使用 $$ 代替 $ 嗨,我自己整理的。是的,存在 ($$) 语法错误以及字段使用错误。现在一切正常link感谢您的帮助。 很高兴你得到了排序。我们不能期望知道您的变量是否适用,因此我们只能帮助我们解决您询问的具体错误:) 如果我的回答有帮助,您可以考虑接受/支持它,以便其他有类似问题的用户知道是否可能也帮助他们。接受它也会在网站上将其标记为已解决,我们都会获得代表点:)

以上是关于我们可以在 wp_mail() 函数中使用多个消息字段吗?的主要内容,如果未能解决你的问题,请参考以下文章

在 wordpress 中使用 wp_mail 发送 HTML 和纯文本电子邮件

使用 wp_mail() 将带有表单变量的电子邮件发送到电子邮件

使用 wp_mail() 发送 HTML 电子邮件时,CSS 将不起作用

在 wp_mail 标头中设置回复地址

wp_mail() 将电子邮件主题中的“-”转换为“-”

WP_MAIL() 不发送附件