You are going to want to control how many files get selected. It is a bad idea to display a large quantity of files/check boxes on you page. Especially if there are so many files you can't query them all without getting an error. (Something else is probably going on with that 503 error.) How many pdfs are we talking about? Anyways...
What happens if you show me just 100 files and I select all 100. Your code is going to send me an email with 100 attachments.(Even 100 will fail, emails are not meant for this.) Then what happens if I really like your pdf's and all I do is sit at my computer all day long, clicking submit over and over sending an email with every press of the button, with 100 attachments every time. Now image 100 people all do that. I'm sure you get the idea, this will destroy your server's resources in a hurry.
If you can get the file names from the db, that's probably your best method. I would paginate the results so say only 20 files are shown at once. I would only let the user select a maximum amount of files per submit as there is most likely a limit to what an email can do. I would even control how often the user could perform this function in a given period of time in order to prevent abuse.
I would also look at just directly downloading the files from your server to the user's computer and completely bypass the email thing all together unless that is not within your current project's goal.
But those are all topics to keep in the back of your mind as you build this. But to get a proof of concept going. Check out below.
Step 1 - Download PHPMailer and get it going so you can call $mail = new PHPMailer;
on a page and it does not error. Here is a link:
PHPMailer
Step 2 - Search your folder structure or db and return an array containing the paths to the files.
Step 3 - Loop over the array and populate your check boxes.
Step 4 - Create PHPMailer object and add the object properties - send the email.
require('path/to/PHPMailerClass');
$path = '/root/path/to/pdfFiles/';
$array = array_map('basename', glob($path . '*.pdf'));
echo
'<form action="" method="post">';
foreach($array as $file){
echo '<input name="fileName[]" type="checkbox" value="' . $file . '">' . $file . '<br>';
}
echo '<input name="send_email" type="submit" value="Send Email">';
echo
'</form>';
$userData = array(
'name' => 'John Doe',
'email' => 'John.Doe@example.com'
);
if(isset($_POST['send_email']) && $_POST['send_email']){
if(isset($_POST['fileName']) && $_POST['fileName']){
if(count($_POST['fileName']) > 5){
echo 'You can only select 5 files.';
exit();
}
$mail = new PHPMailer;
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress($userData['email'], $userData['name']);
$mail->Subject = 'This is how to send attachments.';
$mail->isHTML(true);
$mail->Body = $userData['name'] . ',<br>' .
'Here are the files you selected!';
foreach($_POST['fileName'] as $file){
$mail->AddAttachment($path . $file, $file, 'base64', 'application/pdf');
}
if(!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
}else{
echo 'No file was selected.';
}
}
If you are testing on a localhost setup you may have to do some trouble shooting to get the emails to send. I'm not experienced with that so search google and SO to problem solve that.. If after some good attempts to make it work and you can't figure it out, post another question.