mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-03 22:47:32 -05:00 
			
		
		
		
	Working on email failure notifications
This commit is contained in:
		
							parent
							
								
									827bb0c3c4
								
							
						
					
					
						commit
						cfbf6e8f4e
					
				
							
								
								
									
										99
									
								
								app/Jobs/Mail/EntityFailedSendMailer.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								app/Jobs/Mail/EntityFailedSendMailer.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,99 @@
 | 
			
		||||
<?php
 | 
			
		||||
/**
 | 
			
		||||
 * Invoice Ninja (https://invoiceninja.com).
 | 
			
		||||
 *
 | 
			
		||||
 * @link https://github.com/invoiceninja/invoiceninja source repository
 | 
			
		||||
 *
 | 
			
		||||
 * @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
 | 
			
		||||
 *
 | 
			
		||||
 * @license https://opensource.org/licenses/AAL
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace App\Jobs\Mail;
 | 
			
		||||
 | 
			
		||||
use App\Libraries\MultiDB;
 | 
			
		||||
use App\Mail\Admin\EntityNotificationMailer;
 | 
			
		||||
use App\Mail\Admin\EntitySentObject;
 | 
			
		||||
use Illuminate\Bus\Queueable;
 | 
			
		||||
use Illuminate\Contracts\Queue\ShouldQueue;
 | 
			
		||||
use Illuminate\Foundation\Bus\Dispatchable;
 | 
			
		||||
use Illuminate\Queue\InteractsWithQueue;
 | 
			
		||||
use Illuminate\Queue\SerializesModels;
 | 
			
		||||
use Illuminate\Support\Facades\Mail;
 | 
			
		||||
 | 
			
		||||
/*Multi Mailer implemented*/
 | 
			
		||||
 | 
			
		||||
class EntityFailedSendMailer extends BaseMailerJob implements ShouldQueue
 | 
			
		||||
{
 | 
			
		||||
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 | 
			
		||||
 | 
			
		||||
    public $company;
 | 
			
		||||
 | 
			
		||||
    public $user;
 | 
			
		||||
 | 
			
		||||
    public $invitation;
 | 
			
		||||
 | 
			
		||||
    public $entity_type;
 | 
			
		||||
 | 
			
		||||
    public $entity;
 | 
			
		||||
 | 
			
		||||
    public $settings;
 | 
			
		||||
 | 
			
		||||
    public $template;
 | 
			
		||||
    /**
 | 
			
		||||
     * Create a new job instance.
 | 
			
		||||
     *
 | 
			
		||||
     * @param $invitation
 | 
			
		||||
     * @param $entity_type
 | 
			
		||||
     * @param $user
 | 
			
		||||
     * @param $company
 | 
			
		||||
     */
 | 
			
		||||
    public function __construct($invitation, $entity_type, $user, $company, $template)
 | 
			
		||||
    {
 | 
			
		||||
        $this->company = $company;
 | 
			
		||||
 | 
			
		||||
        $this->user = $user;
 | 
			
		||||
 | 
			
		||||
        $this->invitation = $invitation;
 | 
			
		||||
 | 
			
		||||
        $this->entity = $invitation->{$entity_type};
 | 
			
		||||
 | 
			
		||||
        $this->entity_type = $entity_type;
 | 
			
		||||
 | 
			
		||||
        $this->settings = $invitation->contact->client->getMergedSettings();
 | 
			
		||||
    
 | 
			
		||||
        $this->template = $template;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Execute the job.
 | 
			
		||||
     *
 | 
			
		||||
     * @return void
 | 
			
		||||
     */
 | 
			
		||||
    public function handle()
 | 
			
		||||
    {
 | 
			
		||||
        nlog("entity sent mailer");
 | 
			
		||||
        
 | 
			
		||||
        /*If we are migrating data we don't want to fire these notification*/
 | 
			
		||||
        if ($this->company->is_disabled) {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        //Set DB
 | 
			
		||||
        MultiDB::setDb($this->company->db);
 | 
			
		||||
 | 
			
		||||
        //if we need to set an email driver do it now
 | 
			
		||||
        $this->setMailDriver();
 | 
			
		||||
 | 
			
		||||
        $mail_obj = (new EntitySentObject($this->invitation, $this->entity_type, $this->template))->build();
 | 
			
		||||
        $mail_obj->from = [config('mail.from.address'), config('mail.from.name')];
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            Mail::to($this->user->email)
 | 
			
		||||
                ->send(new EntityNotificationMailer($mail_obj));
 | 
			
		||||
        } catch (\Exception $e) {
 | 
			
		||||
            $this->failed($e);
 | 
			
		||||
            $this->logMailError($e->getMessage(), $this->entity->client);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										63
									
								
								app/Listeners/Invoice/InvoiceFailedEmailNotification.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								app/Listeners/Invoice/InvoiceFailedEmailNotification.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,63 @@
 | 
			
		||||
<?php
 | 
			
		||||
/**
 | 
			
		||||
 * Invoice Ninja (https://invoiceninja.com).
 | 
			
		||||
 *
 | 
			
		||||
 * @link https://github.com/invoiceninja/invoiceninja source repository
 | 
			
		||||
 *
 | 
			
		||||
 * @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
 | 
			
		||||
 *
 | 
			
		||||
 * @license https://opensource.org/licenses/AAL
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace App\Listeners\Invoice;
 | 
			
		||||
 | 
			
		||||
use App\Jobs\Mail\EntitySentMailer;
 | 
			
		||||
use App\Libraries\MultiDB;
 | 
			
		||||
use App\Notifications\Admin\EntitySentNotification;
 | 
			
		||||
use App\Utils\Traits\Notifications\UserNotifies;
 | 
			
		||||
use Illuminate\Contracts\Queue\ShouldQueue;
 | 
			
		||||
 | 
			
		||||
class InvoiceFailedEmailNotification implements ShouldQueue
 | 
			
		||||
{
 | 
			
		||||
    use UserNotifies;
 | 
			
		||||
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Handle the event.
 | 
			
		||||
     *
 | 
			
		||||
     * @param  object  $event
 | 
			
		||||
     * @return void
 | 
			
		||||
     */
 | 
			
		||||
    public function handle($event)
 | 
			
		||||
    {
 | 
			
		||||
        MultiDB::setDb($event->company->db);
 | 
			
		||||
 | 
			
		||||
        $first_notification_sent = true;
 | 
			
		||||
 | 
			
		||||
        $invoice = $event->invitation->invoice;
 | 
			
		||||
        $invoice->last_sent_date = now();
 | 
			
		||||
        $invoice->save();
 | 
			
		||||
 | 
			
		||||
        foreach ($event->invitation->company->company_users as $company_user) {
 | 
			
		||||
            $user = $company_user->user;
 | 
			
		||||
 | 
			
		||||
            $notification = new EntitySentNotification($event->invitation, 'invoice');
 | 
			
		||||
 | 
			
		||||
            $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent']);
 | 
			
		||||
 | 
			
		||||
            if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) {
 | 
			
		||||
                unset($methods[$key]);
 | 
			
		||||
 | 
			
		||||
                EntitySentMailer::dispatch($event->invitation, 'invoice', $user, $event->invitation->company, $event->template);
 | 
			
		||||
                $first_notification_sent = false;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $notification->method = $methods;
 | 
			
		||||
 | 
			
		||||
            $user->notify($notification);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										140
									
								
								app/Mail/Admin/EntityFailedSendObject.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										140
									
								
								app/Mail/Admin/EntityFailedSendObject.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,140 @@
 | 
			
		||||
<?php
 | 
			
		||||
/**
 | 
			
		||||
 * Invoice Ninja (https://invoiceninja.com).
 | 
			
		||||
 *
 | 
			
		||||
 * @link https://github.com/invoiceninja/invoiceninja source repository
 | 
			
		||||
 *
 | 
			
		||||
 * @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
 | 
			
		||||
 *
 | 
			
		||||
 * @license https://opensource.org/licenses/AAL
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
namespace App\Mail\Admin;
 | 
			
		||||
 | 
			
		||||
use App\Utils\Number;
 | 
			
		||||
use stdClass;
 | 
			
		||||
 | 
			
		||||
class EntityFailedSendObject
 | 
			
		||||
{
 | 
			
		||||
    public $invitation;
 | 
			
		||||
 | 
			
		||||
    public $entity_type;
 | 
			
		||||
 | 
			
		||||
    public $entity;
 | 
			
		||||
 | 
			
		||||
    public $contact;
 | 
			
		||||
 | 
			
		||||
    public $company;
 | 
			
		||||
 | 
			
		||||
    public $settings;
 | 
			
		||||
 | 
			
		||||
    public $template;
 | 
			
		||||
 | 
			
		||||
    private $template_subject;
 | 
			
		||||
 | 
			
		||||
    private $template_body;
 | 
			
		||||
 | 
			
		||||
    public function __construct($invitation, $entity_type, $template)
 | 
			
		||||
    {
 | 
			
		||||
        $this->invitation = $invitation;
 | 
			
		||||
        $this->entity_type = $entity_type;
 | 
			
		||||
        $this->entity = $invitation->{$entity_type};
 | 
			
		||||
        $this->contact = $invitation->contact;
 | 
			
		||||
        $this->company = $invitation->company;
 | 
			
		||||
        $this->template = $template;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function build()
 | 
			
		||||
    {
 | 
			
		||||
        $this->setTemplate();
 | 
			
		||||
 | 
			
		||||
        $mail_obj = new stdClass;
 | 
			
		||||
        $mail_obj->amount = $this->getAmount();
 | 
			
		||||
        $mail_obj->subject = $this->getSubject();
 | 
			
		||||
        $mail_obj->data = $this->getData();
 | 
			
		||||
        $mail_obj->markdown = 'email.admin.generic';
 | 
			
		||||
        $mail_obj->tag = $this->company->company_key;
 | 
			
		||||
 | 
			
		||||
        return $mail_obj;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function setTemplate()
 | 
			
		||||
    {
 | 
			
		||||
        // nlog($this->template);
 | 
			
		||||
 | 
			
		||||
        switch ($this->template) {
 | 
			
		||||
            case 'invoice':
 | 
			
		||||
                $this->template_subject = "texts.notification_invoice_bounced_subject";
 | 
			
		||||
                $this->template_body = "texts.notification_invoice_bounced";
 | 
			
		||||
                break;
 | 
			
		||||
            case 'reminder1':
 | 
			
		||||
                $this->template_subject = "texts.notification_invoice_reminder1_sent_subject";
 | 
			
		||||
                $this->template_body = "texts.notification_invoice_sent";
 | 
			
		||||
                break;
 | 
			
		||||
            case 'reminder2':
 | 
			
		||||
                $this->template_subject = "texts.notification_invoice_reminder2_sent_subject";
 | 
			
		||||
                $this->template_body = "texts.notification_invoice_sent";
 | 
			
		||||
                break;
 | 
			
		||||
            case 'reminder3':
 | 
			
		||||
                $this->template_subject = "texts.notification_invoice_reminder3_sent_subject";
 | 
			
		||||
                $this->template_body = "texts.notification_invoice_sent";
 | 
			
		||||
                break;
 | 
			
		||||
            case 'reminder_endless':
 | 
			
		||||
                $this->template_subject = "texts.notification_invoice_reminder_endless_sent_subject";
 | 
			
		||||
                $this->template_body = "texts.notification_invoice_sent";
 | 
			
		||||
                break;
 | 
			
		||||
            case 'quote':
 | 
			
		||||
                $this->template_subject = "texts.notification_quote_bounced_subject";
 | 
			
		||||
                $this->template_body = "texts.notification_quote_sent";
 | 
			
		||||
                break;
 | 
			
		||||
            case 'credit':
 | 
			
		||||
                $this->template_subject = "texts.notification_credit_bounced_subject";
 | 
			
		||||
                $this->template_body = "texts.notification_credit_bounced";
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                $this->template_subject = "texts.notification_invoice_sent_subject";
 | 
			
		||||
                $this->template_body = "texts.notification_invoice_sent";
 | 
			
		||||
                break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function getAmount()
 | 
			
		||||
    {
 | 
			
		||||
        return Number::formatMoney($this->entity->amount, $this->entity->client);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function getSubject()
 | 
			
		||||
    {
 | 
			
		||||
        return
 | 
			
		||||
            ctrans(
 | 
			
		||||
                $this->template_subject,
 | 
			
		||||
                [
 | 
			
		||||
                        'client' => $this->contact->present()->name(),
 | 
			
		||||
                        'invoice' => $this->entity->number,
 | 
			
		||||
                    ]
 | 
			
		||||
            );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function getData()
 | 
			
		||||
    {
 | 
			
		||||
        $settings = $this->entity->client->getMergedSettings();
 | 
			
		||||
 | 
			
		||||
        return [
 | 
			
		||||
            'title' => $this->getSubject(),
 | 
			
		||||
            'message' => ctrans(
 | 
			
		||||
                $this->template_body,
 | 
			
		||||
                [
 | 
			
		||||
                    'amount' => $this->getAmount(),
 | 
			
		||||
                    'client' => $this->contact->present()->name(),
 | 
			
		||||
                    'invoice' => $this->entity->number,
 | 
			
		||||
                ]
 | 
			
		||||
            ),
 | 
			
		||||
            'url' => $this->invitation->getAdminLink(),
 | 
			
		||||
            'button' => ctrans("texts.view_{$this->entity_type}"),
 | 
			
		||||
            'signature' => $settings->email_signature,
 | 
			
		||||
            'logo' => $this->company->present()->logo(),
 | 
			
		||||
            'settings' => $settings,
 | 
			
		||||
            'whitelabel' => $this->company->account->isPaid() ? true : false,
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -14,7 +14,7 @@ namespace App\Mail\Admin;
 | 
			
		||||
use App\Utils\Number;
 | 
			
		||||
use stdClass;
 | 
			
		||||
 | 
			
		||||
class EntitySentObject
 | 
			
		||||
class EntityFailedSendObject
 | 
			
		||||
{
 | 
			
		||||
    public $invitation;
 | 
			
		||||
 | 
			
		||||
@ -91,7 +91,6 @@ class EntitySentObject
 | 
			
		||||
                $this->template_subject = "texts.notification_credit_sent_subject";
 | 
			
		||||
                $this->template_body = "texts.notification_credit_sent";
 | 
			
		||||
                break;
 | 
			
		||||
 | 
			
		||||
            default:
 | 
			
		||||
                $this->template_subject = "texts.notification_invoice_sent_subject";
 | 
			
		||||
                $this->template_body = "texts.notification_invoice_sent";
 | 
			
		||||
 | 
			
		||||
@ -3371,4 +3371,6 @@ return [
 | 
			
		||||
    'required_payment_information_more' => 'To complete a payment we need more details about you.',
 | 
			
		||||
 | 
			
		||||
    'required_client_info_save_label' => 'We will save this, so you don\'t have to enter it next time.',
 | 
			
		||||
    'notification_credit_bounced' => 'We were unable to deliver Credit :invoice to :contact.',
 | 
			
		||||
    'notification_credit_bounced_subject' => 'Unable to deliver Credit :invoice',
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user