2016-07-14 22:37:04 +02: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;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* Class Mailer
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
class Mailer
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $toEmail
|
|
|
|
* @param $fromEmail
|
|
|
|
* @param $fromName
|
|
|
|
* @param $subject
|
|
|
|
* @param $view
|
|
|
|
* @param array $data
|
|
|
|
* @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
|
|
|
// check the username is set
|
|
|
|
if ( ! env('POSTMARK_API_TOKEN') && ! env('MAIL_USERNAME')) {
|
|
|
|
return trans('texts.invalid_mail_config');
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't send emails to dummy addresses
|
2015-10-20 19:12:34 +02:00
|
|
|
if (stristr($toEmail, '@example.com')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-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-10-11 16:41:09 +02:00
|
|
|
// Attach the PDF to the email
|
2015-10-13 09:11:44 +02:00
|
|
|
if (!empty($data['pdfString']) && !empty($data['pdfFileName'])) {
|
|
|
|
$message->attachData($data['pdfString'], $data['pdfFileName']);
|
2015-03-15 13:01:13 +01:00
|
|
|
}
|
2016-03-24 00:20:08 +01:00
|
|
|
|
|
|
|
// Attach documents to the email
|
|
|
|
if(!empty($data['documents'])){
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $response
|
|
|
|
* @param $data
|
|
|
|
* @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
|
|
|
|
|
|
|
$invoice->markInvitationSent($invitation, $messageId);
|
2015-07-02 22:21:29 +02:00
|
|
|
}
|
2015-10-11 16:41:09 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $exception
|
2016-07-14 22:37:04 +02:00
|
|
|
*
|
2016-07-03 18:11:58 +02: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')) {
|
2015-10-11 16:41:09 +02:00
|
|
|
$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;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|