1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-23 01:41:34 +02:00
invoiceninja/app/Ninja/Mailers/ContactMailer.php

356 lines
12 KiB
PHP
Raw Normal View History

<?php namespace App\Ninja\Mailers;
2015-03-16 22:45:25 +01:00
2016-03-02 14:36:42 +01:00
use Form;
2016-03-24 02:25:33 +01:00
use HTML;
2015-03-16 22:45:25 +01:00
use Utils;
2015-04-06 08:58:47 +02:00
use Event;
use URL;
2015-09-17 21:01:06 +02:00
use Auth;
2015-03-31 20:50:58 +02:00
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\Activity;
2015-06-10 22:55:23 +02:00
use App\Models\Gateway;
2015-10-28 20:22:07 +01:00
use App\Events\InvoiceWasEmailed;
use App\Events\QuoteWasEmailed;
2015-03-16 22:45:25 +01:00
class ContactMailer extends Mailer
{
2015-12-02 14:26:06 +01:00
public static $variableFields = [
'footer',
'account',
'dueDate',
'invoiceDate',
2015-12-02 14:26:06 +01:00
'client',
'amount',
'contact',
'firstName',
'invoice',
'quote',
'password',
2016-03-24 02:25:33 +01:00
'documents',
2015-12-02 14:26:06 +01:00
'viewLink',
'viewButton',
'paymentLink',
'paymentButton',
];
2015-10-13 09:11:44 +02:00
public function sendInvoice(Invoice $invoice, $reminder = false, $pdfString = false)
2015-03-16 22:45:25 +01:00
{
$invoice->load('invitations', 'client.language', 'account');
2015-03-16 22:45:25 +01:00
$entityType = $invoice->getEntityType();
$client = $invoice->client;
$account = $invoice->account;
$response = null;
2015-11-16 20:29:22 +01:00
if ($client->trashed()) {
2015-11-16 20:51:34 +01:00
return trans('texts.email_errors.inactive_client');
2015-11-16 20:29:22 +01:00
} elseif ($invoice->trashed()) {
2015-11-16 20:51:34 +01:00
return trans('texts.email_errors.inactive_invoice');
2015-09-29 12:21:57 +02:00
}
$account->loadLocalizationSettings($client);
2015-10-11 16:41:09 +02:00
$emailTemplate = $account->getEmailTemplate($reminder ?: $entityType);
$emailSubject = $account->getEmailSubject($reminder ?: $entityType);
2015-03-16 22:45:25 +01:00
2015-09-17 21:01:06 +02:00
$sent = false;
2015-10-13 09:11:44 +02:00
if ($account->attatchPDF() && !$pdfString) {
2016-02-17 20:13:55 +01:00
$pdfString = $invoice->getPDFString();
2015-10-13 09:11:44 +02:00
}
$documentStrings = array();
if ($account->document_email_attachment && $invoice->hasDocuments()) {
$documents = $invoice->documents;
foreach($invoice->expenses as $expense){
$documents = $documents->merge($expense->documents);
}
$documents = $documents->sortBy('size');
$size = 0;
$maxSize = MAX_EMAIL_DOCUMENTS_SIZE * 1000;
foreach($documents as $document){
$size += $document->size;
if($size > $maxSize)break;
$documentStrings[] = array(
'name' => $document->name,
'data' => $document->getRaw(),
);
}
}
2015-10-13 09:11:44 +02:00
2015-03-16 22:45:25 +01:00
foreach ($invoice->invitations as $invitation) {
$response = $this->sendInvitation($invitation, $invoice, $emailTemplate, $emailSubject, $pdfString, $documentStrings);
2015-11-16 20:51:34 +01:00
if ($response === true) {
2015-10-11 16:41:09 +02:00
$sent = true;
2015-03-16 22:45:25 +01:00
}
2015-10-11 16:41:09 +02:00
}
2015-09-17 21:01:06 +02:00
2015-10-11 16:41:09 +02:00
$account->loadLocalizationSettings();
2015-09-17 21:01:06 +02:00
2015-10-11 16:41:09 +02:00
if ($sent === true) {
2015-10-28 20:22:07 +01:00
if ($invoice->is_quote) {
event(new QuoteWasEmailed($invoice));
} else {
event(new InvoiceWasEmailed($invoice));
}
2015-10-11 16:41:09 +02:00
}
2015-11-16 20:51:34 +01:00
return $response;
2015-10-11 16:41:09 +02:00
}
private function sendInvitation($invitation, $invoice, $body, $subject, $pdfString, $documentStrings)
2015-10-11 16:41:09 +02:00
{
$client = $invoice->client;
$account = $invoice->account;
if (Auth::check()) {
$user = Auth::user();
} else {
$user = $invitation->user;
if ($invitation->user->trashed()) {
$user = $account->users()->orderBy('id')->first();
2015-03-16 22:45:25 +01:00
}
2015-10-11 16:41:09 +02:00
}
2015-03-16 22:45:25 +01:00
2015-11-16 20:51:34 +01:00
if (!$user->email || !$user->registered) {
return trans('texts.email_errors.user_unregistered');
} elseif (!$user->confirmed) {
return trans('texts.email_errors.user_unconfirmed');
} elseif (!$invitation->contact->email) {
return trans('texts.email_errors.invalid_contact_email');
} elseif ($invitation->contact->trashed()) {
return trans('texts.email_errors.inactive_contact');
2015-10-11 16:41:09 +02:00
}
2015-03-16 22:45:25 +01:00
2015-10-11 16:41:09 +02:00
$variables = [
'account' => $account,
'client' => $client,
'invitation' => $invitation,
'amount' => $invoice->getRequestedAmount()
];
if (empty($invitation->contact->password) && $account->isPro() && $account->enable_portal_password && $account->send_portal_password) {
// The contact needs a password
$variables['password'] = $password = $this->generatePassword();
$invitation->contact->password = bcrypt($password);
$invitation->contact->save();
}
2015-03-16 22:45:25 +01:00
2015-10-13 09:11:44 +02:00
$data = [
'body' => $this->processVariables($body, $variables),
'link' => $invitation->getLink(),
'entityType' => $invoice->getEntityType(),
'invoiceId' => $invoice->id,
'invitation' => $invitation,
2015-11-06 11:59:53 +01:00
'account' => $account,
2015-12-02 14:26:06 +01:00
'client' => $client,
2015-11-08 09:43:49 +01:00
'invoice' => $invoice,
'documents' => $documentStrings,
2015-10-13 09:11:44 +02:00
];
if ($account->attatchPDF()) {
$data['pdfString'] = $pdfString;
$data['pdfFileName'] = $invoice->getFileName();
}
2015-03-16 22:45:25 +01:00
2015-10-11 16:41:09 +02:00
$subject = $this->processVariables($subject, $variables);
$fromEmail = $user->email;
2015-12-02 14:26:06 +01:00
if ($account->getEmailDesignId() == EMAIL_DESIGN_PLAIN) {
2015-12-02 14:26:06 +01:00
$view = ENTITY_INVOICE;
} else {
$view = 'design' . ($account->getEmailDesignId() - 1);
2015-12-02 14:26:06 +01:00
}
2015-10-29 15:42:05 +01:00
2015-12-02 14:26:06 +01:00
$response = $this->sendTo($invitation->contact->email, $fromEmail, $account->getDisplayName(), $subject, $view, $data);
2015-07-02 22:21:29 +02:00
2015-10-11 16:41:09 +02:00
if ($response === true) {
return true;
} else {
2015-11-16 20:51:34 +01:00
return $response;
2015-03-16 22:45:25 +01:00
}
}
protected function generatePassword($length = 9)
{
$sets = array(
'abcdefghjkmnpqrstuvwxyz',
'ABCDEFGHJKMNPQRSTUVWXYZ',
'23456789',
);
$all = '';
$password = '';
foreach($sets as $set)
{
$password .= $set[array_rand(str_split($set))];
$all .= $set;
}
$all = str_split($all);
for($i = 0; $i < $length - count($sets); $i++)
$password .= $all[array_rand($all)];
$password = str_shuffle($password);
return $password;
}
2015-03-16 22:45:25 +01:00
public function sendPaymentConfirmation(Payment $payment)
{
$account = $payment->account;
$client = $payment->client;
$account->loadLocalizationSettings($client);
2015-03-16 22:45:25 +01:00
$invoice = $payment->invoice;
$accountName = $account->getDisplayName();
$emailTemplate = $account->getEmailTemplate(ENTITY_PAYMENT);
2015-09-17 21:01:06 +02:00
$emailSubject = $invoice->account->getEmailSubject(ENTITY_PAYMENT);
2015-03-16 22:45:25 +01:00
if ($payment->invitation) {
$user = $payment->invitation->user;
2015-06-15 19:37:46 +02:00
$contact = $payment->contact;
2015-09-17 21:01:06 +02:00
$invitation = $payment->invitation;
} else {
$user = $payment->user;
$contact = $client->contacts[0];
2015-09-17 21:01:06 +02:00
$invitation = $payment->invoice->invitations[0];
}
2015-09-17 21:01:06 +02:00
$variables = [
'account' => $account,
'client' => $client,
'invitation' => $invitation,
2015-11-06 11:59:53 +01:00
'amount' => $payment->amount,
2015-09-17 21:01:06 +02:00
];
$data = [
2015-11-06 11:59:53 +01:00
'body' => $this->processVariables($emailTemplate, $variables),
2015-12-16 12:49:26 +01:00
'link' => $invitation->getLink(),
'invoice' => $invoice,
'client' => $client,
2015-11-06 11:59:53 +01:00
'account' => $account,
2015-12-16 12:49:26 +01:00
'payment' => $payment,
'entityType' => ENTITY_INVOICE,
2015-09-17 21:01:06 +02:00
];
2015-10-13 09:11:44 +02:00
if ($account->attatchPDF()) {
2016-02-17 20:13:55 +01:00
$data['pdfString'] = $invoice->getPDFString();
2015-10-13 09:11:44 +02:00
$data['pdfFileName'] = $invoice->getFileName();
2015-09-17 21:01:06 +02:00
}
2015-09-07 11:07:55 +02:00
2015-10-13 09:11:44 +02:00
$subject = $this->processVariables($emailSubject, $variables);
$data['invoice_id'] = $payment->invoice->id;
if ($account->getEmailDesignId() == EMAIL_DESIGN_PLAIN) {
2015-12-31 11:23:39 +01:00
$view = 'payment_confirmation';
} else {
$view = 'design' . ($account->getEmailDesignId() - 1);
2015-12-31 11:23:39 +01:00
}
2015-06-15 19:37:46 +02:00
if ($user->email && $contact->email) {
$this->sendTo($contact->email, $user->email, $accountName, $subject, $view, $data);
}
$account->loadLocalizationSettings();
2015-03-16 22:45:25 +01:00
}
public function sendLicensePaymentConfirmation($name, $email, $amount, $license, $productId)
{
$view = 'license_confirmation';
$subject = trans('texts.payment_subject');
if ($productId == PRODUCT_ONE_CLICK_INSTALL) {
$license = "Softaculous install license: $license";
} elseif ($productId == PRODUCT_INVOICE_DESIGNS) {
$license = "Invoice designs license: $license";
} elseif ($productId == PRODUCT_WHITE_LABEL) {
$license = "White label license: $license";
}
$data = [
'client' => $name,
'amount' => Utils::formatMoney($amount, DEFAULT_CURRENCY, DEFAULT_COUNTRY),
2015-03-16 22:45:25 +01:00
'license' => $license
];
$this->sendTo($email, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data);
}
2015-09-17 21:01:06 +02:00
private function processVariables($template, $data)
{
2015-12-02 14:26:06 +01:00
$account = $data['account'];
$client = $data['client'];
$invitation = $data['invitation'];
$invoice = $invitation->invoice;
$passwordHTML = isset($data['password'])?'<p>'.trans('texts.password').': '.$data['password'].'<p>':false;
2016-03-24 02:25:33 +01:00
$documentsHTML = '';
2015-12-02 14:26:06 +01:00
if($account->isPro() && $invoice->hasDocuments()){
2016-03-24 02:25:33 +01:00
$documentsHTML .= trans('texts.email_documents_header').'<ul>';
foreach($invoice->documents as $document){
$documentsHTML .= '<li><a href="'.HTML::entities($document->getClientUrl($invitation)).'">'.HTML::entities($document->name).'</a></li>';
}
foreach($invoice->expenses as $expense){
foreach($expense->documents as $document){
$documentsHTML .= '<li><a href="'.HTML::entities($document->getClientUrl($invitation)).'">'.HTML::entities($document->name).'</a></li>';
}
}
2016-03-24 02:25:33 +01:00
$documentsHTML .= '</ul>';
}
2015-09-17 21:01:06 +02:00
$variables = [
2015-12-02 14:26:06 +01:00
'$footer' => $account->getEmailFooter(),
'$client' => $client->getDisplayName(),
'$account' => $account->getDisplayName(),
'$dueDate' => $account->formatDate($invoice->due_date),
'$invoiceDate' => $account->formatDate($invoice->invoice_date),
2015-12-02 14:26:06 +01:00
'$contact' => $invitation->contact->getDisplayName(),
'$firstName' => $invitation->contact->first_name,
'$amount' => $account->formatMoney($data['amount'], $client),
2015-12-02 14:26:06 +01:00
'$invoice' => $invoice->invoice_number,
'$quote' => $invoice->invoice_number,
'$link' => $invitation->getLink(),
'$password' => $passwordHTML,
'$viewLink' => $invitation->getLink().'$password',
'$viewButton' => Form::emailViewButton($invitation->getLink(), $invoice->getEntityType()).'$password',
'$paymentLink' => $invitation->getLink('payment').'$password',
'$paymentButton' => Form::emailPaymentButton($invitation->getLink('payment')).'$password',
2015-12-23 15:21:48 +01:00
'$customClient1' => $account->custom_client_label1,
'$customClient2' => $account->custom_client_label2,
'$customInvoice1' => $account->custom_invoice_text_label1,
'$customInvoice2' => $account->custom_invoice_text_label2,
2016-03-24 02:25:33 +01:00
'$documents' => $documentsHTML,
2015-09-17 21:01:06 +02:00
];
// Add variables for available payment types
2015-12-02 14:26:06 +01:00
foreach (Gateway::$paymentTypes as $type) {
$camelType = Gateway::getPaymentTypeName($type);
$type = Utils::toSnakeCase($camelType);
2016-03-09 10:15:53 +01:00
$variables["\${$camelType}Link"] = $invitation->getLink('payment') . "/{$type}";
2016-03-02 14:36:42 +01:00
$variables["\${$camelType}Button"] = Form::emailPaymentButton($invitation->getLink('payment') . "/{$type}");
2015-09-17 21:01:06 +02:00
}
$includesPasswordPlaceholder = strpos($template, '$password') !== false;
2015-09-17 21:01:06 +02:00
$str = str_replace(array_keys($variables), array_values($variables), $template);
if(!$includesPasswordPlaceholder && $passwordHTML){
$pos = strrpos($str, '$password');
if($pos !== false)
{
$str = substr_replace($str, $passwordHTML, $pos, 9/* length of "$password" */);
}
}
$str = str_replace('$password', '', $str);
2016-03-08 19:38:28 +01:00
$str = autolink($str, 100);
2015-09-17 21:01:06 +02:00
return $str;
}
2015-03-16 22:45:25 +01:00
}