Why Laravel and how it works
Laravel is a PHP framework that stands out for its simplicity, modernity, and connectivity. Probably, those characteristics make it especially popular among startups.
Laravel is widely used for building websites, marketplaces, web apps, and even frameworks. The mail function is essential for these tasks, to register or notify the users, so it’s native in Laravel and provides a wide range of capabilities:
- A list of integrations for sending emails through local or cloud-based services. Now Laravel proposes using drivers for SMTP, Mailtrap Email Delivery, Mailgun, SparkPost, Amazon SES, and
sendmail
. - Options for queueing emails.
- Markdown support, which is available in a quite few frameworks. It lets you create beautiful templates and easily include buttons, tables, or panels.
- Regular plain text and HTML messages.
- Attaching files of different formats and MIME types, as well as raw data, inline attachments, and even embedding raw data into mail templates.
- Templating system, which lets you use various templates and configure the view.
- Message previews in-browser.
In addition, there are some noteworthy options:
- Localization methods, so that you can set the desired language for a specific user.
- Local development mailing. This is how you can prevent sending test emails to real inboxes. Mailtrap is one of the preferred options.
What you need to know about Laravel Mail
You will intuitively find all guidelines on the Laravel website and the educational Laracasts portal. That is why we rejected the idea of creating our own tutorial. For a better understanding, we decided to outline some basic principles, provide you with examples of creating an email in Laravel, and give you some tips and explanations.
Building email in Laravel
Here are a couple of basic things to keep in mind.
- Laravel includes its own command-line interface called
Artisan
. (Yes, it definitely refers to their slogan “The PHP framework for web artisans”). It provides a bundle of useful commands, for email creation in particular. To get all available commands, type:
php artisan list
- Each email category can be represented as a “mailable”. It’s a class that is responsible for building a specific email message using a set of methods.
For example:php artisan make:mail NewUserNotification
command generates a class, which you’ll find at ‘app/Mail/NewUserNotification.php. The build() method of this class creates email messages: