Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Configure Mailtrap SMTP to Send or Test e-Mail in PHP Laravel 5.4 App - 9 Easy Steps

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 6.76k
    Comment on it

    Mailtrap is a fake or we can say test SMTP server which provides us "Developers" a service to send, test or share email messages to the mailbox, In a way that the real user does not get disturbed and mark the email spam. And to do that you have to configure Mailtrap, which can be done easily in just 9 simple steps.

     

    Image Source: https://mailtrap.io/blog/2015-05-30-introduction-to-mailtrap

     

    So, In this tutorial, Today I am going to tell you how to configure mailtrap SMTP and send email in laravel 5.4.

     

    Steps to configure mailtrap:

    1) Register to mailtrap: https://mailtrap.io/register/signup

    2) Go to Inbox page: https://mailtrap.io/inboxes

    3) Click on Demo Inbox link and you will see the following mailtrap smtp credentials as shown below

     

     

    4) Now go to .env file in the root directory of your project and paste the following line:

    MAIL_DRIVER=smtp
    MAIL_HOST=smtp.mailtrap.io
    MAIL_PORT=2525
    MAIL_USERNAME=**************
    MAIL_PASSWORD=**************
    MAIL_ENCRYPTION=null

     

    5) Next, run the following command in the laravel project directory:

    php artisan make:mail Welcome

     

    6) After running the above command Mail folder will be created under app directory if it is not present and Welcome.php file will be created under Mail folder. Replace the code with the following:

    <?php
    
    namespace App\Mail;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Mail\Mailable;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Contracts\Queue\ShouldQueue;
    
    class Welcome extends Mailable
    {
        use Queueable, SerializesModels;
    
        /**
         * Create a new message instance.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }
    
        /**
         * Build the message.
         *
         * @return $this
         */
        public function build()
        {
            return $this->view('emails.welcome');
        }
    }
    

     

    If you compare the above code from the original one in Welcome.php file, then you will see build function has been modified. This function will render the welcome page which has been created under resources/emails/welcome.php

     

    7) Now go to your controller in which you will send the email and include the following line of code on the top:

    use App\Mail\Welcome;

    Above line will call the Welcome class which you just created under app/Mail/Welcome.php

     

    8) Create following function in the controller to send mail:

     

    public function sendmail(){
       $user = Auth::user();
       \Mail::to($user)->send(new Welcome);
       return redirect()->home();
    }

    You can modify sendmail function according to your requirement. Here I am sending mail to respective user by reading the Auth. Use below line to send mail to the respective user.

    \Mail::to($user)->send(new Welcome);

     

    9) Create email template under following directory: laravelProject/resources/views/emails/welcome.blade.php . You can modify welcome.blade.php file according to you:

    <!DOCTYPE html>
    <html>
        <head>
            <title></title>
        </head>
        <body>
            <h1>Welcome to laravel</h1>
        </body>
    </html>

     

     

    Thats all you have to do. If you have any questions please feel free to write in comment section below.

     Configure Mailtrap SMTP to Send or Test e-Mail in PHP Laravel 5.4 App - 9 Easy Steps

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: