Laravel不会在邮件发送

时间:2018-04-15 15:58:36

标签: php laravel-5

首先,该联系表格有效。当所有数据都有效时,它可以正常工作。 “成功”会话消息也有效。

但是,如果我错过了一些必需的数据。什么都没发生。点击提交,页面重新加载,电子邮件不发送(预期),但由于某种原因,laravel不返回'失败'会话消息。

表格

    <div class="row">
    <div class="col-12">
        <form action="{{ url('/#contact') }}" method="POST">
        {{ csrf_field() }}
            <div class="row">
                <div class="form-group col-md-6">
                    <input class="form-control" name="name" id="name" placeholder="Ime i prezime *">
                </div>
                <div class="form-group col-md-6">
                    <input class="form-control" name="email" id="email" placeholder="Email *">
                </div>
                <div class="form-group col-md-6">
                    <input class="form-control" name="telephone" id="telephone" placeholder="Broj telefona">
                </div>
                <div class="form-group col-md-6">
                    <input class="form-control" name="website" id="website" placeholder="Adresa Web stranice">
                </div>
                <div class="form-group col-md-12">
                    <input class="form-control" name="subject" id="subject" placeholder="Naslov poruke">
                </div>
                <div class="form-group col-md-12">
                    <textarea class="form-control" rows="6" name="message" id="message" placeholder="Tekst poruke"></textarea>
                </div>
                <div class="col-md-12">
                    <button type="submit" class="btn btn-default submit btn-block"><i class="fa fa-paper-plane" aria-hidden="true"></i>Pošalji mail</button>
                </div>
            </div>
        </form>
    </div>
    @if (Session::has('success') || Session::has('failed'))
      <div class="col-12">
        @include('partials/_messages')
      </div>
    @endif
</div>

部分(消息)

@if (Session::has('success'))

    <div class="alert alert-success" role="alert">
        <strong>Bravo:</strong> {!! Session::get('success') !!}
    </div>

@endif

@if (Session::has('failed'))

    <div class="alert alert-danger" role="alert">
        <strong>Pozor:</strong> {!! Session::get('failed') !!}
    </div>

@endif

@if (count($errors) > 0)

    <div class="alert alert-danger" role="alert">
        <strong>Pogreška:</strong>
        <ul>
        @foreach ($errors->all() as $error)

            <li>{{ $error }}</li>

        @endforeach
        </ul>
    </div>

@endif

控制器

public function postContact(Request $request) {
    $this->validate($request, array(
        'name' => 'required|max:50',
        'email' => 'required|email',
        'telephone' => 'max:50',
        'website' => 'max:50',
        'subject' => 'min:3',
        'message' => 'required|min:5'
    ));

    $data = array(
        'name' => $request->name,
        'email' => $request->email,
        'telephone' => $request->telephone,
        'website' => $request->website,
        'subject' => $request->subject,
        'bodyMessage' => $request->message
    );

    Mail::send('emails/contact', $data, function($message) use ($data){
        $message->from($data['email']);
        $message->to('no-replay@mail.eu');
        $message->subject($data['subject']);
    });

    Session::flash('success', 'Poruka je uspješno poslana!');

    return redirect()->route('website.index');
}

1 个答案:

答案 0 :(得分:0)

您可以在Mail::failures()

后查看Mail::send(...)
Mail::send('emails/contact', $data, function($message) use ($data){
    $message->from($data['email']);
    $message->to('no-replay@mail.eu');
    $message->subject($data['subject']);
});

if (Mail::failures()) {
   // Error occured
}