How to Send e-mail using Laravel 5 with Example
In this post we will give you information about How to Send e-mail using Laravel 5 with Example. Hear we will give you detail about How to Send e-mail using Laravel 5 with ExampleAnd how to use it also give you demo for it if it is necessary.
To sending mail, Laravel provides drivers for Mandrill, SMTP, Amazon SES and PHP mail function.
Mailgun and Mandrill that are api based drivers are mostly faster and simpler rather than SMTP servers.
Guzzle HTTP library should be installed in your application if don’t have installed then run this command after adding in composer.json file.
"guzzlehttp/guzzle": "~5.3|~6.0"
To use all api based driver you need to set the driver option in your config/mail.php and set credentials in config/services.php
Mailgun Driver
- 'mailgun'=>[
- 'domain'=>'your-mailgun-domain',
- 'secret'=>'your-mailgun-key',
- ],
'mailgun' => [ 'domain' => 'your-mailgun-domain', 'secret' => 'your-mailgun-key',],
Mandrill Driver
- 'mandrill'=>[
- 'secret'=>'your-mandrill-key',
- ],
'mandrill' => [ 'secret' => 'your-mandrill-key',],
Laravel Mail Send Example
Use Mail facade to send mail via send method. The send method contain three parameters. First parameter is your view blade file where you write your messages, second parameter is for passing array data to view and last one is closure callback that receives a message instance through which you can customize the subjects, recipients and other features of mail messages.
- Mail::send('email.welcome',['name'=>$name],function($message)use($data)
- {
- $message->to($data['email'],$data['name'])->subject('Welcome to Expertphp.in!');
- });
Mail::send('email.welcome', ['name' => $name], function ($message) use($data) { $message->to($data['email'], $data['name'])->subject('Welcome to Expertphp.in!'); });Now you can see we are passing $data in view welcome.blade.php in email directory
Access $data in view :
- <!--?phpecho$data['name'];?>
Add Attachments
- Mail::send('users.file',$data,function($message){
- $message->attach($pathToFile);
- });
Mail::send('users.file', $data, function ($message) { $message->attach($pathToFile);});You can easily sent mail in laravel with files, you have to put your file path only in attachment method.
Send emails with activation link after successful Register
- $this->validate($request,[
- 'name'=>'required',
- 'email'=>'required|email',
- 'password'=>'required'
- ]);
- $user=new User;
- $user->name=$request->get('name');
- $user->email=$request->get('email');
- $user->password=$request->get('password');
- $user->verification_token =md5(uniqid('KP'));
- $user->save();
- $activation_link=route('user.activate',['email'=>$user->email,'verification_token'=>urlencode($user->verification_token)]);
- Mail::send('users.email.welcome',['name'=>$user->name,'activation_link'=>$activation_link],function($message)use($user,$activation_link)
- {
- $message->to($user->email,$user->name)->subject('Welcome to Expertphp.in!');
- });
$this->validate($request, [ 'name' => 'required', 'email' => 'required|email', 'password'=>'required' ]); $user = new User; $user->name=$request->get('name'); $user->email=$request->get('email'); $user->password=$request->get('password'); $user->verification_token = md5(uniqid('KP')); $user->save(); $activation_link = route('user.activate', ['email' => $user->email, 'verification_token' => urlencode($user->verification_token)]); Mail::send('users.email.welcome', ['name' => $user->name, 'activation_link' => $activation_link], function ($message) use($user,$activation_link) { $message->to($user->email, $user->name)->subject('Welcome to Expertphp.in!'); });When you have to send mail to user for reminder to activate his account then use above code to send mail after user registration
Hope this code and post will helped you for implement How to Send e-mail using Laravel 5 with Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs