laravel attachment with Send Email with Multiple Attachment is always cumbersome work in web application. Laravel provides a very simple api to send mail to users.
laravel attachment
Laravel latest version of the file attachment helpers. Today in this article We are going to show you how to send mail with laravel attachment.
Step 1: Install Laravel
composer create-project --prefer-dist laravel/laravel sendEmailExample
Step 2: Make Configuration
.env
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 [email protected] MAIL_PASSWORD=rrnnucvnqlbsl MAIL_ENCRYPTION=tls [email protected] MAIL_FROM_NAME="${APP_NAME}"
Don’t miss : Unique Collection – Laravel Collection Get Unique Values Example
Step 3: Create Mail
In this phase i will make mail class SendDemoEmailExample for email sending.
php artisan make:mail SendDemoEmailExample
app/Mail/SendDemoEmailExample.php
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class SendDemoEmailExample extends Mailable { use Queueable, SerializesModels; public $information_data; /** * Create a new message instance. * created by Pakainfo.com * @return void */ public function __construct($information_data) { $this->information_data = $information_data; } /** * Build the message. * created by Pakainfo.com * @return $this */ public function build() { $this->subject('Mail from pakainfo.com') ->view('emails.SendDemoEmailExample'); foreach ($this->information_data['files'] as $file){ $this->attach($file); } return $this; } }
Step 4: Create Blade View
resources/views/emails/SendDemoEmailExample.blade.php
<!DOCTYPE html> <html> <head> <title>pakainfo.com</title> </head> <body> <h2>{{ $information_data['title'] }}</h2> <p>{{ $information_data['body'] }}</p> <p>Thank you</p> </body> </html>
Step 5: Add Route
routes/web.php
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\EmailController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | created by Pakainfo.com | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('send-email', [EmailController::class, 'index']);
Step 6: Add Controller
app/Http/Controllers/EmailController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Mail; use App\Mail\SendDemoEmailExample; class EmailController extends Controller { /** * Write code on Method * created by Pakainfo.com * @return response() */ public function index() { $files = [ public_path('files/98256656665589.pdf'), public_path('files/65546656565326.png'), ]; $information_data = [ 'title' => 'Mail from pakainfo.com 2', 'body' => 'Demo Example: This pakainfo is for testing email using smtp', 'files' => $files ]; Mail::to('[email protected]')->send(new SendDemoEmailExample($information_data)); dd("Email is Sent."); } }
I hope you get an idea about laravel attachment.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.