Laravel Mailer不发送或重定向

时间:2017-07-19 12:09:32

标签: php laravel laravel-5.4

所以我创建了一个联系邮件,但它没有发送,因此也没有重定向,任何帮助都将不胜感激。

以下是相关文件:

以下是contact.show

@extends('layouts.app')

@section('content')
  <section class="title">Contact</section>

  <section class="text-center">
    <p>Do you have a project in mind, or just want a chat to see what I can do for your company?</p>
    <p>Not to worry, just send me an email and I shall be sure to get back to you.</p>
  </section>

  <form method="POST" action="{{ url('contact') }}">
    {{ csrf_field() }}

    <section class="{{ $errors->has('mame') ? ' has-error' : '' }}">
      <input id="name" type="text" name="name" required placeholder="Full Name">

      @if ($errors->has('name'))
        <span class="help-block">
          <strong>{{ $errors->first('name') }}</strong>
        </span>
      @endif
    </section>

    <section class="{{ $errors->has('email') ? ' has-error' : '' }}">
      <input id="email" type="email" name="email" required placeholder="Email Address">

      @if ($errors->has('email'))
        <span class="help-block">
          <strong>{{ $errors->first('email') }}</strong>
        </span>
      @endif
    </section>

    <section class="{{ $errors->has('subject') ? ' has-error' : '' }}">
      <input id="subject" type="text" name="subject" required placeholder="Subject">

      @if ($errors->has('subject'))
        <span class="help-block">
          <strong>{{ $errors->first('subject') }}</strong>
        </span>
      @endif
    </section>

    <section class="{{ $errors->has('message') ? ' has-error' : '' }}">
      <textarea id="message" type="text" name="message" value="message" required placeholder="Type in your message here"></textarea>

      @if ($errors->has('message'))
        <span class="help-block">
          <strong>{{ $errors->first('message') }}</strong>
        </span>
      @endif
    </section>

    <button type="submit" class="light-button">Send Equiry</button>
  </form>
@endsection

以下是ContactController

<?php

namespace benbagley\Http\Controllers;

use Illuminate\Http\Request;
use Mail;

class ContactController extends Controller
{
    // Render Contact View
    public function show() {
      return view('contact.show');
    }

    public function thank_you(Request $request) {
      $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email',
        'subject' => 'required|min:3',
        'HAMessage' => 'required|min:10'
      ]);

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

      Mail::send('contact.mail', $data, function($message) use ($data) {
        $message->from($data['email']);
        $message->to('ben@benbagley.co.uk');
        $message->subject($data['subject']);
      });

      return redirect('contact.thank_you');
    }
}

以下是thank you页面

@extends('layouts.app')

@section('content')
  <section class="thankyou text-center">
    <h3>Thank you for your email</h3>
    <p>I shall reply to you as soon as possible available!</p>
  </section>
@endsection

以下是路线:

Route::get('contact', 'ContactController@show');
Route::post('contact', 'ContactController@thank_you');

Route::get('/contact/thank_you', function () {
    return view('contact.thank_you');
});

我还在.env文件中配置了MailTrap:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=...
MAIL_PASSWORD=...
MAIL_ENCRYPTION=null

我在日志中看不到任何内容,任何帮助都会很棒。

-

修改

问题已修复。

'HAMessage' => 'required|min:10'

应该是

'message' => 'required|min:10'

并在数组中

'message' => $request->message

应该是

'HAMessage' => $request->message

感谢大家的回复,非常感谢

3 个答案:

答案 0 :(得分:1)

你可以像这样重新定位laravel

   return Redirect::to('contact.thank_you');

如果你想传递一些参数,那么试试这个

return Redirect::to('contact.thank_you')
->with(['parameter_name'=>'Any message']);
if($data){
    Mail::send('contact.view_mail',['email' => $data['email']], function($message)
    use($data){
            $message->to($data['email'])->subject('Welcome!');              
         });
      return Redirect::back()->with('succ_msg',"Email send Successfully");     
                }

还要检查这些字段名称

 $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email',
        'subject' => 'required|min:3',
        'HAMessage' => 'required|min:10'
      ]);

      $data = array(
        'name' => $request->name,
        'email' => $request->email,
        'subject' => $request->subject,
        'HAMessage' => $request->message
      );
此行中的

错误'message' => $request->message

答案 1 :(得分:1)

use Illuminate\Support\Facades\Mail; 

并使邮件发送功能如下

Mail::send('contact.mail' , $data, function($message) use ($data)
{
    $message->to('ben@benbagley.co.uk',$data['message'])->from($data['email'])->subject($data['subject']);
});

答案 2 :(得分:0)

我认为这条规则失败了:

'HAMessage' => 'required|min:10'

您的标记显示name="message"作为名称属性:

<textarea id="message" type="text" name="message" ...

将规则从'HAMessage'修改为'message',它应该可以正常运行。

错误未显示的原因也是因为它以'HAMessage'的形式发回,而您没有检查错误。

考虑重新思考调试此类问题的方法,我相信下次您可能会在下次立即自行解决问题。快乐的编码!