2015-03-24 09:21:12 +01:00
|
|
|
<?php namespace App\Ninja\Mailers;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-07-02 22:21:29 +02:00
|
|
|
use Exception;
|
2015-03-16 22:45:25 +01:00
|
|
|
use Mail;
|
|
|
|
use Utils;
|
2015-04-06 08:58:47 +02:00
|
|
|
use App\Models\Invoice;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
class Mailer
|
|
|
|
{
|
|
|
|
public function sendTo($toEmail, $fromEmail, $fromName, $subject, $view, $data = [])
|
|
|
|
{
|
|
|
|
$views = [
|
|
|
|
'emails.'.$view.'_html',
|
|
|
|
'emails.'.$view.'_text',
|
|
|
|
];
|
|
|
|
|
2015-07-02 22:21:29 +02:00
|
|
|
try {
|
|
|
|
Mail::send($views, $data, function ($message) use ($toEmail, $fromEmail, $fromName, $subject, $data) {
|
2015-07-07 22:08:16 +02:00
|
|
|
|
2015-08-14 14:04:33 +02:00
|
|
|
$toEmail = strtolower($toEmail);
|
2015-07-02 22:21:29 +02:00
|
|
|
$replyEmail = $fromEmail;
|
2015-07-07 22:08:16 +02:00
|
|
|
$fromEmail = CONTACT_EMAIL;
|
|
|
|
|
2015-09-17 21:01:06 +02:00
|
|
|
$message->to($toEmail)
|
|
|
|
->from($fromEmail, $fromName)
|
|
|
|
->replyTo($replyEmail, $fromName)
|
|
|
|
->subject($subject);
|
|
|
|
|
2015-08-14 14:04:33 +02:00
|
|
|
if (isset($data['invoice_id'])) {
|
2015-09-17 21:01:06 +02:00
|
|
|
$invoice = Invoice::with('account')->where('id', '=', $data['invoice_id'])->first();
|
|
|
|
if ($invoice->account->pdf_email_attachment && file_exists($invoice->getPDFPath())) {
|
2015-07-02 22:21:29 +02:00
|
|
|
$message->attach(
|
|
|
|
$invoice->getPDFPath(),
|
|
|
|
array('as' => $invoice->getFileName(), 'mime' => 'application/pdf')
|
|
|
|
);
|
|
|
|
}
|
2015-03-15 13:01:13 +01:00
|
|
|
}
|
2015-07-02 22:21:29 +02:00
|
|
|
});
|
2015-07-07 22:08:16 +02:00
|
|
|
|
2015-07-02 22:21:29 +02:00
|
|
|
return true;
|
2015-07-12 21:43:45 +02:00
|
|
|
} catch (Exception $exception) {
|
2015-09-17 21:01:06 +02:00
|
|
|
Utils::logError('Email Error: ' . $exception->getMessage());
|
2015-08-13 07:45:48 +02:00
|
|
|
if (isset($_ENV['POSTMARK_API_TOKEN'])) {
|
2015-07-12 21:43:45 +02:00
|
|
|
$response = $exception->getResponse()->getBody()->getContents();
|
|
|
|
$response = json_decode($response);
|
|
|
|
return nl2br($response->Message);
|
|
|
|
} else {
|
|
|
|
return $exception->getMessage();
|
|
|
|
}
|
2015-07-02 22:21:29 +02:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|