简体   繁体   中英

Send MULTIPLE Attachments from Input on Form via PHPMailer

I have searched all around but cannot find a specific answer. I have a form there the user can put in MULTIPLE attachments. I am using PHPMailer to send the submitted form. I have looked everywhere and it seems people who explain how to upload multiple files but NOT from user input. THANK YOU!.

My attachment upload button name is "attachments"

if (isset($_POST["submit"])) {

$optionOne = $_POST['optionOne'] ;
$tick = $_REQUEST['tick'] ;
$results = $_REQUEST['results'] ;
$option = $_POST['option'] ;
$option = $_POST['requirements'] ;


$mail = new PHPMailer;
    //Server settings
$path = 'upload/' . $_FILES["attachments"]["name"];
 move_uploaded_file($_FILES["attachments"]["tmp_name"], $path);

    $mail->SMTPDebug = 2;                                       // Enable verbose debug output
    $mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->isSendmail();  // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'bonnie';                     // SMTP username
    $mail->Password   = 'bonnie';                               // SMTP password
    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('bonniethompson12345@gmail.com', 'Mailer');
    $mail->addAddress('bonniethompson12345@gmail.com');     // Add a recipient

    $mail->isHTML(true);          
    $mail->AddAttachment($path);                        // Set email format to HTML
    $mail->Subject = 'Form submission';
    $mail->Body    = 'This course is identified in my Work Plan and Learning Agreement: $optionOne \n \n I am attending this session because: $tick \n \n What would you like to achieve as a result of your attendance: $results \n \n Do you require adjustments or additions to the session delivery to support your participation: $option \n \n Please provide details of your requirments: $requirements</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    //$mail->AddAttachment('Logo.png');

if(!$mail->send()) {
    echo "Failure to send";
} else {
    echo "Message has been sent successfully";
}

}
<p>Please upload any supporting documentation to support your registration request </p>
<div class="browse-button">
  <input type="file" name="attachments" multiple="multiple"></input>
</div>

The thing you're missing is the naming of the input element; you need to use array naming to make it handle multiple files correctly, so build it like this:

<input type="file" name="attachments[]" multiple="multiple">

The important bit is the [] at the end of the element name, which means that the multiple values will get submitted as an array. When you receive a submission from that, try a var_dump($_FILES) so you can see what you've got. You should also check the return value of move_uploaded_file() rather than assuming it works. Also you don't need a closing </input> tag if you're using HTML5.

The multiple file upload example provided with PHPMailer demonstrates exactly what you're asking, and includes the error checking I mentioned.

It's also covered in the PHP docs . Look harder next time!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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