【发布时间】: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