0

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:

  1. 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] screenshot
    1. 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.

1
  • Please add the code of your mail's view. Commented Aug 12, 2018 at 11:44

1 Answer 1

2

In your code, you're not passing the from and subject data correctly, the data is stored your class properties (you're using a local variable which is not defined).

You can pass the name in the 2nd argument in the from method, same goes with the subject method just pass the subject there. Also the you can pass in the with method the variables you want to use in your email template view. So your FormMail.php will become:

<?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->from($this->email1, $this->name)->subject($this->subject)->with([
        'name' => $this->name,
        'subject' => $this->subject,
        'bodyMessage' => $this->bodyMessage
        ])
        ->markdown('emails.form.data');
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Please accept my answer as best answer if you find it helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.