Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php表单附件将作为数组而不是文件发送_Php_Email Attachments - Fatal编程技术网

Php表单附件将作为数组而不是文件发送

Php表单附件将作为数组而不是文件发送,php,email-attachments,Php,Email Attachments,我希望能够通过我的联系人表单发送多个附件,当我发送任意数量的附件时,我会在fake@fake.com. 不知道如何使它们作为单独的附件通过 HTML代码 <form id="contact-form" action="contact.php" method="post" enctype="multipart/form-data"> <div class="row"> <div>

我希望能够通过我的联系人表单发送多个附件,当我发送任意数量的附件时,我会在fake@fake.com. 不知道如何使它们作为单独的附件通过

HTML代码

<form id="contact-form" action="contact.php" method="post" enctype="multipart/form-data">
            <div class="row">
                <div>
                    <div class="form-group">
                        <label for="name">
                            Name</label>
                            <input type="text" class="form-control" id="name" name="name" placeholder="Enter name" required />

                    </div>
                    <div class="form-group">
                        <label for="email">
                            Email Address</label>
                        <div class="input-group">
                            <input type="email" class="form-control" id="email" name="email" placeholder="Enter email" required /></div>
                    </div>
                    <div class="form-group">
                        <label for="subject">
                            Subject</label>
                        <select id="subject" name="subject" class="form-control" required>
                            <option value="na" selected="careers">Choose One:</option>
                            <option value="careers">Careers</option>
                            <option value="general">General</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for ="attach">
                            Attachments</label>
                         <input type="file" class="form-control" name="newupload[]" id="newupload" />
                         <input type="file" class="form-control" name="newupload[]" id="newupload" />
                         <input type="file" class="form-control" name="newupload[]" id="newupload" />
                    </div>
                    </div>
                    <div class="form-group">
                        <label for="name">
                            Message</label>
                        <textarea name="message" id="message" class="form-control" rows="7" cols="20" required
                            placeholder="Message"></textarea>
                    </div>


                    <button type="submit" name="submit" id="btnContactUs">
                        Send Message</button>
            </div>
            </form>

名称
电子邮件地址
主题
选择一个:
事业
一般的
附件
消息
发送消息
PHP


您需要循环文件

我更改了uniqid()的附件id:

试试这个,我希望能帮助你:

<?php

if ($_POST && isset($_FILES['newupload'])) {

    $name = $_POST["name"];
    $email = $_POST["email"];
    $subject = $_POST["subject"];
    $message = $_POST["message"];
    $recipient_email = 'fake@fake.com';

    $user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);

    $boundary = md5("sanwebe");
    //header
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= "From: " . $email . "\n";
    $headers .= "Subject:  " . $subject . "\n";
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

    //plain text
    $body = "--$boundary\r\n";
    $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
    $body .= chunk_split(
        base64_encode(
            'Name: ' . $name . "\n" . 'Email: ' . $email . "\n" . 'Subject: ' . $subject . "\n" . 'Message: ' . $message
            . "\r\n"
        )
    );

    for ($i = 0; $i < 3; $i++) {
        if (!empty($_FILES['newupload']['name'][$i])) {
            //read from the uploaded file & base64_encode content for the mail
            //get file details we need
            $file_tmp_name = $_FILES['newupload']['tmp_name'][$i];
            $file_name = $_FILES['newupload']['name'][$i];
            $file_size = $_FILES['newupload']['size'][$i];
            $file_type = $_FILES['newupload']['type'][$i];
            $file_error = $_FILES['newupload']['error'][$i];

            $handle = fopen($file_tmp_name, "r");
            $content = fread($handle, $file_size);
            fclose($handle);
            $encoded_content = chunk_split(base64_encode($content));

            //attachment
            $body .= "--$boundary\r\n";
            $body .= "Content-Type: $file_type; name=\"$file_name\"\r\n";
            $body .= "Content-Disposition: attachment; filename=\"$file_name\"\r\n";
            $body .= "Content-Transfer-Encoding: base64\r\n";
            $body .= "X-Attachment-Id: " . uniqid('email_', true) . "\r\n\r\n";
            $body .= $encoded_content;
        }
    }

这是您上传的文件结构

Array
(
    [newupload] => Array
        (
            [name] => Array
                (
                    [0] => simple-hotstrings.ahk
                    [1] => 
                    [2] => 
                )

            [type] => Array
                (
                    [0] => application/octet-stream
                    [1] => 
                    [2] => 
                )

            [tmp_name] => Array
                (
                    [0] => T:\Temp\Php\php7A7B.tmp
                    [1] => 
                    [2] => 
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 4
                    [2] => 4
                )

            [size] => Array
                (
                    [0] => 4775
                    [1] => 0
                    [2] => 0
                )

        )

)

谢谢毛里西奥!完美无瑕!10/10
<?php

if ($_POST && isset($_FILES['newupload'])) {

    $name = $_POST["name"];
    $email = $_POST["email"];
    $subject = $_POST["subject"];
    $message = $_POST["message"];
    $recipient_email = 'fake@fake.com';

    $user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);

    $boundary = md5("sanwebe");
    //header
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= "From: " . $email . "\n";
    $headers .= "Subject:  " . $subject . "\n";
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

    //plain text
    $body = "--$boundary\r\n";
    $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
    $body .= chunk_split(
        base64_encode(
            'Name: ' . $name . "\n" . 'Email: ' . $email . "\n" . 'Subject: ' . $subject . "\n" . 'Message: ' . $message
            . "\r\n"
        )
    );

    for ($i = 0; $i < 3; $i++) {
        if (!empty($_FILES['newupload']['name'][$i])) {
            //read from the uploaded file & base64_encode content for the mail
            //get file details we need
            $file_tmp_name = $_FILES['newupload']['tmp_name'][$i];
            $file_name = $_FILES['newupload']['name'][$i];
            $file_size = $_FILES['newupload']['size'][$i];
            $file_type = $_FILES['newupload']['type'][$i];
            $file_error = $_FILES['newupload']['error'][$i];

            $handle = fopen($file_tmp_name, "r");
            $content = fread($handle, $file_size);
            fclose($handle);
            $encoded_content = chunk_split(base64_encode($content));

            //attachment
            $body .= "--$boundary\r\n";
            $body .= "Content-Type: $file_type; name=\"$file_name\"\r\n";
            $body .= "Content-Disposition: attachment; filename=\"$file_name\"\r\n";
            $body .= "Content-Transfer-Encoding: base64\r\n";
            $body .= "X-Attachment-Id: " . uniqid('email_', true) . "\r\n\r\n";
            $body .= $encoded_content;
        }
    }
Array
(
    [newupload] => Array
        (
            [name] => Array
                (
                    [0] => simple-hotstrings.ahk
                    [1] => 
                    [2] => 
                )

            [type] => Array
                (
                    [0] => application/octet-stream
                    [1] => 
                    [2] => 
                )

            [tmp_name] => Array
                (
                    [0] => T:\Temp\Php\php7A7B.tmp
                    [1] => 
                    [2] => 
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 4
                    [2] => 4
                )

            [size] => Array
                (
                    [0] => 4775
                    [1] => 0
                    [2] => 0
                )

        )

)