I don't have any errors but I did not receive any email from the form.
This is the view (reduced):
{!! Form::open(['route' => 'contact_store', 'class' => 'form', 'method' => 'POST', 'files' => false]) !!}
<input type="text" id="nombre" name="nombre" class="form-control">
<input type="text" id="email" name="email" class="form-control">
<textarea id="texto" name="texto" class="form-control"></textarea>
{!! Form::submit('enviar', ['class' =>'btn btn-primary']) !!}
{!! Form::close() !!}
The controller with the two methods:
namespace App\Http\Controllers\Web;
use App\Http\Requests\ContactRequest;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Mail;
class ContactController extends Controller
{
public function create()
{
return view('web.nosotros.escribenos.escribenos');
}
public function store(Request $request)
{
$data = array(
'nombre' => $request->nombre,
'email' => $request->email,
'texto' => $request->texto,
);
Mail::send('web.emails.contact', $data, function($message) use ($data){
$message->from($data['email']);
$message->to('[email protected]');
$message->subject('web contact');
});
return view('web.nosotros.escribenos.escribenos', compact('data'));
}
}
The view web/emails/contact is only the format of the email:
<h3>New message from the web!</h3>
<div>
{{$texto}}
</div>
<p>from: {{ $email }}</p>
The routes in routes/web.php
Route::get('escribenos', ['as' => 'contact', 'uses' => 'ContactController@create']);
Route::post('escribenos', ['as' => 'contact_store', 'uses' => 'ContactController@store']);
In config/mail.php I have:
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'mydomain.com'),
'port' => env('MAIL_PORT', 25),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
'name' => env('MAIL_FROM_NAME', 'webmaster'),
],
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
And finally in .env:
MAIL_DRIVER=smtp
MAIL_HOST=mydomain.com
MAIL_PORT=25
[email protected]
MAIL_PASSWORD=my_password
MAIL_ENCRYPTION=null
All seem to be well, I don´t have any error but I don´t receive any email either. Any idea? Thanks.
To test your emails you can utilize mailtrap.io and add the username/password etc into the .env settings.
Here is generally how I would set it out:
Controller:
use Illuminate\Support\Facades\Input;
public function sendEmail(Request $request)
{
$data = array(
'nombre' => $request->get('nombre'),
'email' => $request->get('email'),
'texto' => $request->get('texto')
);
// php artisan make:mail sendMessage - this needs to be ran in terminal / cmd
Mail::to(config('mail.from.address'))->send(new sendMessage($data));
}
SendMail.php:
private $data;
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$data = $this->data;
return $this->view('web.emails.contact', compact('data'))
->subject('Web Contact')
->from(Input::get('email'));
}
web/emails/contact.blade (basic example):
<p>
Hey there {{ config('mail.from.name') }}
</p>
<p>
You have an email from {{ $data['nombre'] }} - email address {{ $data['email'] }}
</p>
<p>
{!! $data['texto'] !!}
</p>
config('mail.from.name)
& config('mail.from.address)
can be adjusted within config/mail.php
or you can simply remove these and add your own name / email address.