How to send emails from Laravel

This post is about how to send emails from Laravel in an easy way.

step 1

First you have to  create a view in your laravel project for the email message. I will create a view file in resources/views/mail.blade.php  in my project and paste the following piece of line or put what you want to send as the mail.

This is a test mail

step 2

Then create the controller class and method for your emailing function. You can customize this as you wish. Run the following command on your project terminal..

php artisan make:controller mailController

Then add mail provider  in your controller as following.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;

class mailController extends Controller
{
    public function send(){
        Mail:: send(['text'=>'mail'],['name'=>'piyumikablog'],function($message){
            $message->to('usereamil@gmail.com','testing')->subject('Testing');
            $message->from('sender@gmail.com','me');
        });
    }
}


step 3

Replace the sender and user emails as you want.It's better to try this using https://mailtrap.io .
Now you have to configure .env file as below.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=465
MAIL_USERNAME=mailtrap username
MAIL_PASSWORD=mailtrap password

MAIL_ENCRYPTION=null

for the demonstration i have used mailtrap.io credentials. Using mailtrap you can view, test and share emails within your application. It gives you the configuration details. copy paste those details as in your mailtrap to your .env file as above . If you are using your current gmail then configure .env file as below

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=username@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls

step 4

Then you have to create the route for the email.blade.php. Go to routes/web.php and add the below part of code.

Route::get('send','mailController@send');

step 5

Now you can run the command in your terminal.

php artisan serve

step 6

If you have used your real gmail account details for the .env configuration and if it gives you an error in email's security then go to your gmail and click the button to allow for less secure apps. Then stop the terminal(ctrl+c) and again run the project(php artisan serve). Then probably you will solve the problems. 

When you want to send email to custom email according to the database or user inputs simply use the following structure of code.


public function sendEmail(Request $request, $id)
    {
        $user = User::findOrFail($id);

        Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
            $m->from('hello@app.com', 'Your Application');

            $m->to($user->email, $user->name)->subject('Your Reminder!');
        });
    } 
} 


Since we are passing as array containing the user key in the example code, we could display the user's name within our e-mail view using the following PHP code.
(in mail.blade.php file)
<?php echo $user->name; ?>
When you want to display the message in html format replace 'text' with 'html'
..
 public function send(){
        Mail:: send(['html'=>'mail'],['name'=>'piyumikablog'],function($message)....
..


Now all the things are done..After doing all above, you can see email preview as below.




you can refer this video for a clear idea.....

Comments

Popular posts from this blog

How to implement a Laravel 5 Rating System

About angular

How to implement malloc function using java