简体   繁体   中英

How to make a send email form using laravel

Route:

 Route::get('sendemail', function () { $data = array( 'name' => "Learning Laravel", ); Mail::send('AltHr/Portal/welcome', $data, function ($message) { $message->from('rajveer.digi@gmail.com', 'John Doe'); $message->to('ra7veer@gmail.com')->subject('Alt Support'); }); return "Your email has been sent successfully"; }); 

Welcome.blade.php:

 <html> <head> <meta charset="utf-8"> </head> <body> <div> Hello Idris test test </div> </body> </html> 

Hey guys so i have set up my config/mail.php file and my .env file and i am using sendgrid smtp to send email.

The code above is a simple code that i manage to so when i open my browser to : localhost/sendemail it sends an email directly to my email stated in the code.

Now im trying to create a form to send that email basically a contact me form like this

 <form role="form" class="mt-15"> <div class="form-group form-group-default"> <label>Full Name*</label> <input type="text" placeholder="As per IC" class="form-control" required> </div> <div class="form-group form-group-default"> <label>Company*</label> <input type="text" placeholder="Company name" class="form-control" required> </div> <div class="form-group form-group-default"> <label>Email*</label> <input type="email" placeholder="Company email preferred" class="form-control" required> </div> <div class="form-group form-group-default"> <label>Category</label> <select class="full-width form-control"> <option value=""></option> <option value="1">Sign Up</option> <option value="2">Onboarding</option> </select> </div> <div class="form-group form-group-default"> <label>Message*</label> <textarea placeholder="Please type your message here" style="height:100px" class="form-control" required></textarea> </div> <div class="form-group form-group-default"> <label>Attachment</label> <input type="file" name="pic" accept="file_extension|image/*|media_type"> </div> <div class="sm-pt-10 clearfix"> <p class="pull-left small hint-text mt-5 font-arial">*indicates required field</p> <button class="btn btn-primary font-montserrat all-caps fs-12 pull-right xs-pull-left">Submit</button> </div> <div class="clearfix"></div> </form> 

So i have created the form now i want to know how i can i do so that the details will be dynamic and not hardcoded in the route file

How can i do that?

Change your route to post

Route::post('sendemail', function (Request $request) {

    $data = array(
        'name' => $request->name,
         'mail'=>$request->mail,
          'message'=>$request->message,
         'category'=>$request->category,
         'company'=>$request->company
    );

    Mail::send('AltHr/Portal/welcome', $data, function ($message) use($request) {

        $message->from($request->mail,$request->name);

        $message->to('ra7veer@gmail.com')->subject('Alt Support');

    });

    return "Your email has been sent successfully";

});

change form to:

<form role="form" action={{route('sendemail')}} method="post" class="m-t-15">
  <div class="form-group form-group-default">
    <label>Full Name*</label>
    <input type="text" name="name" placeholder="As per IC" class="form-control" required>
  </div>
  <div class="form-group form-group-default">
    <label>Company*</label>
    <input type="text" name="company" placeholder="Company name" class="form-control" required>
  </div>
  <div class="form-group form-group-default">
    <label>Email*</label>
    <input type="email" name="mail" placeholder="Company email preferred" class="form-control" required>
  </div>
      <div class="form-group form-group-default">
                  <label>Category</label>
                  <select name="category" class="full-width form-control">
                    <option value=""></option>
      <option value="1">Sign Up</option>
      <option value="2">Onboarding</option>
                  </select>
              </div>
  <div class="form-group form-group-default">
    <label>Message*</label>
    <textarea name="message" placeholder="Please type your message here" style="height:100px" class="form-control" required></textarea>
  </div>
  <div class="form-group form-group-default">
    <label>Attachment</label>
    <input type="file" name="pic" accept="file_extension|image/*|media_type">
  </div>

  <div class="sm-p-t-10 clearfix">
    <p class="pull-left small hint-text m-t-5 font-arial">*indicates required field</p>
    <button class="btn btn-primary font-montserrat all-caps fs-12 pull-right xs-pull-left">Submit</button>
  </div>
  <div class="clearfix"></div>
</form>

your email blade

<html>
<head>
    <meta charset="utf-8">
</head>
<body>

<div>
  {{name}}
{{mail}}
{{message}}
{{category}}
{{company}}
</div>

</body>
</html>

To attach the file use $message->attach($pathToFile);

more info at: https://laravel.com/docs/5.1/mail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM