vendredi 9 mars 2018

How to access into Mail data from MessageSent event on Laravel?

I'm using laravel 5.5, and I'm trying to send emails with an image that is the sign of the client. To make the image accessible from the views I'm copying it into public folder and queued emails will access to it.

With a single action I can send multiple emails to the client, with sign into email, and pdf like the email attached, with sign image too. Then, the same image can be called multiple times from different emails. Is for this that I copy one image with a codified name for each email and passing the name of the image to the Mailable.

The problem is to make a sign of client public with a limited time. Then I'm trying to make listener for Illuminate\Mail\Events\MessageSent event that deletes the image of public folder getting the image name from the event... but I can't access to it.

  • How can I access to data of mailable from the event?
  • Do you know a better way to do this?

Thanks in advance.

Mailable class

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'
    ]
];

Listener

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



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2IdNvw7
via IFTTT

Aucun commentaire:

Enregistrer un commentaire