Posted by Arjun on Friday 6th March 2015

Sending Email in Laravel Application?

Sending Emails is very important requirement of the almost all modern web applications. Sending Emails in Laravel Based applications is very simple because Laravel email class is built on the SwiftMailer package and it's configurable , you can find the config file at app/config/mail.php.

Sending Email

By using mail class facade Mail and send() method we can send basic emails.

$data = [];
Mail::send('emails.welcome', $data, function($message)
{
  //
});

Here : emails.welcome is emails is the sub directory of views, and welcome is the blade template name (welcome.blade.php),

$data is the data to the email template, same as passing data to views.

and third one is the closure with an instance of $message.This allows you to specify different settings like who to send it to and the subject of the message.

$data = [];
Mail::send('emails.welcome', $data, function($message)
{
   $message->to('[email protected]', 'Arjun PHP')
          ->subject('Welcome to Laravel World!');
});

If you want to pass data to the closure, you should pass it using the use keyword.

$data = ['name' =>'Arjun PHP','email'=> '[email protected]','subject' => 'Welcome to the laravle World'];
Mail::send('emails.welcome', $data, function($message) use $data
{
   $message->to($data['eamil'], $data['name'])
          ->subject($data['subject']);
});

That's it.