2017-11-12 1 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; 
} 

WordPress의 업로드 폴더에 파일을 업로드 한 다음 그 파일을 추가하십시오. ㅇ 첨부 :

$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'];을 변경하려고 시도했다. ~ attachments [] = $ file [ 'name']; 하지만 아무것도 보내지 않습니다. 어떤 제안? – DakotaDC3

+0

내가 사용하는 두 번째 방법을 사용하여 worpress 디렉토리에 업로드 한 후 해당 파일을 첨부하십시오. –

+0

WordPress의 업로드 폴더에 파일을 업로드하고 그 후에 첨부 파일을 추가하십시오. –

관련 문제