我正在尝试提交表单,并根据通过表单提交的数据发送电子邮件,但由于我没有收到成功消息,因此似乎无法正常工作。
控制器
public function contact(Request $request)
{
$request = request()->validate([
'name' => 'required',
'email' => 'required| email',
'message' => 'required | max:1000',
]);
Mail::to('support@mail.com')->send(new contact($request));
return redirect()->back()->with("success", "You email has successfully been sent");
}
表格
@if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
@endif
@if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
<form action="/contact" method="POST">
@csrf
<div class="col-md-12">
<input type="text" placeholder="Full Name" required name="name">
</div>
<div class="col-md-8">
<input type="text" placeholder="email" required name="email">
</div>
<div class="col-md-2">
<textarea name="message"></textarea>
</div>
<div class="col-md-12">
<button class="btn btn-black no-margin-bottom btn-small"
type="submit">Contact</button>
</div>
</form>
路线
`Route::post('/contact', 'HomeController@contact');`
答案 0 :(得分:0)
如果您被重定向回表单页面而没有错误消息或成功的Flash消息,那么我倾向于认为问题出在您的模板中。假设您使用的是laracasts / flash软件包,则您的方法应如下所示
public function contact(Request $request)
{
$request = request()->validate([
'name' => 'required',
'email' => 'required| email',
'message' => 'required | max:1000'
]);
Mail::to('support@mail.com')->send(
new contact($request)
);
return redirect()->back()->with("success", "You email has successfully been sent");
}
redirect()-> with()将数据添加到会话中,但是除非您在这样的模板中手动这样做,否则它不会显示消息
@if (session('sucess'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
答案 1 :(得分:0)
确保在表格中正确显示错误,将session('error')
块替换为以下内容:
@if($errors->any())
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
在您的控制器中:
use App\Mail\Contact;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class HomeController extends Controller
{
// ...
public function contact(Request $request)
{
// Note I'm not overwriting the $request variable.
// This method returns an array of the validated data.
$request->validate([
'name' => 'required',
'email' => 'required|email',
'message' => 'required|max:1000'
]);
Mail::to('support@mail.com')->send(new Contact($request));
return back()->with('success', 'Your email has successfully been sent');
}
}
您的邮件类别应如下所示:
namespace App\Mail;
use Illuminate\Http\Request;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class Contact extends Mailable
{
use Queueable, SerializesModels;
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function build()
{
// The request instance must be passed to the view...
return $this->markdown('emails.contact', [
'request' => $this->request
]);
}
}
您现在可以在电子邮件减价(Request
)中使用emails.contact
对象。例如:
**Name**<br>
{{ $request->input('name') }}
**Email**<br>
{{ $request->input('email') }}
**Message**<br>
{{ $request->input('message') }}