1
0
mirror of https://github.com/freescout-helpdesk/freescout.git synced 2024-11-25 11:52:29 +01:00
freescout/app/Mail/AutoReply.php
2018-11-12 09:12:57 +00:00

93 lines
2.2 KiB
PHP

<?php
namespace App\Mail;
use Illuminate\Mail\Mailable;
class AutoReply extends Mailable
{
/**
* Conversation created by customer.
*/
public $conversation;
/**
* Mailbox.
*/
public $mailbox;
/**
* Customer.
*/
public $customer;
/**
* Custom headers.
*/
public $headers = [];
/**
* Create a new message instance.
*/
public function __construct($conversation, $mailbox, $customer, $headers)
{
$this->conversation = $conversation;
$this->mailbox = $mailbox;
$this->customer = $customer;
$this->headers = $headers;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$view_params = [];
// Set headers
$this->setHeaders();
$data = [
'mailbox' => $this->mailbox,
'conversation' => $this->conversation,
'customer' => $this->customer,
];
// Set variables
$subject = \App\Misc\Mail::replaceMailVars($this->mailbox->auto_reply_subject, $data);
$view_params['auto_reply_message'] = \App\Misc\Mail::replaceMailVars($this->mailbox->auto_reply_message, $data);
$message = $this->subject($subject)
->view('emails/customer/auto_reply', $view_params)
->text('emails/customer/auto_reply_text', $view_params);
return $message;
}
/**
* Set headers.
* Settings via $this->addCustomHeaders does not work.
*/
public function setHeaders()
{
$new_headers = $this->headers;
if (!empty($new_headers)) {
$this->withSwiftMessage(function ($swiftmessage) use ($new_headers) {
if (!empty($new_headers['Message-ID'])) {
$swiftmessage->setId($new_headers['Message-ID']);
}
$headers = $swiftmessage->getHeaders();
foreach ($new_headers as $header => $value) {
if ($header != 'Message-ID') {
$headers->addTextHeader($header, $value);
}
}
return $swiftmessage;
});
}
}
}