我的wp_mail附件未通过电子邮件发送

时间:2017-11-12 09:49:53

标签: php wordpress email-attachments

我创建了一个简单的前端WordPress表单(带有多个附件),可以创建新帖子和电子邮件。一切都按预期工作,但附件未送达。

请你看看下面的代码,我试图将文件路径作为数组插入$ attachments,但没有发送任何内容。我不确定我做错了什么。

这是触发电子邮件并处理文件上传的代码段:

//SEND THE EMAIL
    function set_html_content_type() {
        return 'text/html';
    }
    $inv_query_email = 'name@site.co.za';
    $admin_email = 'admin@site.co.za';
    $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表单:

<!-- 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>

1 个答案:

答案 0 :(得分:1)

在这种情况下,我建议您创建附件,如下面的PHP代码:

x

上传WordPress上传文件夹中的文件,之后添加到附件:

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;
}

我使用以下代码进行测试,一切正常:

$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;
}