PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 5, 2022

[FIXED] CakePHP 3: Sending Multiple Attachments through A Email

 January 05, 2022     cakephp, cakephp-3.0, cakephp-3.x, email-attachments     No comments   

Issue

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.


Solution

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:

Cake\Mailer\Email::addAttachments()

Cake\Mailer\Email::setAttachments()



Answered By - Szymon
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing