anonymous
anonymous

Reputation: 257

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('[email protected]', 'John Doe');

        $message->to('[email protected]')->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="m-t-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-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>

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?

Upvotes: 0

Views: 7840

Answers (1)

madalinivascu
madalinivascu

Reputation: 32354

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('[email protected]')->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

Upvotes: 1

Related Questions