0

I was hoping someone could help me/guide me in the right direction with this issue.

I want to create a button called "Send File". When this button is clicked, a directory on my web server is opened where multiple PDF files are stored. I must then have the option of selecting multiple files. Once I click "Okay/Confirm", a new mail must be opened on Outlook with the files added as attachments.

So it's basically like adding an attachment on your local computer through Outlook, but the only difference is the "source" of the file is in a directory on my web server.

I hope this question isn't too broad or not specific enough. I have really no idea how to go about this, so any tips are appreciated. I will attempt to write some code but I don't have a clue on how I'd do this.

<?php 


$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){ //Loop across the selected files.
      $mail->AddAttachment($path . $file, $file, 'base64', 'application/pdf');  //Attach the file.
    }

    if(!$mail->send()) { //Send the mail.
      echo 'Message was not sent.';
      echo 'Mailer error: ' . $mail->ErrorInfo;
    } else {
      echo 'Message has been sent.';
    }

}else{

  echo 'No file was selected.';

  }

} 
?>
  • The problem is not that your question is too broad, just that the answer is not simple, or short. So what code have you written at this point? There is a field type called Filelist in joomla that can be used, but you will need to build ajax and controller validation, then modeling of post values, and finally a helper class in your component to send emails via the Joomla API. So this is not a small task. I can give you most of this code, but first want to make sure we are on the same page. So show me your code. – Llewellyn Jun 15 '18 at 11:15
  • Hello! Thanks for your reply. I will post the code I have written so far, which is not working. I am using a custom module in Joomla and using "sourcerer" – MailBlade Jun 19 '18 at 7:00
  • Apologies for only replying now, I was away for a while doing some exams! – MailBlade Jun 19 '18 at 7:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.