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