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',
- ],
- 'mandrill' => [
- 'secret' => 'your-mandrill-key',
- ],
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!');
- });
Now you can see we are passing $data in view welcome.blade.php
in email
directory
Access $data
in view :
- echo $data['name']; ?>
- 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!');
- });
When you have to send mail to user for reminder to activate his account then use above code to send mail after user registration