当我尝试从联系人向我的mailtrap发送电子邮件时,为什么会出现此错误错误消息图像
这是我的网络路线routesweb.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}"
这是我的控制器appHttpControllersContactFormController.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
// ! IMPORT
use AppMailContactFormMail;
use IlluminateSupportFacadesMail;
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());
}
}
这是我的联系人.blade.phpresourcesviewscontactcreate.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.phpresourcesviewsemailscontactcontact-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
这是我的邮件appMailContactFormMail.php
<?php
namespace AppMail;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
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
。
性能方式:
- 将示例值
MAIL_FROM_ADDRESS
和MAIL_FROM_NAME
添加到.env
- 使用
php artisan config:clear
- 重新启动服务CMD
Ctrl+C
php artisan serve