У меня есть уведомление и класс почты, и я хотел бы отправлять уведомления по электронной почте нескольким случайным пользователям, когда они нажимают кнопку. Любые идеи о том, как помочь с кодом. Ниже приведены 2 класса. Не уверен, что что-то еще нужно. Я уже настроил таблицу пользователей. Я также настроил свой файл .env с учетными данными из моей учетной записи mailtrap, но не уверен, что делать дальше. -Спасибо
Класс уведомления:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class ProvideAnswer extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('Thank you for answering the question.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
Почтовый класс:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class ThankYou 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->view('view.name');
}
}
Форма вопроса:
@extends('layouts.app')
@section('content')
{{ Breadcrumbs::render('home') }}
<div class="container">
<div class="row ">
<div class="col-md-8">
<div class="card">
<div class="card-header">Question</div>
<div class="card-body">
{{$question->body}}
</div>
<div class="card-footer">
<a class="btn btn-primary float-right"
href="{{ route('question.edit',['id'=> $question->id])}}">
Edit Question
</a>
{{ Form::open(['method' => 'DELETE', 'route' => ['question.destroy', $question->id]])}}
<button class="btn btn-danger float-right mr-2" value="submit" type="submit" id="submit">Delete
</button>
{!! Form::close() !!}
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header"><a class="btn btn-primary float-left"
href="{{ route('answers.create', ['question_id'=> $question->id])}}">
Answer Question
</a></div>
<div class="card-body">
@forelse($question->answers as $answer)
<div class="card">
<div class="card-body">{{$answer->body}}</div>
<div class="card-footer">
<a class="btn btn-primary float-right"
href="{{ route('answers.show', ['question_id'=> $question->id,'answer_id' => $answer->id]) }}">
View
</a>
</div>
</div>
@empty
<div class="card">
<div class="card-body"> No Answers</div>
</div>
@endforelse
</div>
</div>
</div>
@endsection
AnswerController:
<?php
namespace App\Http\Controllers;
use App\Answer;
use App\Question;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
class AnswerController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create($question)
{
$answer = new Answer;
$edit = FALSE;
return view('answerForm', ['answer' => $answer,'edit' => $edit, 'question' =>$question ]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, $question)
{
$input = $request->validate([
'body' => 'required|min:5',
], [
'body.required' => 'Body is required',
'body.min' => 'Body must be at least 5 characters',
]);
$input = request()->all();
$question = Question::find($question);
$Answer = new Answer($input);
$Answer->user()->associate(Auth::user());
$Answer->question()->associate($question);
$Answer->save();
return redirect()->route('question.show',['question_id' => $question->id])->with('message', 'Saved');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($question, $answer)
{
$answer = Answer::find($answer);
return view('answer')->with(['answer' => $answer, 'question' => $question]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($question, $answer)
{
$answer = Answer::find($answer);
$edit = TRUE;
return view('answerForm', ['answer' => $answer, 'edit' => $edit, 'question'=>$question ]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $question, $answer)
{
$input = $request->validate([
'body' => 'required|min:5',
], [
'body.required' => 'Body is required',
'body.min' => 'Body must be at least 5 characters',
]);
$answer = Answer::find($answer);
$answer->body = $request->body;
$answer->save();
return redirect()->route('answers.show',['question_id' => $question, 'answer_id' => $answer])->with('message', 'Updated');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($question, $answer)
{
$answer = Answer::find($answer);
$answer->delete();
return redirect()->route('questions.show',['question_id' => $question])->with('message', 'Delete');
}
Mail.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
| "sparkpost", "log", "array"
|
*/
'driver' => env('MAIL_DRIVER', 'smtp'),
/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/
'port' => env('MAIL_PORT', 587),
/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/
'sendmail' => '/usr/sbin/sendmail -bs',
/*
|--------------------------------------------------------------------------
| Markdown Mail Settings
|--------------------------------------------------------------------------
|
| If you are using Markdown based email rendering, you may configure your
| theme and component paths here, allowing you to customize the design
| of the emails. Or, you may simply stick with the Laravel defaults!
|
*/
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];