1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 09:21:34 +02:00
invoiceninja/app/Ninja/Mailers/Mailer.php
Hillel Coren 478d435cf1 Bug fixes
2015-10-09 07:42:52 +03:00

58 lines
2.0 KiB
PHP

<?php namespace App\Ninja\Mailers;
use Exception;
use Mail;
use Utils;
use App\Models\Invoice;
class Mailer
{
public function sendTo($toEmail, $fromEmail, $fromName, $subject, $view, $data = [])
{
// https://github.com/wildbit/laravel-postmark-provider/issues/2
if (isset($data['invoice_id']) && isset($_ENV['POSTMARK_API_TOKEN'])) {
$views = 'emails.'.$view.'_html';
} else {
$views = [
'emails.'.$view.'_html',
'emails.'.$view.'_text',
];
}
try {
Mail::send($views, $data, function ($message) use ($toEmail, $fromEmail, $fromName, $subject, $data) {
$toEmail = strtolower($toEmail);
$replyEmail = $fromEmail;
$fromEmail = CONTACT_EMAIL;
$message->to($toEmail)
->from($fromEmail, $fromName)
->replyTo($replyEmail, $fromName)
->subject($subject);
if (isset($data['invoice_id'])) {
$invoice = Invoice::with('account')->where('id', '=', $data['invoice_id'])->first();
if ($invoice->account->pdf_email_attachment && file_exists($invoice->getPDFPath())) {
$message->attach(
$invoice->getPDFPath(),
array('as' => $invoice->getFileName(), 'mime' => 'application/pdf')
);
}
}
});
return true;
} catch (Exception $exception) {
Utils::logError('Email Error: ' . $exception->getMessage());
if (false && isset($_ENV['POSTMARK_API_TOKEN'])) {
$response = $exception->getResponse()->getBody()->getContents();
$response = json_decode($response);
return nl2br($response->Message);
} else {
return $exception->getMessage();
}
}
}
}