2019-12-10 21:25:54 +01:00
|
|
|
<?php
|
|
|
|
namespace App\Mail;
|
|
|
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Mail\Mailable;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
|
|
class TemplateEmail extends Mailable
|
|
|
|
{
|
|
|
|
use Queueable, SerializesModels;
|
2020-02-15 10:01:15 +01:00
|
|
|
private $build_email; //the message array // ['body', 'footer', 'title', 'files']
|
2019-12-10 21:25:54 +01:00
|
|
|
private $user; //the user the email will be sent from
|
2020-02-15 10:01:15 +01:00
|
|
|
private $customer;
|
|
|
|
private $footer;
|
2019-12-10 21:25:54 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
public function __construct($build_email, $user, $customer)
|
2019-12-10 21:25:54 +01:00
|
|
|
{
|
2020-02-15 10:01:15 +01:00
|
|
|
$this->build_email = $build_email;
|
2019-12-16 12:34:38 +01:00
|
|
|
$this->user = $user; //this is inappropriate here, need to refactor 'user' in this context the 'user' could also be the 'system'
|
2020-02-15 10:01:15 +01:00
|
|
|
$this->customer = $customer;
|
2019-12-10 21:25:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the message.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function build()
|
|
|
|
{
|
|
|
|
/*Alter Run Time Mailer configuration (driver etc etc) to regenerate the Mailer Singleton*/
|
|
|
|
//if using a system level template
|
2020-02-15 10:01:15 +01:00
|
|
|
$template_name = 'email.template.' . $this->build_email->getTemplate();
|
2019-12-16 12:34:38 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
$settings = $this->customer->getMergedSettings();
|
|
|
|
\Log::error(print_r($settings, 1));
|
|
|
|
$company = $this->customer->account;
|
2019-12-16 12:34:38 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
$message = $this->from($this->user->email,
|
|
|
|
$this->user->present()->name())//todo this needs to be fixed to handle the hosted version
|
|
|
|
->subject($this->build_email->getSubject())
|
|
|
|
->text('email.template.plain', ['body' => $this->build_email->getBody(), 'footer' => $this->build_email->getFooter()])
|
2019-12-10 21:25:54 +01:00
|
|
|
->view($template_name, [
|
2020-02-15 10:01:15 +01:00
|
|
|
'body' => $this->build_email->getBody(),
|
|
|
|
'footer' => $this->build_email->getFooter(),
|
|
|
|
'title' => $this->build_email->getSubject(),
|
2019-12-16 12:34:38 +01:00
|
|
|
'settings' => $settings,
|
|
|
|
'company' => $company
|
2019-12-10 21:25:54 +01:00
|
|
|
]);
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
//conditionally attach files
|
|
|
|
if($settings->pdf_email_attachment !== false && !empty($this->build_email->getAttachments())){
|
2019-12-22 11:28:41 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
foreach($this->build_email->getAttachments() as $file)
|
|
|
|
$message->attach($file);
|
|
|
|
}
|
2019-12-22 11:28:41 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
return $message;
|
2019-12-10 21:25:54 +01:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|