简体   繁体   English

在 PHP 中使用 wp_mail 发送多个附件

[英]Send multiple attachments with wp_mail in PHP

I'm trying to send multiple attachments with wp_mail() , after some issues of attachments names (it displayed something like phprTxfd.pdf instead of file_name.pdf ), i succeeded to display the name of the attachment.我正在尝试使用wp_mail()发送多个附件,在附件名称出现一些问题之后(它显示了类似phprTxfd.pdf而不是file_name.pdf ),我成功地显示了附件的名称。

But when i'm trying to send multiple attachment, it only display the first attachment in the mail, and not the others.但是当我尝试发送多个附件时,它只显示邮件中的第一个附件,而不显示其他附件。

Here is my code :这是我的代码

function my_custom_email_content_type( $content_type ) {
                return 'text/html';
            }

            if ( ! function_exists( 'wp_handle_upload' ) ) {
                require_once( ABSPATH . 'wp-admin/includes/file.php' );
            }


                $files = $_FILES[ 'fichier' ];

            $upload_overrides = array( 'test_form' => false );

                $attachments = array();

            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 ]
                    );
                    $movefile = wp_handle_upload(
                        $file,
                        $upload_overrides
                    );
                    $attachments[] = $movefile[ 'file' ];
                }
            }

                add_filter(
                 'wp_mail_content_type',
                 'my_custom_email_content_type'
             );
    wp_mail($to, $subject, $message, $headers, $attachments);

I think the issue is coming that $files = $_FILES['fichier'] just save the first file in the variable.我认为问题是$files = $_FILES['fichier']只是将第一个文件保存在变量中。

I tried to put $files as an array, but it doesn't work.我试图将 $files 作为一个数组,但它不起作用。

Thanks.谢谢。

EDIT :编辑

print_r($files) display this : print_r($files)显示这个:

Array ( [name] => Array ( [0] => logo1.png [1] => logo2.png ) [type] => Array ( [0] => image/png [1] => image/png ) [tmp_name] => Array ( [0] => C:\\xamp\\tmp\\phpC62D.tmp [1] => C:\\xamp\\tmp\\phpC62E.tmp ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 4440 [1] => 7830 ) )数组 ( [名称] => 数组 ( [0] => logo1.png [1] => logo2.png ) [类型] => 数组 ( [0] => 图像/png [1] => 图像/png ) [tmp_name] => Array ( [0] => C:\\xamp\\tmp\\phpC62D.tmp [1] => C:\\xamp\\tmp\\phpC62E.tmp ) [error] => Array ( [0] => 0 [1] => 0 ) [大小] => 数组 ( [0] => 4440 [1] => 7830 ) )

when i'm trying to insert 2 or several files in my file input multiple.当我尝试在我的文件中插入 2 个或多个文件时,输入多个。

I was running into this same issue when I came across this post.当我遇到这篇文章时,我遇到了同样的问题。

The OP was very close in determining their issue...I too set it up as above and didn't receive any of my attachments. OP 在确定他们的问题方面非常接近......我也按照上面的方式进行设置,但没有收到我的任何附件。

It turns out, you need to set the 'name' of the file submission field as an array.事实证明,您需要将文件提交字段的“名称”设置为数组。 In the OP case it should be set to name='fichier[]'在 OP 的情况下,它应该设置为 name='fichier[]'

Once I changed that, my multiple files came through.一旦我改变了它,我的多个文件就通过了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM