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.');
}
Related
This question already has an answer here:
Laravel htmlspecialchars() error when sending email
(1 answer)
Closed 1 year ago.
ErrorException
htmlspecialchars() expects parameter 1 to be string, object given (View: F:\OWL\owl-technical\resources\views\emails\contact-mail.blade.php)
This error appears after I try to send a message from the form on the contact page!
Contact Form
<!-- ***** Contact Form Start ***** -->
<div class="col-lg-8 col-md-6 col-sm-12">
<form action="{{ route('contacts') }}/send" method="POST">
#csrf
<div class="contact-form">
<div class="row">
<div class="col-lg-6 col-md-12 col-sm-12">
<input type="text" name="name" id="name" placeholder="Name">
</div>
<div class="col-lg-6 col-md-12 col-sm-12">
<input name="email" id="email" type="email" placeholder="E-Mail">
</div>
<div class="col-lg-12">
<textarea name="message" id="message" placeholder="Your message"></textarea>
</div>
<div class="col-lg-12">
<button type="send">Send message</button>
</div>
</div>
</div>
</form>
</div>
<!-- ***** Contact Form End ***** -->
But if I add {{json_decode ($ name)}} in file
contact-mail
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
name: {{ $name}} <br>
email: {{ $email}} <br>
message : {{$message }} <br>
</body>
</html>
The names are encoded with something like / u042 / u043 (but it's clear here, I encoded the name using json) and so on.
But the message field remains empty when receiving a letter.
Laravel 7.0
Sending email from localhost does not work. He writes that he cannot send a message without an email, but I put all the fields with emails. Created everything with docs.laravel.
App\Http\Controller\MailSetting
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
use App\Mail\MailClass;
class MailSetting extends Controller
{
public function send_form(Request $request)
{
$name = $request->name;
$email = $request->email;
$message = $request->message;
Mail::to('test#mail.ru')->send(new MailClass($name, $email, $message));
}
}
App\Mail\MailClass
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class MailClass extends Mailable
{
use Queueable, SerializesModels;
protected $name;
protected $email;
protected $message;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($name, $email, $message)
{
$this->name = $name;
$this->email = $email;
$this->message = $message;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('emails.contact-mail')
->with([
'name' => $this->name,
'email' => $this->email,
'message' => $this->message,
])
->subject('New MESSAGE ');
}
}
I checked everything I could, but I never found an error
Change $message variable to another variable. Laravel automatically makes the $message variable available to all of your email templates
I am still new to Laravel, so I am trying to learn it from a certain website and try inserting data into database mysql by using form.
this is the view code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Form Tambah Data</h1>
<form action="/home/simpan" method="post">
{{ csrf_field() }}
Nama <input type="text" name="nama" required="required"><br/>
Umur <input type="number" name="umur" required="required"><br/>
Kota <input type="text" name="kota" required="required"><br/>
<input type="submit" value="Simpan Data">
</form>
</body>
</html>
and then this is the web.php code
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', 'HomeController#index');
Route::get('/profil', function (){
return view('profil');
});
Route::get('/home/tambah','HomeController#tambahData');
Route::post('/home/simpan','HomeController#simpan');
And this one is the controller codes
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class HomeController extends Controller
{
//
public function index(){
//mengambil data dari tabel siswa
$mahasiswa = DB::table('mahasiswa')->get();
//mengirim data ke view mahasiswa
return view('mahasiswa', ['mahasiswa' => $mahasiswa]);
}
public function tambahData(){
return view('form_data');
}
public function simpan(Request $request){
DB::table('mahasiswa')->insert([
'nama' => $request->nama,
'umur' => $request->umur,
'kota' => $request->kota
]);
return redirect('/home');
}
}
The table in database has 4 columns, "id_mahasiswa, nama, umur, kota" and I try to only insert the data to 3 columns. But then, it always shows this error.
"Field 'id_mahasiswa' doesn't have a default value"
Can anyone tell me the solution for this?
It appears possibly that your database column "id_mahasiswa" within your "mahasiswa" table has a NOT NULL constraint and no default value so when you are trying to insert your record without a value for "id_mahasiswa" your query fails.
If the above is true you need to either provide a value for the field or change the design of the table you are working with.
I reviewed you code. In your phpmyadmin change the "id_mahasiswa" column default value to null for example :)
I am trying to send test emails in my Laravel project, and am encountering the following error:
ErrorException in helpers.php line 532:
htmlspecialchars() expects parameter 1 to be string, object given (View: C:\...\resources\views\mail-test.blade.php)
I've been toying around with my code, following some guidelines/tutorials online the best I can, but I don't see what I'm doing wrong. Code snippets are as follows:
web.php
Route::post('/send-mail', 'MailController#send')->name('send-mail');
sample-page.blade.php
...
<div style="text-align: center;">
<form action="{{ route('send-mail') }}" method="post">
{{ csrf_field() }}
<input type="email" name="email" placeholder="Email Address">
<input type="text" name="message" placeholder="Insert Message Here.">
<button type="submit">Let's send an email!</button>
</form>
</div>
....
MailController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Mail\Mailer;
use App\Mail\SendMail;
class MailController extends Controller
{
public function send(Request $request, Mailer $mailer) {
$mailer
->to($request->input('email'))
->send(new SendMail($request->input('message')));
return back();
}
}
SendMail.php
...
use Queueable, SerializesModels;
public $message;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($message)
{
$this->message = $message;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('test#test.com')
->view('mail-test');
}
mail-test.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Email Test</title>
</head>
<body>
<h1>EMAIL TESTING</h1>
<p>{{ $message }}</p>
</body>
</html>
The $message variable is automatically passed into the view by Laravel, and it's an instance of the Illuminate/Mail/Message class. If you have a string of content you need to pass to the view, you should do that in the view() call. But you should rename it from $message to avoid conflict. I believe this may do it for you:
SendMail.php
return $this->from('test#test.com')
->view('mail-test', ['contentMessage' => $this->message]);
mail-test.blade.php
<body>
<h1>EMAIL TESTING</h1>
<p>{{ $contentMessage }}</p>
</body>
I want to save some data through dropdownlist .. after loading the page, database also fetched with the dropdown but it doesn't save after I clicked the save button. I think there is problem with Course migration table but I couldn't get it.
[Scenery is while taking courses student can take a class from the dropdown list.]
Here is my contoller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Course;
use App\TheClass;
use Redirect;
class courseController extends Controller
{
public function index()
{
$alldata=Course::all();
return view('course.index',compact('alldata'));
}
public function create()
{
$input=\App\TheClass::all();
return view('course.create',compact('input'));
}
public function store(Request $request)
{
$input = $request->all();
Course::create($input);
return redirect('course');
}
}
Here is my view page:
<html>
<head>
<title> Create Course </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" >
<h3> Create course </h3>
{!! Form::open(array('route' => 'course.store','class'=>'form-horizontal')) !!}
{!! Form::token(); !!}
<?php echo csrf_field(); ?>
<div class="form-group">
<label>Course Code</label>
<input type="text" name="course_code" class="form-control" placeholder="Code">
</div>
<div class="form-group">
<label>Course Title</label>
<input type="text" name="course_title" class="form-control" placeholder="Title">
</div>
<div class="form-group">
<label>Course Credit</label>
<input type="text" name="course_credit" class="form-control" placeholder="Credit">
</div>
<div class="form-group">
<label for="">Class</label>
<select class="form-control input-sm" name="class_id" >
#foreach($input as $row)
<option value="{{$row->class_id}}">{{$row->class_name}}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-default">Submit</button>
{!! Form::close() !!}
</div>
</body>
</html>
Course Table Migration:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCourseTable extends Migration
{
public function up()
{
Schema::create('courses', function (Blueprint $table) {
$table->increments('course_id');
$table->string('course_code',10);
$table->string('course_title',50);
$table->string('course_credit');
$table->integer('class_id')->unsigned();
$table->timestamps();
$table->foreign('class_id')->references('id')->on('classes');
});
}
public function down()
{
Schema::drop('courses');
}
}
The Class table Migration:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateClassTable extends Migration
{
public function up()
{
Schema::create('classes', function (Blueprint $table) {
$table->increments('id');
$table->string('class_name',10);
$table->timestamps();
});
}
public function down()
{
//
}
}
Change {{$row->class_id}} to {{$row->id}}
Because your classes table does not have class_id column.
First of all you need to add $fillable to your model as create
method uses Mass Assignment.
Secondly I dont see any category field in migrations. Your select
has name category so in database also should be category field.
Basically here you need to use One To Many
P.S. Don't have enough points for comments so answered.
I want to Edit my Database through Laravel Form. Edit do works but when i want to update the database it's showing the following Error.
MethodNotAllowedHttpException in RouteCollection.php line 219:
here is my Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Course;
class courseController extends Controller
{
public function index()
{
$alldata=Course::all();
return view('course.index',compact('alldata'));
}
public function create()
{
return view('course.create');
}
public function store(Request $request)
{
$input = $request->all();
Course::create($input);
return redirect('course');
}
public function show($id)
{
//
}
public function edit($id)
{
$course=Course::findOrFail($id);
return view('course.edit',compact('course'));
}
public function update(Request $request, $id)
{
$input = $request->all();
$data=Course::findOrFail($id);
$data->update($input);
return redirect('course');
}
public function destroy($id)
{
$data=Course::findOrFail($id);
$data->delete($input);
return redirect('course');
}
}
Here is my Edit Page:
<html>
<head>
<title> Update Course </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" >
<h3> Update course </h3>
{!! Form::open(array('route' =>['course.update',$course->course_id],'class'=>'form-horizontal')) !!}
{!! Form::token(); !!}
<?php echo csrf_field(); ?>
<div class="form-group">
<label >Course Code</label>
<input type="text" name="course_code" class="form-control" value="{{$course->course_code}}">
</div>
<div class="form-group">
<label >Course Title</label>
<input type="text" name="course_title" class="form-control" value="{{$course->course_title}}">
</div>
<div class="form-group">
<label>Course Credit</label>
<input type="text" name="course_credit" class="form-control" value="{{$course->course_credit}}">
</div>
<button type="submit" class="btn btn-default">Update</button>
{!! Form::close() !!}
</div>
</body>
</html>
Here is the route:
<?php
Route::resource('course','courseController');
Route::group(['middleware' => ['web']], function () {
});
If anyone can solve the problem.please help.
When you try to edit you need to add method type according this link.
Specifying different methods
You can use methods other than POST with your forms. Pass the 'method'
you want in the array argument. Valid methods are 'get', 'put',
'patch', 'post', or 'delete'.
So in your case you need to add 'method' => 'patch' to your Form::open..
So your final code in blade will look like this:
{!! Form::open([
'method' => 'PATCH',
'route' => ['course.update',$course->course_id],
'class'=>'form-horizontal'
]) !!}
Extra
I can see you are using php tags like <?php echo csrf_field(); ?>, I assume you know in Laravel you can use {{ csrf_field() }} which is equal, but since I do not have in depth knowledge about your code, so it is left to you.