2017-11-12 27 views
1

新しい投稿と電子メールを作成する簡単なフロントエンドのWordPressフォーム(複数の添付ファイルあり)を作成しました。添付ファイルが配信されないことを除いて、すべてが期待どおりに動作しています。私のwp_mail添付ファイルが電子メールで送信されていません

あなたは以下のコードを見てください。$ attachmentsにファイルのパスを配列として挿入しようとしていますが、何も送信されません。私は何が間違っているのか分かりません。

//SEND THE EMAIL 
    function set_html_content_type() { 
     return 'text/html'; 
    } 
    $inv_query_email = '[email protected]'; 
    $admin_email = '[email protected]'; 
    $to = array(
     $inv_query_email, 
     //$admin_email 
    ); 
    $page_slug = get_permalink($pid); 
    $subject = 'BSP: Invoice Query | '.$refno.''; 
    ob_start(); 
    include plugin_dir_path(__FILE__).'/email_templates/bsp_inv_query_template.php'; 
    $bsp_inv_query_message = ob_get_contents(); 
    ob_end_clean(); 
    $headers[] = 'From: '.$username.' <'.$useremail.'>'; 
    //GET ATTACHMENTS 
    if (! function_exists('wp_handle_upload')) { 
     require_once(ABSPATH . 'wp-admin/includes/file.php'); 
    } 
    $attachments = array(); 
    $files = $_FILES['bsp_inv_query_upload']; 
    foreach ($files['name'] as $key => $value) { 
     if ($files['name'][$key]) { 
      $file = array(
       'name'  => $files['name'][$key], 
       'type'  => $files['type'][$key], 
       'tmp_name' => $files['tmp_name'][$key], 
       'error' => $files['error'][$key], 
       'size'  => $files['size'][$key] 
      ); 
      //UPLOAD AND ATTACH TO POST - WORKS AS EXPECTED 
      $_FILES = array("bsp_inv_query_upload" => $file); 
      foreach ($_FILES as $file => $array) { 
       $newupload = insert_attachment($file,$pid); 
      } 
      //CREATE EMAIL ATTACHMENT 
      $emailfile = wp_handle_upload($file); 
      //$attachments[] = $emailfile['file']; //ALSO TRIED THIS 
      array_push($attachments, $emailfile['file']); 
      } 
     } 
    add_filter('wp_mail_content_type', 'set_html_content_type'); 
    wp_mail($to, $subject, $bsp_inv_query_message, $headers, $attachments); 
    remove_filter('wp_mail_content_type', 'set_html_content_type'); 

HTMLフォーム::私はあなたが次のPHPコードのような添付ファイルを作成してください示唆この場合

<!-- BSP INVOICE QUERY FORM --> 
     <form id="bsp_inv_query" class="bsp_inv_query" name="bsp_inv_query" method="post" action="<?php the_permalink(); ?>" enctype="multipart/form-data"> 
      <ul class="bsp_form"> 
       <li><div class="required_txt">* Required</div><input placeholder="Name" type="text" class="log_title active" id="bsp_inv_query_name" name="bsp_inv_query_name" /></li> 
       <li><div class="required_txt">* Required</div><input placeholder="Branch" type="text" class="log_title active" id="bsp_inv_query_branch" name="bsp_inv_query_branch" /></li> 
       <li><div class="required_txt">* Required</div><input placeholder="Telephone" type="text" class="log_title active" id="bsp_inv_query_tel" name="bsp_inv_query_tel" /></li> 
       <li><div class="required_txt">* Required</div><input placeholder="Email" type="text" class="log_title active" id="bsp_inv_query_email" name="bsp_inv_query_email" /></li> 
       <li><div class="required_txt">* Required</div><input placeholder="PAX Name/Surname" type="text" class="log_title active" id="bsp_inv_query_pax" name="bsp_inv_query_pax" /></li> 
       <li><div class="required_txt">* Required</div><input placeholder="Ticket Number" type="text" class="log_title active" id="bsp_inv_query_ticket" name="bsp_inv_query_ticket" /></li> 
       <li><div class="required_txt">* Required</div><input placeholder="Airline Code" type="text" class="log_title active" id="bsp_inv_query_airline" name="bsp_inv_query_airline" /></li> 
       <li><div class="required_txt">* Required</div><input placeholder="Invoice Number" type="text" class="log_title active" id="bsp_inv_query_inv_no" name="bsp_inv_query_inv_no" /></li> 
      </ul> 
      <div class="required_txt">* Required</div> 
      <textarea placeholder="Description of query..." class="log_message" id="bsp_inv_query_desc" name="bsp_inv_query_desc" /></textarea> 
      <div class="log_upload_holder"> 
       <input type="file" data-multiple-caption="{count} files selected" multiple name="bsp_inv_query_upload[]" id="bsp_inv_query_upload" class="upload_attachment"><label for="bsp_inv_query_upload" id=""><svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" 
width="200px" height="200px" viewBox="0 0 200 200" style="enable-background:new 0 0 200 200;" xml:space="preserve"><path class="st0" d="M100.1,2.5c2.9,0,5.7,1.1,7.9,3.3l44.8,44.8c2.1,2.1,3.3,4.9,3.3,7.9s-1.2,5.8-3.3,7.9c-2.1,2.1-4.9,3.3-7.9,3.3s-5.8-1.2-7.9-3.3l-25.8-25.8v104.7c0,6.2-5,11.2-11.2,11.2c-6.1,0-11.1-5-11.1-11.2V40.5L63,66.3c-1.9,2-4.7,3.2-7.7,3.3c-3,0-5.9-1.2-8-3.3c-2.1-2.1-3.3-4.9-3.3-7.9s1.2-5.8,3.3-7.9L92,5.7C94.4,3.6,97.2,2.5,100.1,2.5zM54.5,57.8L54.5,57.8C54.5,57.7,54.5,57.7,54.5,57.8z"/><path class="st0" d="M188.9,128.6c-6.2,0-11.2,5-11.2,11.1v38.1h-48.4H70.7H22.2v-38.1c0-6.1-5-11.1-11.2-11.1c-6.1,0-11.1,5-11.1,11.1v49.2c0,6.3,4.9,11.2,11.1,11.2h60.3h57.3h60.3c6.2,0,11.1-4.9,11.1-11.2v-49.2C200,133.5,195,128.6,188.9,128.6z"/></svg><br><span>Upload a file&hellip;</span></label> 
       <div class="remove_files"></div> 
      </div> 
      <div class="clearfix"></div> 
      <!-- HIDDEN INPUTS SET BY SELECTS --> 
      <input type="hidden" name="bsp_inv_query_department" id="bsp_inv_query_department" value="26" /> 
      <input type="hidden" name="bsp_inv_query_category" id="bsp_inv_query_category" value="27" /> 
      <input type="hidden" name="bsp_inv_query_topic" id="bsp_inv_query_topic" value="43" /> 
      <!-- NONCE --> 
      <input type="hidden" name="bsp_inv_query" id="bsp_inv_query" value="bsp_inv_query" /> 
      <?php wp_nonce_field('bsp_inv_query'); ?> 
      <input type="submit" class="button-primary" id="submit" name="submit" tabindex="6" value="Submit Query" /> 
     </form> 
+0

は、あなたのHTMLフォームは、私が質問にそれを追加した –

+0

@MajidAbbasiを追加します。ありがとう。 – DakotaDC3

答えて

0

この

は、電子メールを起動し、ファイルのアップロードを処理スニペットです
function reArrayFiles(&$file_post) { 
    $file_ary = array(); 
    $file_count = count($file_post['name']); 
    $file_keys = array_keys($file_post); 

    for ($i=0; $i<$file_count; $i++) { 
     foreach ($file_keys as $key) { 
      $file_ary[$i][$key] = $file_post[$key][$i]; 
     } 
    } 

    return $file_ary; 
} 

あなたのワードプレスのアップロードフォルダにファイルをアップロードした後、 O添付ファイル:

$file_ary = reArrayFiles($_FILES['ufile']); 
$attachments = array(); 
foreach ($file_ary as $file) { 
    $path = WP_CONTENT_DIR . '/uploads/'.$file['name']; 
    move_uploaded_file($file['tmp_name'], $path); 
    $attachments[] = $path; 
} 

私は次のコードでテストされ、すべてが正常に動作:

function reArrayFiles(&$file_post) { 
    $file_ary = array(); 
    $file_count = count($file_post['name']); 
    $file_keys = array_keys($file_post); 

    for ($i=0; $i<$file_count; $i++) { 
     foreach ($file_keys as $key) { 
      $file_ary[$i][$key] = $file_post[$key][$i]; 
     } 
    } 

    return $file_ary; 
} 
if(isset($_FILES['bsp_inv_query_upload'])) 
{ 
    $file_ary = reArrayFiles($_FILES['bsp_inv_query_upload']); 
    $attachments = array(); 
    foreach ($file_ary as $file) { 
     $path = WP_CONTENT_DIR . '/uploads/'.$file['name']; 

     move_uploaded_file($file['tmp_name'], $path); 
     $attachments[] = $path; 
    } 
} 

?> 
<form id="bsp_inv_query" class="bsp_inv_query" name="bsp_inv_query" method="post" action="<?php the_permalink(); ?>" enctype="multipart/form-data"> 
    <input type="file" data-multiple-caption="{count} files selected" 
multiple name="bsp_inv_query_upload[]" id="bsp_inv_query_upload" class="upload_attachment"><label for="bsp_inv_query_upload" id=""><svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" 
width="200px" height="200px" viewBox="0 0 200 200" style="enable-background:new 0 0 200 200;" xml:space="preserve"><path class="st0" d="M100.1,2.5c2.9,0,5.7,1.1,7.9,3.3l44.8,44.8c2.1,2.1,3.3,4.9,3.3,7.9s-1.2,5.8-3.3,7.9c-2.1,2.1-4.9,3.3-7.9,3.3s-5.8-1.2-7.9-3.3l-25.8-25.8v104.7c0,6.2-5,11.2-11.2,11.2c-6.1,0-11.1-5-11.1-11.2V40.5L63,66.3c-1.9,2-4.7,3.2-7.7,3.3c-3,0-5.9-1.2-8-3.3c-2.1-2.1-3.3-4.9-3.3-7.9s1.2-5.8,3.3-7.9L92,5.7C94.4,3.6,97.2,2.5,100.1,2.5zM54.5,57.8L54.5,57.8C54.5,57.7,54.5,57.7,54.5,57.8z"/><path class="st0" d="M188.9,128.6c-6.2,0-11.2,5-11.2,11.1v38.1h-48.4H70.7H22.2v-38.1c0-6.1-5-11.1-11.2-11.1c-6.1,0-11.1,5-11.1,11.1v49.2c0,6.3,4.9,11.2,11.1,11.2h60.3h57.3h60.3c6.2,0,11.1-4.9,11.1-11.2v-49.2C200,133.5,195,128.6,188.9,128.6z"/></svg><br><span>Upload a file&hellip;</span></label> 

     <input type="submit" class="button-primary" id="submit" name="submit" tabindex="6" value="Submit Query" /> 
    </form> 
+0

ありがとう@MajidAbbasi、最初のオプションはファイルを添付していますが、tmp_namesとファイル拡張子のないメールクライアントに表示されています。私は$ attachments [] = $ file ['tmp_name']を変更しようとしました。 〜への添付ファイル[] = $ファイル['名前'];しかし、それは全く何も送らない。助言がありますか? – DakotaDC3

+0

私が送った2番目の方法を使用して、worpressディレクトリにアップロードしてください。その後にそれらを添付してください。 –

+0

あなたはあなたのワードプレスのアップロードフォルダにファイルをアップロードしてから添付ファイルに追加します –

関連する問題