数组中的文件没有在邮件中添加为多个附件?

时间:2018-06-21 09:04:23

标签: php joomla phpmailer

尤其是在此问题上,我得到了一位用户的极大帮助,但是,我只剩下一件事要定稿,而且似乎无法正常工作。

这是代码:

<?php 
$path = JFolder::files(JPATH_SITE . '/tmp/containers/waybills');

echo '<form action="" method="post">';

foreach($path as $file){
   echo '<input name="fileName[]" type="checkbox" value="' . $file . '">' . $file . '<br>';

}

echo '<input name="send_email" type="submit" value="Send Email">';
echo '</form>';

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();

    }

$mailer = JFactory::getMailer();
$config = JFactory::getConfig();
$sender = array( 

    $config->get( 'mailfrom' ),

    $config->get( 'fromname' ) 

);

$mailer->setSender($sender);

$user = JFactory::getUser();
$recipient = $user->email;

$mailer->addRecipient($recipient);

$body   = "Test";

$mailer->setSubject('Testing');

$mailer->setBody($body);

foreach($_POST['fileName'] as $file2){ //should loop across the files again

      $mailer->addAttachment($file, $file2);  //This should add all the file's where I have ticked their respective checkboxes, as an attachment to the mail.
        }

 if(!$mailer->send()) { 

      echo 'Message was not sent.';

    } else {

      echo 'Message has been sent.';

    }

}else{

  echo 'No file was selected.';

  }
} 
?>

邮件本身没有问题,只是附件没有被附件。

1 个答案:

答案 0 :(得分:1)

在您的代码中

foreach($path as $file){
   echo '<input name="fileName[]" type="checkbox" value="' . $file . '">' . $file . '<br>';

}

与使用双引号一样,name参数仍保留为字符串。 您可以进行以下更改以提供完整的值路径

$files = JFolder::files(JPATH_SITE . '/tmp/containers/waybills');
$path = JPATH_SITE . '/tmp/containers/waybills/'; //path to your files
echo '<form action="" method="post">';

foreach($files as $file){
   echo '<input name="' .$file. '" type="checkbox" value="' . $path.$file . '">' . $file . '<br>';
}

请记住所有文件都应具有唯一的名称。