Laravel 5: Error in Sending Email - php

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>

Related

Laravel mail - no message body [duplicate]

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

Laravel - controller doesn't see image input

My form:
<form action="{{route('settings.update')}}" method="POST">
#csrf
<div class="col-sm-3">
<div class="form-group">
<label for="avatar">Upload a new one:</label>
<input type="file" id="pic" name="pic"/>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Upload avatar</button>
</div>
</form>
My Controller:
public function update_settings($request)
{
$this->validate($request, [
'pic' => 'required|image'
]);
$path = $request->image->store('/img/');
Auth::user()->image = $path;
return view('pages.settings');
}
The error:
Too few arguments to function App\Http\Controllers\PagesController::update_settings(), 0 passed and exactly 1 expected
I am passing only the image file in the $request, but for some reason the controller doesn't see it, what am I doing incorrectly?
To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request class on your controller constructor or method. The current request instance will automatically be injected by the service container:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class UserController extends Controller
{
/**
* Store a new user.
*
* #param Request $request
* #return Response
*/
public function store(Request $request)
{
$name = $request->input('name');
//
}
}
Look well that in the method I am injecting the dependence of Illuminate\Http\Request
You need to add enctype='multipart/form-data' to your opening form tag
<form action="{{route('settings.update')}}" method="POST" enctype="multipart/form-data">

Laravel send email on form submit

So I have a simple form and I want whenever a user submits the form it to email me, not sure what the problem is as whenever I submit the form I'm getting a 500 error and I'm not sure why.
Here is the form:
<div class="contact" id="contact">
<form action="{{url('/contact')}}" method="post">
{{ csrf_field() }}
<input type="text" class="fname" name="firstname" placeholder="First Name">
<input type="text" class="lname" name="lastname" placeholder="Last Name">
<input type="text" class="address" name="address" placeholder="Address">
<input type="text" class="email" name="email" placeholder="Email">
<textarea id="subject" name="message" placeholder="Message" style="height:200px"></textarea>
<label class="checkbox-label">
<input type="checkbox" class="yard" name="yard"> I Want a Yard Sign
</label>
<br>
<label class="checkbox-label">
<input type="checkbox" class="host" name="host"> Host a Meet and Greet
</label>
<br>
<input type="submit" value="Get More Information">
<br>
</form>
</div>
And in my web routes file I have my two routes setup
Route::get('/contact', function () {
return view('contact');
});
Route::post('/contact', function (Request $request) {
Mail::send(new ContactMail($request));
return redirect('/');
});
I created my Mail file
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ContactMail extends Mailable
{
use Queueable, SerializesModels;
public $email;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct()
{
$this->email = $request;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject('New Contact Mail')
->from($this->email->email, $this->email->firstname)
->to('test#gmail.com')
->view('email.contactmail');
}
}
And then my simple blade file
{{ $email->content }}
Whenever I submit the form or try and call the route I get a 500 error and I'm not sure why.
I think you forgot to insert Request $request as a parameter on the class constructor and import the Request class.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Http\Request;
class ContactMail extends Mailable
{
use Queueable, SerializesModels;
public $email;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct(Request $request)
{
$this->email = $request;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject('New Contact Mail')
->from($this->email->email, $this->email->firstname)
->to('test#gmail.com')
->view('email.contactmail');
}
}
Also, as per your description, you may not have debug on. Change it at .env file:
APP_DEBUG=TRUE

Send email in laravel by using mailtrap

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.');
}

laravel validator not show errors

I am new to Laravel and other PHP frameworks.
Try simple form and validating, like examples in https://laravel.com/docs/5.1/validation
routes.php
Route::get('/post/', 'PostController#create');
Route::post('/post/store', 'PostController#store');
create.blade.php
<html>
<head>
<title>Post form</title>
</head>
<body>
<h1>Create Post</h1>
<form action="/post/store" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="title">Title</label>
<input type="text" id="title" name='title'>
</div>
<button type="submit" class="btn btn-default">Save</button>
</form>
</body>
PostController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use View;
use Validator;
class PostController extends Controller
{
/**
* Show the form to create a new blog post.
*
* #return Response
*/
public function create()
{
return view('post.create');
}
/**
* Store a new blog post.
*
* #param Request $request
* #return Response
*/
public function store(Request $request)
{
// Validate and store the blog post...
$validator = Validator::make($request->all(), [
'title' => 'required|min:5'
]);
if ($validator->fails()) {
dd($validator->errors);
//return redirect('post')
//->withErrors($validator)
//->withInput();
}
}
}
When I post not valid data:
ErrorException in PostController.php line 37: Undefined property: Illuminate\Validation\Validator::$errors
Validator object nor have errors.
If enabled in controller
return redirect('post')->withErrors($validator)
->withInput();
and enabled in form
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Have error
ErrorException in c5df03aa6445eda15ddf9d4b3d08e7882dfe13e1.php line 1: Undefined variable: errors (View: /www/alexey-laravel-1/resources/views/post/create.blade.php)
This error in default get request to form and after redirect from validator.
For $errors to be available in the view, the related routes must be within the web middleware:
Route::group(['middleware' => ['web']], function () {
Route::get('/post/', 'PostController#create');
Route::post('/post/store', 'PostController#store');
});

Categories