使用请求输入在laravel 5.4中发送邮件

时间:2017-02-26 05:03:56

标签: php laravel laravel-5.4

我有一个联系表单,其中包含名称,电子邮件,电话等字段。我想将字段传递给发送的电子邮件,我在下面尝试的示例似乎并没有将变量发送到发送出去的html电子邮件中。

这是在本页的Route ::帖子中:

  

$ data = [           '名称' => $请求 - >输入('名称&#39),           '电子邮件' => $请求 - >输入('电子邮件&#39),           '电话' => $请求 - >输入('电话&#39),           '消息' => $请求 - >输入('消息')         ];

  Mail::to('info@getkutt.com')->send(new ContactForm($data));

然后在构建方法中的邮件文件夹下的contactform.php中有

public function build()
{
    return $this->from('no-reply@domain.com')->view('emails.app.contact-form-submission');
}

4 个答案:

答案 0 :(得分:3)

给出的答案对我来说太复杂了。下面是一个使用表单中nameemailbody字段的简单示例。把它放在你的控制器方法中:

    Mail::send('emails.contact', request()->all(), function ($message) {
        $message->subject('Website Contact');
        $message->to(config('mail.from.address'));
        $message->replyTo(request()->input('email'));
    });

继承emails/contact视图文件:

Name: {{ $name }}
Email: {{ $email }}
Message:

{{ $body }}

答案 1 :(得分:2)

这是laravel docs的示例     

namespace App\Mail;

use App\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class YOURCLASSNAME extends Mailable
{
    use Queueable, SerializesModels;

    //Here you are going to define the variables 
    //which you are pass to your view
    public $name;
    public $email;
    public $phone;
    public $message;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(Order $order)
    {
        //Now in the construct, you receive the input from request
        $this->name = $request->input('name');
        $this->email = $request->input('email');
        $this->phone = $request->input('phone');
        $this->message = $request->input('message');
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        //And now you return the input to your view
        return $this->view('emails.app.contact-form-submission')
                ->with([
                    'name' => $this->name,
                    'email' => $this->email,
                    'phone' => $this->phone,
                    'message' => $this->message,
                ]);
     }
}

之后,您可以在刀片中使用with的任何变量

The name: {{ $name }} and email: {{ $email }}

答案 2 :(得分:0)

在ContactForm mailabel 类中创建公共属性,并使用constuctor从控制器接收数据到该类。

例如 -

public $name;

 public function __construct($data)
    {
        $this->name= $data.name;
    }

答案 3 :(得分:0)

使用公共属性(https://laravel.com/docs/5.6/mail#writing-mailables)的Laravel 5.6:

可邮寄类,例如“ CallBack.php”:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class CallBack extends Mailable
{
    use Queueable, SerializesModels;

    public $name;
    public $phone;
    public $body;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($name, $phone, $body)
    {
        $this->name = $name;
        $this->phone = $phone;
        $this->body = $body;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.callback')->subject('mail subject');
    }
}

控制器:

    Mail::to('admin@email.com')
        ->send(new CallBack($request->input('name'), $request->input('phone'), $request->input('body')));

callback.blade.php:

<h1>Laravel Email</h1>

<p>name: {{ $name }}</p>

<p>phone: {{ $phone }}</p>

<p>body: {{ $body }}</p>