G
G
GAS-ART2021-03-16 22:28:09
Laravel
GAS-ART, 2021-03-16 22:28:09

How to send form data to email in Laravel?

There is a feedback form.

<form action="{{ route('send') }}" method="post">
                    @csrf

                    <div class="form-group">
                        <label for="name">Ваше имя</label>
                        <input type="text" name="name" placeholder="Введите имя">
                    </div>

                    <div>
                        <label for="email">Ваша почта</label>
                        <input type="text" name="email" placeholder="Напишите вашу почту">
                    </div>

                    <div >
                        <label for="phone">Ваш номер телефона</label>
                        <input type="text" name="phone" placeholder="Напишите ваш номер телефона">
                    </div>
                    <button type="submit">Отправить</button>
                </form>

There is a route for the form:
Route::post('/send', '[email protected]')->name('send');

there is a controller:
namespace App\Http\Controllers;

use App\Contact;
use Illuminate\Http\Request;
use App\Http\Requests\ContactRequest;
use Mail;

class ContactController extends Controller
{
    
   public function send (Request $req){
   $firstname = $req->input('name');
  mail::send(['mailtest' , ['firstname' => $firstname] ], ['name' => 'ARTEM'], function($message) use ($firstname) {
     $message->to('[email protected]', 'ARTEM')->subject('test email');
    });
   }

There is a view with one line (named mailtest.blade.php):
Name: {{ $firstname }}

The compiler swears and says that it does not see the variable in the view. How to correctly pass a variable to a view?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2021-03-16
@Gavr_Gavr

artisan make:mailwill generate a Mailable class and template for you.
All public fields of this class will be available in the template as variables.
To send, just call

Mail::to($user->email)
  ->send(new RegistrationMail($user));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question