Sending E-Mail with Laravel 4 using mail

In this short post I will show you how you can send emails with Laravel 4 using the built in mail function of PHP. I don’t want to use any other email transport at this point and just plain PHP mail function can be utilized for sending emails.

It is super easy to send mail with new Laravel 4 functionality without installing any other packages. Laravel 4 has Mail function that uses SwiftMailer as a dependency (but I want to stress it again, you do not need to install anything, Laravel 4 already comes with it).

First and foremost, go to the app/config/mail.php and change the driver to “mail”. Also put the host as blank.

Here is a quick example, in the controller or in the model where I want to send the mail :

// I'm creating an array with user's info but most likely you can use $user->email or pass $user object to closure later
$user = array(

// the data that will be passed into the mail view blade template
$data = array(
	'detail'=>'Your awesome detail here',
	'name'	=> $user['name'];

// use Mail::send function to send email passing the data and using the $user variable in the closure
Mail::send('emails.welcome', $data, function($message) use ($user)
  $message->from('', 'Site Admin');
  $message->to($user['email'], $user['name'])->subject('Welcome to My Laravel app!');

This example above will use the Blade template under views/emails/welcome.blade.php , the contents of that template below:

<!DOCTYPE html>
<html lang="en-US">
		<meta charset="utf-8">
		<h2>Welcome to my site</h2>

			Your sign up details are below:
		<div>{{ $detail }}</div>
		<div>{{ $name}} </div>

Let me know if you have any questions!

As suggested by Twitter user Ryan Tablada, you can find tons of great email templates at this link :

In the current version of Laravel (4.0.6) you can set “pretend” setting in mail.php to true and instead of actually sending the emails you will instead see them in the application logs. This is super helpful when trying to test and debug email capabilities.

You may also like

More in Code Blog
Laravel 4 release

Hey Laravel users! Laravel version 4 has been released this Tuesday! I feel you cheering and smiling because this means...