简体   繁体   中英

Laravel Mailables pass several variables to __construct() and Controller

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 (hello@example.com). I want the email to be the one a user enters. For instance emily@mail.com 截图
    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 = 'random@email.com';
        $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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM