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(
	'email'=>'myemail@mailserver.com',
	'name'=>'Laravelovich'
);

// 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('admin@site.com', '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">
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<h2>Welcome to my site</h2>

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

Let me know if you have any questions!

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

UPDATE 2:
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.

http://mailchimp.com/resources/html-email-templates/

Liked it? Take a second to support Maks Surguy on Patreon!
Become a patron at Patreon!

You may also like

26 comments

  • Lee ibrahim August 16, 2013  

    Hi,

    Thanks for your tutorial.
    I used the tutorial and set a filter which showed the execution was a success but the problem is it did not send the mail to the set address email address.

    How can I achieve this without using SwiftMailer because your solution is the only one which has got me where I am at the moment.

    Greatly appreciate your help.

  • Jesse August 21, 2013  

    Thanks for this tutorial! How would I go about sending the message to more than one person?

  • Maks Surguy August 22, 2013  

    have you tried separating the emails with a comma, ex :
    $message->to(’email@.com, email2@mail.com, email3@mail.com‘)->subject(‘Welcome to My Laravel app!’);
    ?

  • Renato Theodoro September 4, 2013  

    Great! This article helped me!

  • Maks Surguy September 4, 2013  

    Were you able to solve this problem?

  • Calin September 11, 2013  

    Great post!
    For Jesse
    You just need to pass an array containing all the email;
    $to = array(’email1@domain.tld’, ’email2@domain.tld’);

    and then…

    Mail::send(’emails.welcome’, $data, function($message) use ($to)
    {
    $message->from(‘admin@site.com’, ‘Site Admin’);
    $message->to($to)->subject(‘Welcome to My Laravel app!’);
    });

  • Pingback: Missing notes for Laravel 4 | Andrews Blog September 17, 2013  
  • Tortue Torche October 16, 2013  

    Great Mail class introduction.
    By the way, there is a typo in:

    “`php
    $data = array(
        ‘detail’=>’Your awesome detail here’,
        ‘name’  => $user[‘name’];
    );
    “`

    Should be :

    “`php
    $data = array(
        ‘detail’=>’Your awesome detail here’,
        ‘name’  => $user[‘name’],
    );
    “`

  • Max October 16, 2013  

    Hello,

    what’d be the best way to check the mailserver response?
    e.g. I am using SMTP; I have tried along the lines of

    $returncode = Mail::send(…….)

    Log::info( ….. $returncode)

    but to no avail at all. It’s a bit a shot in the dark not to be able to check whether the emails made it to the smtp server so as to know whether to delete / requeue the job…

  • Justin December 16, 2013  

    How do I verify if email was sent successfully ?

    Does Mail::send() it throws true or false?

  • Maks Surguy December 17, 2013  

    I believe it is the same as using Swiftmailer (that powers Laravel’s mail functionality) so yes, I believe it does return true or false depending on status.

  • Kombian May 14, 2014  

    Great tuts

  • Sangam Uprety May 22, 2014  

    Hi. Short and useful post. But I get email with empty body. Anything missing from my part?

  • Maks Surguy May 22, 2014  

    Make sure you have the view template that has content in it. For example views/emails/welcome.blade.php should have content.

  • VitaliiSestrinskiy June 11, 2014  

    thanks

  • Shashi Kumar August 27, 2014  

    This one is the good help for starting.

    Please same give one atachment xls file in the same mail ,pls suggest me
    Thank you
    shashi kumar

  • Classical October 23, 2014  

    Thanks!

    use ($user) help me to transfer variable 🙂

  • jasmin jasmin December 28, 2014  

    I already did like your tutorial. There is no error but the mail is not sent.
    What is the problem?

  • karthick April 16, 2015  

    Anyone help me I tried lot of methods to send mail but i am getting error class ‘mail’ not found

  • maxsurguy April 16, 2015  

    That means your PHP does not have ‘mail’ extension! You need to recompile your PHP installation with mail extension.

  • karthick April 16, 2015  

    Thanks for ur reply ok i will do.

  • al April 19, 2016  

    public function fire() {
    echo ‘~1′;
    Mail::send(’emailstest’, array(), function($message){
    echo ‘~2’;
    $message->to(‘familyname.surname@companyname.com’, ‘testing’)->subject(‘hello’);
    echo ‘~3’;
    });
    echo ‘~4’;
    }

    Does any one help?

    4 echo messages came out perfectly, but no messages being sent.

Leave a comment