1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Ninja/Mailers/Mailer.php

147 lines
4.3 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-03-16 22:45:25 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Ninja\Mailers;
use App\Models\Invoice;
2015-07-02 22:21:29 +02:00
use Exception;
2015-03-16 22:45:25 +01:00
use Mail;
2017-01-30 20:40:43 +01:00
use Utils;
2015-03-16 22:45:25 +01:00
/**
2017-01-30 20:40:43 +01:00
* Class Mailer.
*/
2015-03-16 22:45:25 +01:00
class Mailer
{
/**
* @param $toEmail
* @param $fromEmail
* @param $fromName
* @param $subject
* @param $view
* @param array $data
2017-01-30 20:40:43 +01:00
*
* @return bool|string
*/
2015-03-16 22:45:25 +01:00
public function sendTo($toEmail, $fromEmail, $fromName, $subject, $view, $data = [])
{
2016-01-11 12:07:41 +01:00
// don't send emails to dummy addresses
2015-10-20 19:12:34 +02:00
if (stristr($toEmail, '@example.com')) {
return true;
}
2016-11-06 14:21:09 +01:00
2015-10-11 16:41:09 +02:00
if (isset($_ENV['POSTMARK_API_TOKEN'])) {
2015-10-09 06:42:52 +02:00
$views = 'emails.'.$view.'_html';
} else {
$views = [
'emails.'.$view.'_html',
'emails.'.$view.'_text',
];
}
2015-03-16 22:45:25 +01:00
2015-07-02 22:21:29 +02:00
try {
2015-10-11 16:41:09 +02:00
$response = Mail::send($views, $data, function ($message) use ($toEmail, $fromEmail, $fromName, $subject, $data) {
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;
2017-01-29 08:06:10 +01:00
//\Log::info("{$toEmail} | {$replyEmail} | $fromEmail");
2015-07-07 22:08:16 +02:00
// Optionally send for alternate domain
2017-01-30 20:40:43 +01:00
if (! empty($data['fromEmail'])) {
$fromEmail = $data['fromEmail'];
}
2015-09-17 21:01:06 +02:00
$message->to($toEmail)
->from($fromEmail, $fromName)
->replyTo($replyEmail, $fromName)
->subject($subject);
2017-01-06 14:24:53 +01:00
// Optionally BCC the email
2017-01-30 20:40:43 +01:00
if (! empty($data['bccEmail'])) {
$message->bcc($data['bccEmail']);
2017-01-06 14:24:53 +01:00
}
2015-10-11 16:41:09 +02:00
// Attach the PDF to the email
2017-01-30 20:40:43 +01:00
if (! empty($data['pdfString']) && ! empty($data['pdfFileName'])) {
2015-10-13 09:11:44 +02:00
$message->attachData($data['pdfString'], $data['pdfFileName']);
}
2016-11-06 14:21:09 +01:00
// Attach documents to the email
2017-01-30 20:40:43 +01:00
if (! empty($data['documents'])) {
2017-01-30 17:05:31 +01:00
foreach ($data['documents'] as $document) {
$message->attachData($document['data'], $document['name']);
}
}
2015-07-02 22:21:29 +02:00
});
2015-10-11 16:41:09 +02:00
return $this->handleSuccess($response, $data);
2015-07-12 21:43:45 +02:00
} catch (Exception $exception) {
2015-10-11 16:41:09 +02:00
return $this->handleFailure($exception);
}
}
/**
* @param $response
* @param $data
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2015-10-11 16:41:09 +02:00
private function handleSuccess($response, $data)
{
if (isset($data['invitation'])) {
$invitation = $data['invitation'];
2015-10-29 15:42:05 +01:00
$invoice = $invitation->invoice;
$messageId = false;
2015-10-11 16:41:09 +02:00
// Track the Postmark message id
2015-10-13 09:11:44 +02:00
if (isset($_ENV['POSTMARK_API_TOKEN']) && $response) {
2016-01-13 19:06:01 +01:00
$json = json_decode((string) $response->getBody());
$messageId = $json->MessageID;
2015-07-12 21:43:45 +02:00
}
2015-10-29 15:42:05 +01:00
2017-01-30 20:40:43 +01:00
$notes = isset($data['notes']) ? $data['notes'] : false;
2017-01-05 11:46:03 +01:00
$invoice->markInvitationSent($invitation, $messageId, true, $notes);
2015-07-02 22:21:29 +02:00
}
2016-11-06 14:21:09 +01:00
2015-10-11 16:41:09 +02:00
return true;
}
/**
* @param $exception
2017-01-30 20:40:43 +01:00
*
* @return string
*/
2015-10-11 16:41:09 +02:00
private function handleFailure($exception)
{
2015-12-02 14:26:06 +01:00
if (isset($_ENV['POSTMARK_API_TOKEN']) && method_exists($exception, 'getResponse')) {
2016-11-27 15:58:32 +01:00
$response = $exception->getResponse();
if (! $response) {
$error = trans('texts.postmark_error', ['link' => link_to('https://status.postmarkapp.com/')]);
Utils::logError($error);
2017-01-12 14:12:02 +01:00
2017-02-09 12:08:48 +01:00
if (config('queue.default') === 'sync') {
return $error;
} else {
throw $exception;
}
2016-11-27 15:58:32 +01:00
}
$response = $response->getBody()->getContents();
2015-10-11 16:41:09 +02:00
$response = json_decode($response);
$emailError = nl2br($response->Message);
} else {
$emailError = $exception->getMessage();
}
2016-11-06 14:21:09 +01:00
2015-10-11 16:41:09 +02:00
if (isset($data['invitation'])) {
$invitation = $data['invitation'];
$invitation->email_error = $emailError;
$invitation->save();
2017-01-30 17:05:31 +01:00
} elseif (! Utils::isNinjaProd()) {
2016-11-06 14:21:09 +01:00
Utils::logError(Utils::getErrorString($exception));
2015-10-11 16:41:09 +02:00
}
return $emailError;
2015-03-16 22:45:25 +01:00
}
}