I have a contact form like any other contact form (it has a field for a name, email, subject, and message).
I created a FormController.php in which I validate my from and make use of Mail to send my form.
I also generated a Mailable using php artisan make:mail FormMail --markdown=emails.form.data.
I hav the following problems:
- I want to pass the email someone enters into the ->from() method, but I get an error that says
Undefined variable. How can I pull that specific value so that when I receive an email it says it comes from the person who is trying to contact me? So far, I've been getting the default email provided in Laravel ([email protected]). I want the email to be the one a user enters. For instance [email protected]
- I cannot pass the subject into the ->subject() method. I get the same error as #1
Here is my FormController
<?php
namespace App\Http\Controllers;
use App\Mail\FormMail;
use App\Http\Requests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
class FormController extends Controller
{
public function getContacto() {
return view('pages.contacto');
}
public function postContacto(Request $request) {
$email = '[email protected]';
$rules = [
'name' => 'required|regex:/^[\pL\pM\p{Zs}.-]+$/u',
'email1' => 'required|email',
'email2' => 'required|email|same:email1',
'subject' => 'required|max:70',
'bodyMessage' => 'required|max:500'
];
$this->validate($request, $rules);
$name = $request->name;
$email1 = $request->email1;
$subject = $request->subject;
$bodyMessage = $request->bodyMessage;
Mail::to($email)->send(new FormMail($name, $email1, $subject, $bodyMessage));
return view('pages.sent');
}
}
Here is my mailable FormMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class FormMail extends Mailable
{
use Queueable, SerializesModels;
public $name;
public $email1;
public $subject;
public $bodyMessage;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($name, $email1, $subject, $bodyMessage)
{
$this->name = $name;
$this->email1 = $email1;
$this->subject = $subject;
$this->bodyMessage = $bodyMessage;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->with([
'name' => $this->name,
'subject' => $this->subject,
'bodyMessage' => $this->bodyMessage
])
->markdown('emails.form.data');
/*DOES NOT WORK
return $this->from($email1)->subject($subject)->with([
'name' => $this->name,
'email1' => $this->email1])
->markdown('emails.form.data');*/
}
}
Any help and error you see in my code will be highly appreciated. Thanks.