Laravel Emails | Sending emails with multiple attachments

Laravel Emails | Sending emails with multiple attachments

https://ift.tt/2KV6z4P

Laravel Mailable are well equipped for sending emails with multiple attachments. The attachments may be any file type such are pdf, excel, Docx etc. The great thing about this functionality is that setting up attachment is very easy by writing fewer lines of code.

Table of Contents

Setting up controllers

First, we’ll create a controller using the below command.

php artisan make:controller HomeController
 

Sending attachments within the email

Let us create newly mailable using below command.

php artisan make:mail PurchaseInvoiceInformation
 

In this example, class PurchaseInvoiceInformation is used for sending invoice document to customers.

Using the attach() method any type of file can be sent through email. It takes a single parameter which is the system path to the file location.

In laravel_email/app/Mail/PurchaseInvoiceInformation.php.

public function build()
 {
 return $this->from('from-email@example.com')
 ->view('emails.purchase-invoice-information')
 ->attach(public_path('uploads/sample.pdf'));
 
 }
 

To display HTML create a new view at laravel_email/resources/views/emails/purchase-invoice-information.blade.php.

<!DOCTYPE html>
 <html lang="en">
 <head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Purchase Invoice</title>
 </head>
 <body>
 <h1>Test</h1>
 </body>
 </html>
 

Add new method sendEmailWithAttachment() into laravel_email/app/Http/Controllers/HomeController.php controller.

public function sendEmailWithAttachment(Request $request){
 return Mail::to("to-email@example.com")->send(new PurchaseInvoiceInformation());
 }
 

Update routes in laravel_email/routes/web.php with below content.

Route::get('email/send-with-attachment', 'HomeController@sendEmailWithAttachment');
 

Navigating to http://127.0.0.1/laravel_email/public/email/send-with-attachment will send email to the email id you have specified in to() method.

Below is the output you’ll receive when logged into your email account.

laravel send email with attachment

laravel send email with attachment content

 

Attaching Documents with Mime Types

You can also pass file mime type are give a custom name to file. This will help email providers to display your attachment in a proper format.

public function build()
 {
 return $this->from('from-email@example.com')
 ->view('emails.purchase-invoice-information')
 ->attach('file-public-path', [
 'as' => 'file-name-to-be-displayed.pdf',
 'mime' => 'file-mime-type',
 ]);
 }
 

Using as you can give a custom name to your attachment.
Using mime you have to specify which mime type your attachment belongs to.

Attaching Documents from storage disks

If you want to send files from storage disks then you can specify them using the attachFromStorage() method. This method takes a single parameter which is a path to the storage disk.

public function build()
 {
 return $this->from('from-email@example.com')
 ->view('emails.purchase-invoice-information')
 ->attachFromStorage('path-to-storage-disk');
 }
 

Laravel provides various storage disks such as default which is local, amazon cloud etc. To configure disks go to laravel_email/config/filesystems.php file.

Sending multiple attachments through the email

For sending multiple attachments attach() method can be called any number of times. Below is an example of multiple file uploads.

Add sendEmailWithMultipleAttachments() method in controller laravel_email/app/Http/Controllers/HomeController.php.

public function sendEmailWithMultipleAttachments(Request $request){
 $data = [
 "to" => "pavanbaddi911@gmail.com",
 "attachments" => [
 [
 "path" => public_path('uploads/inv-005.pdf'),
 "as" => "Purchase Invoice NO 005.pdf",
 "mime" => "application/pdf",
 ],
 [
 "path" => public_path('uploads/inv-007.pdf'),
 "as" => "Purchase Invoice NO 007.pdf",
 "mime" => "application/pdf",
 ],
 [
 "path" => public_path('uploads/inv-009.pdf'),
 "as" => "Purchase Invoice NO 009.pdf",
 "mime" => "application/pdf",
 ],
 ],
 ];
 return Mail::to($data['to'])->send(new PurchaseInvoiceInformation($data));
 }
 

Refactor class PurchaseInvoiceInformation for sending multiple attachments.

<?php
 
 namespace App\Mail;
 
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Mail\Mailable;
 use Illuminate\Queue\SerializesModels;
 
 class PurchaseInvoiceInformation extends Mailable
 {
 use Queueable, SerializesModels;
 
 /**
 * Create a new message instance.
 *
 * @return void
 */
 
 protected $data;
 
 public function __construct($data)
 {
 $this->data = $data;
 }
 
 /**
 * Build the message.
 *
 * @return $this
 */
 public function build()
 {
 $mail = $this->from('to-email@example.com')
 ->view('emails.purchase-invoice-information');
 
 if(!empty($this->data["attachments"])){
 foreach($this->data["attachments"] as $k => $v){
 $mail = $mail->attach($v["path"], [
 'as' => $v["as"],
 'mime' => $v["mime"],
 ]);
 }
 }
 
 }
 } 
 

$this->data["attachments"] contains an array of attachments which are attached one by one to the attach() method.

Refactor views file laravel_email/resources/views/emails/purchase-invoice-information.blade.php.

<!DOCTYPE html>
 <html lang="en">
 <head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Purchase Invoice</title>
 </head>
 <body>
 <h1>Invoice Information</h1>
 
 <p>Dear User,</p>
 <p>Find invoices for your purchase order INV005, INV007, INV009.</p>
 
 </body>
 </html>
 

Update routes in laravel_email/routes/web.php.

Route::get('email/send-with-multiple-attachments', 'HomeController@sendEmailWithMultipleAttachments');
 

If you navigate to route email/send-with-multiple-attachments in your browser. If mail is sent you will not receive any error message.

Below is the output of email sent.

laravel sending email with multiple attachmentslaravel sending email with multiple attachments content

Take a look at our post on sending emails synchronously and also through a queue.

For more information on laravel mails, you can visit official laravel documentations.

Conclusion

You have come to end of Laravel Emails | Sending emails with multiple attachments post. Support us by sharing this post which will help us grow and comment if you have any doubts we will reach you soon.

programming

via Laravel News Links https://ift.tt/2dvygAJ

May 3, 2020 at 09:03PM