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/
@msurguy You might also like this little resource mailchimp.com/resources/html…
— Ryan Tablada (@RyanTablada) June 6, 2013
@msurguy ive never sent mail w/ Laravel but on Windows mail() actually sucks ass… so… SMTP Settings supported?
@msurguy You might also like this little resource http://t.co/WB7nQ44nC4
@msurguy Really appreciate all the L4 tuts!
Sending E-Mail with #Laravel 4 using mail | http://t.co/vvqfa9UdI2 #L4
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.
Thanks for this tutorial! How would I go about sending the message to more than one person?
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!’);
?
Great! This article helped me!
Were you able to solve this problem?
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!’);
});
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’],
);
“`
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…
How do I verify if email was sent successfully ?
Does Mail::send() it throws true or false?
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.
Great tuts
Hi. Short and useful post. But I get email with empty body. Anything missing from my part?
Make sure you have the view template that has content in it. For example views/emails/welcome.blade.php should have content.
thanks
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
Thanks!
use ($user) help me to transfer variable 🙂
I already did like your tutorial. There is no error but the mail is not sent.
What is the problem?
Anyone help me I tried lot of methods to send mail but i am getting error class ‘mail’ not found
That means your PHP does not have ‘mail’ extension! You need to recompile your PHP installation with mail extension.
Thanks for ur reply ok i will do.
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.