Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Laravel send multiple attachments to Mailables

David Carr

Laravel Framework Tutorials PHP & MySQL

Rather than returning the direct mail call in the build method of a mailable you can assign it to a variable then add multiple attach calls before returning.

Here’s an example:

public function build()
{
    //setup the mail
    $mail = $this->subject('The subject', ['content' => $content])->markdown('mail.sendreply');

    //loop through attachments and attach to $mail
    $att = Attachment::where('sendEmail', 'Yes')->get();
    foreach($att as $file) {
        //attach the file
        $mail->attach($file->filePath);
    }

    //return and execute sending the mailable
    return $mail;
}

 

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.