Overview of Laravel Mailable
If we think of building an application whether it is Portfolio, blog, E-commerce or Marketing. Sending an email through the application is the basic requirement. It may be in the form of contact us page, Newsletter Subscription, Notifications or any other.
Let’s have a piece of basic knowledge on how laravel handles it.
Laravel makes use of the popular SwiftMailer library, which is easy to use and has a variety of email drivers. By default, Laravel uses SMTP mail. but you can also choose from sendmail, mailgun, mandrill, etc.. Wrapper is implemented on top of the SwiftMailer library that makes email management very easy to configure and use at the same time. Default mail settings are configured at config/mail.php
Before laravel introduce “Mailable” class in version 5.3 it was implemented using the Mail::send method like below, which will send the mail at a time. But, what if you have a huge number of users and want to send mail in bulk, if you use the above method will take much time to get processed will result in increased execution time.
There is a way to overcome it by queuing your mail in backend process. and now here laravel introduce Mailable class (for version 5.3 and above) that comes with a way to achieve it using “queue” method.
Mailables are PHP classes in Laravel 5.3 & above that represent a single email. It is responsible for sending emails using a mailer. It’s configured in the config/mail.php file. In fact, Laravel by default provides an artisan command that allows us to create a base template. See the below example we use to generate for NotifyUser mailable class.php artisan make:mail NotifyUserAfter running above command, you will see a file NotifyUser.php in following directory app/Mail.If you open your file named NotifyUser.php, you will get the “build” method within it. All the email-specific configuration will be done in the “build” method of the class.we can call various methods like from(), cc(), bcc(), subject(), view(), attach(), attachData(), etc…
Below is the sample of build method,
Now, whenever we want to send the mail. we can call as below within the controller
Mail::to($user)->send(new NotifyUser);As I said earlier, In addition to all the above features, you can also use a queue, add cc & bcc and many more.
For the queue feature to be used, we can use the queue()method on the Mail facade after specifying the message’s recipients. see the below example.
but, we need to configure our queue first before using it. you can set it up using different drivers like database, Amazon SQS or redis. I usually prefer database one as is more convenient for me.
You can also use the later() method if you want to delay the delivery of the queued email.
$when = Carbon\Carbon::now()->addMinutes(20);
Mail::to($user)->later($when, new NotifyUser);
It also provides the feature of localization. In Laravel, you can send mailable in a locale other than the current language, and will even remember this locale if the mail is queued.
So, after all, Mailable is not rocket science to be used. But definitely, it makes our job much easier where emails are the primary medium of user-interaction for any application.
If you have any query, we are here for you to assist you. We have an efficient team of Laravel developers that can take you to achieve amazing experience with your Web/Mobile application development with the quality product in a cost-effective way.Take a tour for the services provided by us and get in touch.