Laravel 6 : 没有发件人地址无法发送邮件

Laravel 6 : Cannot send message without a sender address

为什么我尝试从联系人发送电子邮件到我的 mailtrap 时收到此错误 Error message image

这是我的网络路线routes\web.php

Route::get('contact','ContactFormController@create');

Route::post('contact','ContactFormController@store');

这是我的 .env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:4Lxefu/REQw/cgLLiwCkXD5HPV6HbbwzRF93N1ZNYvI=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=f65b067cdc6edc
MAIL_PASSWORD=3556eea6c9ad58
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

这是我的控制器app\Http\Controllers\ContactFormController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

// ! IMPORT
use App\Mail\ContactFormMail;
use Illuminate\Support\Facades\Mail;

class ContactFormController extends Controller
{
    //

    public function create(){
        return view('contact.create');
    }

    public function store(){

        $data = request()->validate([
             'name'=> 'required',
             'email'=> 'required|email',
             'message'=> 'required',
        ]);

        // ! Create Mail here

        Mail::to('test@test.com')->send(new ContactFormMail());
        // dd(request()->all());
    }
}

这是我的contact.blade.phpresources\views\contact\create.blade.php

@extends('layouts.app')

@section('content')
   <div class="container">
       <div class="card">
           <div class="card-body">
            <div class="card-title"><h1>Contact Form Email</h1></div>
                 <form action="/contact" method="POST">
                <div class="form-group">
                    <label for="name">Name</label>
                    <input type="text" name="name" value="{{old('name')}}" class=" form-control">
                    <div>{{$errors->first('name')}}</div>
                </div>

                <div class="form-group">
                    <label for="email">Email</label>
                    <input type="email" name="email" value="{{old('email')}}" class=" form-control">
                    <div>{{$errors->first('email')}}</div>
                </div>

                <div class="form-group">
                    <label for="message">Message</label>
                <textarea name="message" id="message" cols="30" rows="10" class=" form-control"></textarea>
                    <div>{{$errors->first('message')}}</div>
                </div>
                @csrf
                <button type="submit" class="btn btn-primary">Send Message</button>
            </form>
            </div>
       </div>
   </div>
@endsection

这是我的联系方式-form.blade.php resources\views\emails\contact\contact-form.blade.php

@component('mail::message')
# Introduction

The body of your message.

@component('mail::button', ['url' => ''])
Button Text
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

这是我的邮件app\Mail\ContactFormMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ContactFormMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.contact.contact-form');
    }
}

请检查我是否做错了什么,我是否需要托管此应用程序才能使邮件功能在 mailtrap 上运行? 上面附有错误消息图像。

ContactFormMail 中的邮件一个正确的发件人地址,例如,

return $this->markdown('emails.contact.contact-form')->from('example@test.in', 'Example');

或者你也可以在.env中配置它,比如

MAIL_FROM_ADDRESS=example@test.in
MAIL_FROM_NAME=Example
MAIL_FROM_ADDRESS=null

由于错误说发件人地址丢失,您需要将其更改为其他内容,所以它不是 null

完美方式:

  1. 将示例值 MAIL_FROM_ADDRESSMAIL_FROM_NAME 添加到 .env
  2. 使用php artisan config:clear
  3. 重启服务 命令 Ctrl+C php artisan serve