1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Merge pull request #4066 from turbo124/v2

Mailer fixes
This commit is contained in:
David Bomba 2020-09-14 15:14:57 +10:00 committed by GitHub
commit da80b50402
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 54 additions and 31 deletions

View File

@ -13,6 +13,7 @@ namespace App\Http\Controllers;
use App\Console\Commands\ImportMigrations;
use App\DataMapper\CompanySettings;
use App\Jobs\Mail\MailRouter;
use App\Jobs\Util\StartMigration;
use App\Mail\ExistingMigration;
use App\Models\Company;
@ -245,7 +246,7 @@ class MigrationController extends BaseController
if ($checks['same_keys'] && ! $checks['with_force']) {
info('Migrating: Same company keys, no force provided.');
Mail::to($user)->send(new ExistingMigration());
MailRouter::dispatch(new ExistingMigration(), $company, $user);
return response()->json([
'_id' => Str::uuid(),
@ -258,7 +259,7 @@ class MigrationController extends BaseController
if (! $checks['same_keys'] && $checks['existing_company'] && ! $checks['with_force']) {
info('Migrating: Different keys, existing company with the key without the force option.');
Mail::to($user)->send(new ExistingMigration());
MailRouter::dispatch(new ExistingMigration(), $company, $user);
return response()->json([
'_id' => Str::uuid(),

View File

@ -14,9 +14,11 @@ namespace App\Jobs\Credit;
use App\Events\Credit\CreditWasEmailed;
use App\Events\Credit\CreditWasEmailedAndFailed;
use App\Jobs\Mail\BaseMailerJob;
use App\Jobs\Mail\MailRouter;
use App\Jobs\Util\SystemLogger;
use App\Libraries\MultiDB;
use App\Mail\TemplateEmail;
use App\Models\Company;
use App\Models\Credit;
use App\Models\SystemLog;
use App\Utils\Ninja;
@ -38,16 +40,17 @@ class EmailCredit extends BaseMailerJob implements ShouldQueue
public $settings;
public $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Credit $credit)
public function __construct(Credit $credit, Company $company)
{
$this->credit = $credit;
$this->settings = $credit->client->getMergedSettings();
$this->company = $company;
}
/**
@ -59,30 +62,29 @@ class EmailCredit extends BaseMailerJob implements ShouldQueue
public function handle()
{
//todo - change runtime config of mail driver if necessary
MultiDB::setDb($this->company->db);
$this->settings = $this->credit->client->getMergedSettings();
$template_style = $this->credit->client->getSetting('email_style');
$this->setMailDriver();
$this->credit->invitations->each(function ($invitation) use ($template_style) {
if ($invitation->contact->send_email && $invitation->contact->email) {
if ($invitation->contact->send_email && $invitation->contact->email)
{
$message_array = $this->credit->getEmailData('', $invitation->contact);
$message_array['title'] = &$message_array['subject'];
$message_array['footer'] = 'Sent to '.$invitation->contact->present()->name();
//send message
Mail::to($invitation->contact->email, $invitation->contact->present()->name())
->send(new TemplateEmail($message_array, $template_style, $invitation->contact->user, $invitation->contact->client));
if (count(Mail::failures()) > 0) {
event(new CreditWasEmailedAndFailed($this->credit, $this->credit->company, Mail::failures(), Ninja::eventVars()));
return $this->logMailError(Mail::failures(), $this->credit->client);
}
MailRouter::dispatch(new TemplateEmail($message_array, $template_style, $invitation->contact->user, $invitation->contact->client), $invitation->company, $invitation->contact);
//fire any events
event(new CreditWasEmailed($this->credit, $this->company, Ninja::eventVars()));
}
});
}

View File

@ -44,11 +44,11 @@ class MailRouter extends BaseMailerJob implements ShouldQueue
public $to_user; //User or ClientContact
public $sending_method;
public $sending_method; //not sure if we even need this
public $settings;
public function __construct(Mailable $mailable, Company $company, $to_user, string $sending_method)
public function __construct(Mailable $mailable, Company $company, $to_user, $sending_method = null)
{
$this->mailable = $mailable;

View File

@ -61,6 +61,8 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue
$this->amount = $amount;
$this->company = $company;
$this->settings = $client->getMergedSettings();
}
@ -71,7 +73,6 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue
*/
public function handle()
{
info('entity payment failure mailer');
//Set DB
MultiDB::setDb($this->company->db);

View File

@ -7,6 +7,7 @@ use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Events\Payment\PaymentWasEmailed;
use App\Events\Payment\PaymentWasEmailedAndFailed;
use App\Helpers\Email\BuildEmail;
use App\Jobs\Mail\BaseMailerJob;
use App\Jobs\Utils\SystemLogger;
use App\Libraries\MultiDB;
use App\Mail\TemplateEmail;
@ -21,7 +22,7 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
class EmailPayment implements ShouldQueue
class EmailPayment extends BaseMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@ -52,6 +53,13 @@ class EmailPayment implements ShouldQueue
public function handle()
{
if ($this->contact->email) {
MultiDB::setDb($this->payment->company->db); //this may fail if we don't pass the serialized object with the company record
//todo fix!!
//if we need to set an email driver do it now
$this->setMailDriver();
Mail::to($this->contact->email, $this->contact->present()->name())
->send(new TemplateEmail($this->email_builder, $this->contact->user, $this->contact->customer));

View File

@ -6,6 +6,7 @@ use App\Events\Invoice\InvoiceWasEmailed;
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Events\Quote\QuoteWasEmailed;
use App\Events\Quote\QuoteWasEmailedAndFailed;
use App\Jobs\Mail\BaseMailerJob;
use App\Jobs\Utils\SystemLogger;
use App\Libraries\MultiDB;
use App\Mail\TemplateEmail;
@ -20,7 +21,7 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
class EmailQuote implements ShouldQueue
class EmailQuote extends BaseMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@ -28,15 +29,19 @@ class EmailQuote implements ShouldQueue
public $email_builder;
public $company;
public $settings;
/**
* EmailQuote constructor.
* @param BuildEmail $email_builder
* @param QuoteInvitation $quote_invitation
*/
public function __construct($email_builder, QuoteInvitation $quote_invitation)
public function __construct($email_builder, QuoteInvitation $quote_invitation, Company $company)
{
$this->quote_invitation = $quote_invitation;
$this->email_builder = $email_builder;
$this->company = $company;
}
/**
@ -47,6 +52,12 @@ class EmailQuote implements ShouldQueue
*/
public function handle()
{
MultiDB::setDb($this->company->db);
$this->settings = $this->quote_invitation->contact->client->getMergedSettings();
$this->setMailDriver();
Mail::to($this->quote_invitation->contact->email, $this->quote_invitation->contact->present()->name())
->send(
new TemplateEmail(

View File

@ -35,7 +35,7 @@ class EntityNotificationMailer extends Mailable
*/
public function build()
{
return $this->from($this->mail_obj->from[0], $this->mail_obj->from[1]) //todo
return $this->from($this->mail_obj->from[0], $this->mail_obj->from[1])
->subject($this->mail_obj->subject)
->markdown($this->mail_obj->markdown, $this->mail_obj->data)
->withSwiftMessage(function ($message) {

View File

@ -48,7 +48,7 @@ class SendEmail
if ($invitation->contact->send_email && $invitation->contact->email) {
$email_builder = (new QuoteEmail())->build($invitation, $this->reminder_template);
EmailQuote::dispatchNow($email_builder, $invitation);
EmailQuote::dispatchNow($email_builder, $invitation, $invitation->company);
}
});

View File

@ -83,8 +83,8 @@ class RecurringInvoiceTransformer extends EntityTransformer
'id' => $this->encodePrimaryKey($invoice->id),
'user_id' => $this->encodePrimaryKey($invoice->user_id),
'assigned_user_id' => $this->encodePrimaryKey($invoice->assigned_user_id),
'amount' => (float) $invoice->amount ?: '',
'balance' => (float) $invoice->balance ?: '',
'amount' => (float) $invoice->amount,
'balance' => (float) $invoice->balance,
'client_id' => (string) $invoice->client_id,
'vendor_id' => (string) $this->encodePrimaryKey($invoice->vendor_id),
'status_id' => (string) ($invoice->status_id ?: 1),
@ -94,7 +94,7 @@ class RecurringInvoiceTransformer extends EntityTransformer
'archived_at' => (int) $invoice->deleted_at,
'is_deleted' => (bool) $invoice->is_deleted,
'number' => $invoice->number ?: '',
'discount' => (float) $invoice->discount ?: '',
'discount' => (float) $invoice->discount,
'po_number' => $invoice->po_number ?: '',
'date' => $invoice->date ?: '',
'last_sent_date' => $invoice->last_sent_date ?: '',
@ -105,11 +105,11 @@ class RecurringInvoiceTransformer extends EntityTransformer
'private_notes' => $invoice->private_notes ?: '',
'uses_inclusive_taxes' => (bool) $invoice->uses_inclusive_taxes,
'tax_name1' => $invoice->tax_name1 ? $invoice->tax_name1 : '',
'tax_rate1' => (float) $invoice->tax_rate1 ?: '',
'tax_rate1' => (float) $invoice->tax_rate1,
'tax_name2' => $invoice->tax_name2 ? $invoice->tax_name2 : '',
'tax_rate2' => (float) $invoice->tax_rate2 ?: '',
'tax_rate2' => (float) $invoice->tax_rate2,
'tax_name3' => $invoice->tax_name3 ? $invoice->tax_name3 : '',
'tax_rate3' => (float) $invoice->tax_rate3 ?: '',
'tax_rate3' => (float) $invoice->tax_rate3,
'total_taxes' => (float) $invoice->total_taxes,
'is_amount_discount' => (bool) ($invoice->is_amount_discount ?: false),
'footer' => $invoice->footer ?: '',

View File

@ -71,8 +71,8 @@ class SystemHealth
'env_writable' => self::checkEnvWritable(),
//'mail' => self::testMailServer(),
'simple_db_check' => (bool) self::simpleDbCheck(),
//'npm_status' => self::checkNpm(),
//'node_status' => self::checkNode(),
'npm_status' => self::checkNpm(),
'node_status' => self::checkNode(),
];
}

View File

@ -367,7 +367,7 @@ trait MockAccountData
$recurring_invoice->next_send_date = Carbon::now()->addDays(10);
$recurring_invoice->status_id = RecurringInvoice::STATUS_ACTIVE;
$recurring_invoice->remaining_cycles = 2;
$recurring_invoice->next_send_date = Carbon::now();
$recurring_invoice->next_send_date = Carbon::now()->addDays(10);
$recurring_invoice->save();
$recurring_invoice->number = $this->getNextInvoiceNumber($this->invoice->client);