【问题标题】:How to access Mail data from MessageSent event on Laravel?如何从 Laravel 上的 MessageSent 事件访问邮件数据?
【发布时间】:2018-08-17 20:05:35
【问题描述】:

我正在使用 laravel 5.5,并尝试发送带有客户标志图像的电子邮件。 为了使图像可以从视图中访问,我将其复制到 public 文件夹中,排队的电子邮件将访问它。

通过一个操作,我可以向客户发送多封电子邮件,其中包含登录电子邮件,以及附件中的 pdf 格式的电子邮件,以及签名图像。然后,可以从不同的电子邮件中多次调用相同的图像。为此,我为每封电子邮件复制一个带有编码名称的图像,并将图像的名称传递给 Mailable。

问题是在有限的时间内公开客户的标志。然后我正在尝试为 Illuminate\Mail\Events\MessageSent 事件创建侦听器,该事件删除公用文件夹的图像,从事件中获取图像名称......但我无法访问它。

  • 如何从活动中访问可邮寄的数据?
  • 您知道更好的方法吗?

提前致谢。

可邮寄类

class SEPA extends Mailable
{
    use Queueable, SerializesModels;

    public $client;

    /**
     * Create a new message instance.
     *
     * @param Client $client
     */
    public function __construct(Client $client)
    {
        $this->client = $client;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $date = Carbon::now();

        // Name codified
        $fileName = md5(microtime()).".png";

        // Making the image accessible from views
        Storage::copy("clients/{$this->client->id}/firma.png", "public/tmp/{$fileName}");
        $pdfName = "SEPA - {$this->client->name}{$this->client->cognom1}{$this->client->cognom2}.pdf";
        $dades = [
            'data'      => $date,
            'client'    => $this->client,
            'firma'     => $fileName
        ];

        // Generating PDF
        $pdf = PDF::loadView('pdfs.SEPA', $dades);
        if (!Storage::has("tmp/clients/{$this->client->id}")) Storage::makeDirectory("tmp/clients/{$this->client->id}");
        $pdf->save(storage_path()."/app/tmp/clients/{$this->client->id}/".$pdfName);

        return $this
            ->from(['address' => 'email@random.com'])
            ->view('emails.SEPA')
            ->with($dades)
            ->attach(storage_path()."/app/tmp/clients/{$this->client->id}/".$pdfName);
    }
}

EventServiceProvider.php

protected $listen = [
    'Illuminate\Mail\Events\MessageSent' => [
        'App\Listeners\DeleteTempResources'
    ]
];

监听器

public function handle(MessageSent $event)
    {
        // Trying to access on data message
        Log::info($event->message->firma);
    }

【问题讨论】:

    标签: php email variables laravel-5


    【解决方案1】:

    您可以通过withSwiftMessage() 方法将您需要从事件访问的附加数据设置为实际 swiftMessage 上的额外字段,因为这是事件中可以访问的内容,如$message

    我看到有人这样做了here,例如添加$user 对象:

    $this->withSwiftMessage(function ($message) {
        $message->user = $this->user; // any crazy field of your choosing
    });
    

    这对我来说似乎很不正统 - 添加这样的流氓字段。

    请注意,您不需要 use $user 对象将其放入闭包中,因为它可以通过 $this 在范围内使用,只要它是包含类的成员属性。

    要在消息离开队列时看到它,您可以在MessageSending 事件中Log::info('The user: ', [$event->message->user])

    我刚刚对此进行了测试,它可以工作(我在 5.5 上),但我自己还没有在代码中使用它,因为它看起来有点奇怪,添加了一个这样的流氓字段。我提到它,因为如果您对这种方法感到满意,它实际上可能会解决您的问题!如果有人知道一种不那么丑陋的方法,我会全神贯注......

    附:对于我自己的情况,我可能会考虑在闭包中添加$message->lara_user_id = $this->user->id,因为这似乎不太可能与任何东西发生冲突,并且可以在事件中方便地撤回。欢迎讨论!

    【讨论】:

      猜你喜欢
      • 2018-05-20
      • 2018-09-26
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 2011-12-28
      • 2016-12-26
      相关资源
      最近更新 更多