I want to attach multiple files while sending email in CakePHP3.x.
Here is my data & tried Like so:
$files = [
(int) 0 => [
'getFileName' => '1568016275_452872.xlsx',
'getOriginalFileName' => 'DID Check.xlsx',
'destinationPath' => '\webroot\/upload/files/'
],
(int) 1 => [
'getFileName' => '1568016275_430107.csv',
'getOriginalFileName' => 'clists.csv',
'destinationPath' => '\webroot\/upload/files/'
]
]
$email->setAttachments($files)->send();
I also tried but only send the last one in the email message:
foreach($files as $file){
$email->setAttachments([$file['getOriginalFileName'] => $file['destinationPath'].$file['getFileName']])
}
Can't understand, need help and thanks in advance.
Email::setAttachments()
overwrites all info about previous attachments. So looping through your files and calling setAttachments()
for each one will result in only the last one being sent.
To solve this, you can use either Email::addAttachments()
, which will just add attachments without overwriting current data, or prepare your array of attachments first and then set it once using setAttachments()
.
Further info: