Laravel (5.7) Mail
Step 1: Install Laravel
composer create-project laravel/laravel emailsend --prefer-dist
After that, we need to define the Email configuration inside the .env file.
Step 2: Email Configuration
For this example, I am using mailtrap. It is a kind of application, which you can use to test your emails. After you will signup, you can see that it provides us following kinds of details.
Host: smtp.mailtrap.io
Port: 25 or 465 or 2525
Username: // some username
Password: // some password
Auth: PLAIN, LOGIN and CRAM-MD5
TLS: Optional
We have to use these details inside the .env file.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME= // some username
MAIL_PASSWORD= // some password
MAIL_ENCRYPTION=null
If you are using Gmail or your company email address, then please replace your respected credentials here.
Step 3: Create Mailable class
Go to a terminal and type the following command.
php artisan make:mail SendMailable
So, it will create this file inside App\Mail\SendMailable.php.Now, this class contains one property, and that is a namewhich we need to pass when we build this class's instance.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendMailable extends Mailable
{
use Queueable, SerializesModels;
public $name;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($name)
{
$this->name = $name;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.name');
}
}
Here in this class, I have used the parameterized constructor. So we have $name property in our class. Now, we need to create a blade file inside the views folder. We can use that $name property as a data of the view name.blade.php file. Now, first form that view.
Step 4: Create a view file.
Inside views folder, it is better to create a new folder called email and in that folder, make one file called name.blade.php.
<div>
Hi, This is : {{ $name }}
</div>
Step 5: Define the route to send an email
Inside routes >> web.php file, we need to define the route to send an email.
Route::get('/send/email', 'HomeController@mail');
Now, we need to write the code inside mail function to send an email.
// HomeController.php
use Illuminate\Support\Facades\Mail;
use App\Mail\SendMailable;
public function mail()
{
$name = 'Krunal';
Mail::to('[email protected]')->send(new SendMailable($name));
return 'Email was sent';
}
We have included Mailable class and Mail facade. For this example, we have hardcode the name value, but in a real-time example, we have dynamic data to generate an email.
Step 6: Start the server.
Go to your terminal and hit the following command.
php artisan serve
Go to this URL: http://localhost:8000/send/email
Previous:
Laravel (5.7) File Storage
Next:
Laravel (5.7) Notifications
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/laravel/mail.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics