duanliaogui4133
2016-09-13 22:56
浏览 52
已采纳

在Laravel 5.3 mailable视图中使用表单请求数据

Looking around I cant find much information on how to do this. Plenty of the same examples just explaining the basic implementation of the mailable class but thats it.

I am sending an email from a contact form and have a route set up for testing that my form posts to:

Route::post('/sendmail', function() {    
    Mail::to("my email.com")->send(new MyTestMail());
});

I then have my mailable class which I have passed a test variable as a test string to:

<?php

namespace App\Mail;

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

class MyTestMail extends Mailable
{
    use Queueable, SerializesModels;

    public $test;

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

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

What I wish to do is have access to my post values from the contact from and use them in the email view.

图片转代码服务由CSDN问答提供 功能建议

环顾四周我找不到有关如何执行此操作的详细信息。 很多相同的例子只是解释了mailable类的基本实现,但就是这样。

我从联系表单发送一封电子邮件,并设置了一条路由来测试我的表单帖子 to:

  Route :: post('/ sendmail',function(){
 Mail :: to(“my email.com”) - &gt; send(new  MyTestMail()); 
}); 
   
 
 

然后我的mailable类将测试变量作为测试字符串传递给:

 &lt;?php 
 
namespace App \ Mail; 
 
use Illuminate \ Bus \ Queueable; 
use Illuminate \ Mail \ Mailable; 
use Illuminate \ Queue \ SerializesModels; 
use  Illuminate \ Contracts \ Queue \ ShouldQueue; 
 
class MyTestMail扩展了Mailable 
 {
使用Queueable,SerializesModels; 
 
 public $ test; 
 
 / ** 
 *创建一个新的消息实例。  n * 
 * @return void 
 * / 
 public function __construct()
 {
 $ this-&gt; test =“test”; 
} 
 
 / ** 
 *构建 消息。
 * 
 * @return $ this 
 * / 
公共乐趣 ction build()
 {
返回$ this-&gt; view('emails.myTestMail'); 
} 
} 
   
 
 

我希望 要做的是可以从联系人访问我的帖子值,并在电子邮件视图中使用它们。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanfu5239 2016-09-14 00:11
    已采纳

    You can try passing the data you need to the constructor of that mailable.

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

    Now 'postdata' will be available to your view.

    Mail::to("my email.com")->send(new MyTestMail($request->all()))
    
    打赏 评论

相关推荐 更多相似问题