Hello this my project with laravel to send an email by using mailtrap this is my sendemail controller
<?php
namespace App\Http\Controllers;
use App\Model\Sendemail;
use Illuminate\Http\Request;
use Mail;
use App\Mail\TestStarted;
class SendemailController extends Controller
{
public function start(Request $request)
{
$send_email = Mail::to($request->email)->send(new TestStarted);
if ($send_email)
{
return redirect()->back()->with('success', 'Sens email
successfully.');
}
}
}
and this function to share the approval student into studentcontroller
public function shareapproval($uniid)
{
$approval = Student :: where ('uniid', $uniid)->firstOrFail();
return view('SendEmail.Request.share',compact('approval'));
}
and this TestStarted.php in Mail file
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestStarted extends Mailable
{
use Queueable, SerializesModels;
public function build()
{
return $this->view('SendEmail.Request.mail');
return redirect()->back()->with('success', 'Sens email successfully.');
}
}
this is in config.mail.php
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'testgp2@system.com'),//
'name' => env('MAIL_FROM_NAME', 'Example'),
],
and this is my form to write the instructor email to send the email
@extends('layouts.app')
@section('content')
<div class="container">
<form method="post" action="/sendemail">
@csrf
<h1> send email </h1>
<br>
<<div class="form-group">
<label for="email">write the instructor email</label><br>
<input type="text" id="email" name="email" class="form-control" >
</div>
<button type="submit" class="btn btn-primary">send </button><br>
</form>
</div>
@endsection
and this is the content of mail I want to send it this is mail.blade.php in (resources\views\SendEmail\Request\mail.blade.php)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-enguiv="X-UA-compatible" content="ie=edge">
<title>document </titel>
</head>
<body background-color: coral>
<h2> thank you for your order </h2>
</body
</html>
Finally, this is my route
Route::post('/student/share-approval/{uniid}',
'StudentController@shareapproval');
//SendEmail
Route::post('/sendemail','SendemailController@start');
Route::get('/start','SendemailController@start');
and I set up my.env with MAIL_USERNAME and MAIL_PASSWORD as shown in my account on mailtrap
Okay, let's start from the route. You're pointing the same method for the GET and POST request:
Route::post('/sendemail','SendemailController@start');
Route::get('/start','SendemailController@start');
As a result the mail field Mail::to($request->email)
is getting null. Which could be a reason behind failure. So try to use different methods for handling GET and POST requests instead of one.
Route::get('/start','SendemailController@start');
Route::post('/sendemail','SendemailController@sendMail');
Secondly, in the code below, you are returning twice. But in real life it will only execute the first one and ignore the second one.
public function build()
{
// this is executing
return $this->view('SendEmail.Request.mail');
// this is getting ingorned
return redirect()->back()->with('success', 'Sens email successfully.');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.