Laravel FormRequest& Mailables?

时间:2016-11-24 13:58:46

标签: php laravel email request

有人可以告诉我如何将表单中的请求发送到FormRequest,然后从那里发送每个邮件的数据吗?

这是我的表格:

json_decode

路线<form class="contact__form" style="background-color: #303233 !important;" method="post" action="{{ route('sendContactMail') }}"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="form-group form-group--light form-group--float"> <input type="text" name="name" class="form-control"> <label>Name</label> <i class="form-group__bar"></i> </div> <div class="form-group form-group--light form-group--float"> <input type="text" name="number" class="form-control"> <label>Email Address</label> <i class="form-group__bar"></i> </div> <div class="form-group form-group--light form-group--float"> <input type="text" name="email" class="form-control"> <label>Contact Number</label> <i class="form-group__bar"></i> </div> <div class="form-group form-group--light form-group--float"> <textarea name="message" class="form-control textarea-autoheight"></textarea> <label>Message</label> <i class="form-group__bar"></i> </div>

sendContactMail

使用:Route::post('/sendmail', ['uses' => 'PageController@sendContactMail', 'as' => 'sendContactMail']);

PageController@sendContactMail

是的,我最好停在这里,因为我遵循了一些教程并将它们混合起来,我没有找到FormRequests&amp;的教程。 Mailables。

有人有提示吗?

1 个答案:

答案 0 :(得分:0)

public function signUp(Request $request){

    $data = $request->all();
    $validator = \Validator::make($data, [
      'email' => 'email|unique:users'
      ]);

    if ($validator->fails()) {
      return "Invalid Email ID provided or Email might be an existing user. Please enter a valid email address";
    }else{

        \Mail::send("email.welcome",
          $dataEmail = array(
            "subjectMsg" => "Welcome to .......",
            "pass" => $password,
            "email" => $data['email']
            ), function($message) use ($dataEmail)
          {
            $message->from("hello@domain.com", "You Name here");
            $message->to($dataEmail['email'])->subject($dataEmail['subjectMsg']);
          });


        if(count(\Mail::failures()) > 0){
          return "An error occured. Please try again";
        }else {
          return "success";
        }    
      }



  }

ROUTE

Route::post('/add-user', 'YourController@signUp');

BLABE (resources/views/email/welcome.blade.php) //If you are sending template email

<html>
<head>
  <title></title>
</head>
<body>
<h2>Thank you for signing up</h2>
See your data here: <br>

Subject in the template: {{ $subjectMsg }} 

Your password:           {{ $pass }}
Your email:              {{ $email }}

</body>
</html>

请记住,您的表单操作将提交到此路线:/add-user