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.
Route\web.php
Route::get('contact', 'ContactsController@index')->name('contacts'); // ContactsPages
Route::post('contact/send', 'MailSetting@send_form'); // ContactsPages(FORM_FeedBack)
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 ');
}
}
.env_file
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=2d1*******3e1d
MAIL_PASSWORD=62e*******db9
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
Config\Mail.php
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailtrap.io'),
'port' => env('MAIL_PORT', 25),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME', ''),
'password' => env('MAIL_PASSWORD', ''),
],
cotacts.blade
<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>
email_view
Name : {{ json_encode($name,TRUE)}} <br>
Email : {{ json_encode($email,TRUE)}} <br>
Message: {{ json_encode($message,TRUE)}} <br>
After the error appeared for the first time, I decided that I could change the settings on Gmail, changed them, but there another error appears.
From Address
is required (A MUST) for sending emails - even when the host is set to mailtrap.io
It can be set globally for the app in the .env
file
MAIL_FROM_ADDRESS=some@value.com
Or it can be set/overridden per Mailable class in the build method
public function build()
{
return $this->view('emails.contact-mail')
->with([
'name' => $this->name,
'email' => $this->email,
'message' => $this->message,
])
->subject('New MESSAGE ')
->from('some@value.com');
}