Sending Emails using Laravel Mailables
Laravel Mailables introduce mailable class for sending emails without any fuss in Laravel 5.3. Like other Laravel classes, Mailable class provide several methods like view()
, from()
, text()
, attach()
etc.
Today I am going to tell you how to send a simple email with Gmail SMTP connection using Laravel Mailable class.
Previously, we used to send emails something like below.
Mail::send('emails.welcome', [ 'user' => $user, 'message' => $message ], function ($message) use ($user) { $message->from('[email protected]','Hello Laravel'); $message->to($user->email, $user->name)->subject('Welcome to Laravel Mailables'); });
Step 1: Generate Laravel Mailable Class
In this step, we will generate a mailable class for reminder using artisan command. Laravel store all Laravel Mailable classed in app\Mail
folder, Mailable classes are responsible for handling data and passing them to views.
php artisan make:mail Reminder
After running above command you will a new file created at app\Mail
named Reminder. Open this file a add below code in it.
namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue; class Reminder extends Mailable { use Queueable, SerializesModels; /** * Create a new message instance. * * @return void */ public function __construct() { // } /** * Build the message. * * @return $this */ public function build() { return $this->view('emails.welcome'); } }
You can configure the sender in two way. First, you can use from a method within mailable class’s build method like this.
public function build() { return $this->from('[email protected]') ->view('emails.welcome'); }
Information
You can also send attached files in email, you just need to pass full path in attach method within the mailable class’s build method.
Step 2: Mailer Configuration
I am going to use gmail service to send emails so I have to add my gmail username and password which is smtp configured. You can configured these details directly in config/mail.php
file or .env
file. I suggest you to add your credentials in .env
file so open your .env
file and fill in your following details.
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=username@gmail.com MAIL_PASSWORD=your-gmail-password MAIL_ENCRYPTION=tls
Step 3: Route
In this step, we will add route to send test mail to make sure it working fine or not. so open your web route file ( routes\web.php
) and add following route :
Route::get('send-mail','UserController@sendMail');
Step 4: Create UserController
In this step we will create a controller file with sendMail
method where we will write code to send test emails. SO create a UserController
in following directory app/Http/Controllers/
and add following line of code.
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use Mail; use App\Mail\Reminder; class UserController extends Controller { /** * Send Reminder E-mail Example * * @return void */ public function sendMail() { $to_email = '[email protected]'; Mail::to($to_email)->send(new Reminder); return "E-mail has been sent Successfully"; } }
Step 5: Create Mail Template
Now I will create a view file to send email text. First create a emails directory and within this directory create a welcome.blade.php
file in following path resources/views/emails/
.
<h3>Hi</h3> <p>Welcome to Laravel Mailables</p>
Now if you visit the route we created earlier, an email will be sent.
If you have any question or suggestion, please leave it in the comment box below.