Quick Contact

    SENDING EMAIL USING LARAVEL

    So let’s see how to send email in Laravel. For quick understanding, when you go for some url and mail automatically send to your gmail and that’s all because of backend programming in which some parameter is passed in url which send the mail.

    1. First start with routes. So, “send-mail” function is passed with route, and call controller class etc. as below-:
    2. Route::get (‘/send-mail’, [HomeController::class, ‘mailsend’]);

    3. So how this is working, suppose you have a form and passing a function such as mailsend(), and inside mailsend() function and include a path, in homecontroller.php file too as-:
    4.    Use App\Mail\SendMail;
      And code is as-:
      
      Class MailSend extends Controller
      {
      Public function mailsend()
      {
          $details = [
          ‘title’ => ‘Title: Mail from abc’,
          ‘body’ => ‘Body: This is for testing email using smtp’
      
      ];
      \Mail::to (‘email’)->send(new SendMail ($details));
      Return view (‘emails.thanks’);
      }
      }
      
      

      Now create a ‘sendmail’ inside a app folder and further in mail as-:

      So homecontroller.php i.e. in controller file includes the path as-:

      Use App\Mail\SendMail;

      – So inside the sendmail file write a code which is class basically as class-:

       Class SendMail extends Mailable
      {
           Use Queueable, SerializeModels;
           Public $details;
      And pass the details from the controller as-:
       
       Public function_construct ($details)
          {
                    $this => details = $details;
      }
      

      So inside the controller further passing build function in same file i.e. sendmail as-:

       Public function build ()
      {
           Return $this -> subject (‘mail from abc)
      	View (‘emails.sendmail’);
      }}
      

      So how this works as-:

      As in homecontroller i.e. is controller file mail is passed using function and passing some parameters lile ‘to’ and ‘send’.

      Inside resource folder you there two file should create- one is sendmail.blade.php and other is thanks.blade.php.

    1. As there passing some details in sendmail.blade.php as well as in code-:
    2. 
      < html>
      < head>
          < h1> {{$details [‘title’]}} < /h1>
          < p> {{$details [‘body’]}} < /p>
          < p> Thank you < /p>
      < /body>
      < /html>
      
    3. Now second file which is thanks.blade.php
    4. Code is as-:

      
      < html>
        < head>
        < title> Thanks < /title>
        < link rel=”stylesheet” href=””>
      < /head>
      < body>
          < div class= “container”>
          < div class= “alert alert-success” role= “alert”>
             Thanks, we will contact soon
         < /div>
      < /body>
      < /html>
      

      So when you click the submit button details will send to the respective mail in controller. And to ensure the details would send successfully then the output will be as-:

    Inside of resource folder there is sendmail.blade.php file in which title and body is given and thank you message is given.

    Let’s sum up in few steps-:

    1. Create Laravel project-:
    2. Go to console and write -:

      Laravel new Laravel_sendmail

    3. Open the project and set “env” file as-:
    4. Now add route as write in above explanation.
    5. Now open sendmail.php file as explained above and write code in that
    6. 
      Class SendMail extends Mailable
      {
           Use Queueable, SerializeModels;
           Public $details;
      

      And pass the details from the controller as-:

      
       Public function_construct ($details)
          {
           $this => details = $details;
      }
      

      So inside the controller further passing build function in same file i.e. sendmail as-:

      
       Public function build ()
      {
           Return $this -> subject (‘mail from abc)
      	View (‘emails.sendmail’);
      }}
      
      
    7. As between this controllers are also created as-:
    8. Php artisan make: controller MailSend

      And moreover as explained above mail information is given in this AS-:

      
      Class MailSend extends Controller
      {
      Public function mailsend()
      {
          $details = [
               ‘title’ => ‘Title: Mail from abc,
               ‘body’ => ‘Body: This is for testing email using smtp’
      
      ];
      \Mail::to (‘email’)->send(new SendMail ($details));
      Return view (‘emails.thanks’);
      }
      }
      

      And also include send mail file as-:

      Use \App\Mail\SendMail;

    9. Create view file as “sendmail.blade.php”
    10. File is placed in resources>views>emails>sendmail.blade.php

      In this title and body details are called.

      
      < html>
      < head>
          < h1> {{$details [‘title’]}} < /h1>
          < p> {{$details [‘body’]}} < /p>
          < p> Thank you < /p>
      < /body>
      < /html>
      
    11. Another view file is also created as-:
    12. 
      < html>
          < head>
          < title> Thanks < /title>
          < link rel=”stylesheet” href=””>
      < /head>
      < body>
          < div class= “container”>
           < div class= “alert alert-success” role= “alert”>
                 Thanks, we will contact soon
         < /div>
      < /body>
      < /html>
      
    13. Enter details in config/mail.php
    14. 
      ‘from’ => [
                ‘Address’ => env (‘mail_from_address’, ‘email’) ,
                ‘name’ => env (‘Mail_from_name’, ‘(NAME TO WRITTEN)’),
      ]
      
    15. Run the server with following command-:
    16. >php artisan serve

    17. Open the project in url and by open the sendmail page, mail will be send and you will get the successful output as-:

     

    Apply now for Advanced PHP Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.