to($toEmail) ->from($fromEmail, $fromName) ->replyTo($replyEmail, $fromName) ->subject($subject); // Attach the PDF to the email if (!empty($data['pdfString']) && !empty($data['pdfFileName'])) { $message->attachData($data['pdfString'], $data['pdfFileName']); } // Attach documents to the email if(!empty($data['documents'])){ foreach($data['documents'] as $document){ $message->attachData($document['data'], $document['name']); } } }); return $this->handleSuccess($response, $data); } catch (Exception $exception) { return $this->handleFailure($exception); } } /** * @param $response * @param $data * @return bool */ private function handleSuccess($response, $data) { if (isset($data['invitation'])) { $invitation = $data['invitation']; $invoice = $invitation->invoice; $messageId = false; // Track the Postmark message id if (isset($_ENV['POSTMARK_API_TOKEN']) && $response) { $json = json_decode((string) $response->getBody()); $messageId = $json->MessageID; } $invoice->markInvitationSent($invitation, $messageId); } return true; } /** * @param $exception * @return string */ private function handleFailure($exception) { if (isset($_ENV['POSTMARK_API_TOKEN']) && method_exists($exception, 'getResponse')) { $response = $exception->getResponse()->getBody()->getContents(); $response = json_decode($response); $emailError = nl2br($response->Message); } else { $emailError = $exception->getMessage(); } if (isset($data['invitation'])) { $invitation = $data['invitation']; $invitation->email_error = $emailError; $invitation->save(); } return $emailError; } }