php - Laravel Mailer 不发送或重定向

标签 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('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dab8bfb49ab8bfb4b8bbbdb6bfa3f4b9b5f4afb1" rel="noreferrer noopener nofollow">[email protected]</a>');
        $message->subject($data['subject']);
      });

      return redirect('contact.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

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

最佳答案

use Illuminate\Support\Facades\Mail; 

并制作如下邮件发送功能

Mail::send('contact.mail' , $data, function($message) use ($data)
{
    $message->to('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f0d0a012f0d0a010d0e08030a16410c00411a04" rel="noreferrer noopener nofollow">[email protected]</a>',$data['message'])->from($data['email'])->subject($data['subject']);
});

关于php - Laravel Mailer 不发送或重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45190128/

相关文章:

php - 如何使用PHP从指定行开始读取txt文件?

php - URL 打错 Controller LARAVEL

node.js - 在 laravel 应用程序中从 cmd 运行 gulp 时出现错误

jquery - 搜索后表单和选择不起作用

php - session::flash 和 request->session->flash 的 laravel 区别

javascript - 如何删除输入框中的某些单词

PHP - 无法使用复选框删除 MySQL 行(无表单)

Laravel/Eloquent内存泄漏重复检索相同的记录

laravel 5.4 在邮件中嵌入图像

php - Laravel 5.4 中的数据未从 Controller 存储到数据库中