1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Attachments with GMail transport (#3726)

* Refactor mailers

* Fixes for mailer

* Fixes for approving a quote

* Refactor emailer

* Refactor emailer

* Attach files with GMail transport

* Attach files to GMail transport
This commit is contained in:
David Bomba 2020-05-20 16:59:29 +10:00 committed by GitHub
parent d5ae025df0
commit 8ab0238f3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 116 additions and 17 deletions

View File

@ -26,6 +26,7 @@ class CloneQuoteToInvoiceFactory
unset($quote_array['client']);
unset($quote_array['company']);
unset($quote_array['hashed_id']);
unset($quote_array['invoice_id']);
unset($quote_array['id']);
foreach($quote_array as $key => $value)

View File

@ -62,8 +62,14 @@ class GmailTransport extends Transport
$this->gmail->cc($message->getCc());
$this->gmail->bcc($message->getBcc());
\Log::error(print_r($message->getChildren(),1));
foreach($message->getChildren() as $child)
$this->gmail->attach($child); //todo this should 'just work'
$this->gmail->send();
$this->sendPerformed($message);
return $this->numberOfRecipients($message);

View File

@ -14,7 +14,7 @@ namespace App\Http\Controllers;
use App\Helpers\Email\InvoiceEmail;
use App\Http\Requests\Email\SendEmailRequest;
use App\Jobs\Invoice\EmailInvoice;
use App\Jobs\Mail\EntityMailer;
use App\Jobs\Mail\EntitySentMailer;
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Quote;
@ -122,7 +122,7 @@ class EmailController extends BaseController
$invitation->contact->notify((new SendGenericNotification($invitation, $entity_string, $subject, $body))->delay($when));
EntityMailer::dispatch($invitation, $entity_string, $entity_obj->user, $invitation->company);
EntitySentMailer::dispatch($invitation, $entity_string, $entity_obj->user, $invitation->company);
}

View File

@ -18,7 +18,7 @@ use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Mail;
class EntityMailer extends BaseMailerJob implements ShouldQueue
class EntitySentMailer extends BaseMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@ -56,6 +56,7 @@ class EntityMailer extends BaseMailerJob implements ShouldQueue
*/
public function handle()
{
info("entity sent mailer");
//Set DB
MultiDB::setDb($this->company->db);
@ -63,8 +64,8 @@ class EntityMailer extends BaseMailerJob implements ShouldQueue
$this->setMailDriver($this->entity->client->getSetting('email_sending_method'));
$mail_obj = (new EntitySentObject($this->invitation, $this->entity_type))->build();
$mail_obj->from = $this->entity->user->present()->name();
$mail_obj->from = [$this->entity->user->email, $this->entity->user->present()->name()];
//send email
Mail::to($this->user->email)
->send(new EntityNotificationMailer($mail_obj));

View File

@ -0,0 +1,94 @@
<?php
namespace App\Jobs\Mail;
use App\Jobs\Util\SystemLogger;
use App\Libraries\Google\Google;
use App\Libraries\MultiDB;
use App\Mail\Admin\EntityNotificationMailer;
use App\Mail\Admin\EntityViewedObject;
use App\Models\SystemLog;
use App\Models\User;
use App\Providers\MailServiceProvider;
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\Config;
use Illuminate\Support\Facades\Mail;
class EntityViewedMailer extends BaseMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $company;
public $user;
public $invitation;
public $entity_type;
public $entity;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($invitation, $entity_type, $user, $company)
{
$this->company = $company;
$this->user = $user;
$this->invitation = $invitation;
$this->entity = $invitation->{$entity_type};
$this->entity_type = $entity_type;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
info("entity viewed mailer");
//Set DB
MultiDB::setDb($this->company->db);
//if we need to set an email driver do it now
$this->setMailDriver($this->entity->client->getSetting('email_sending_method'));
$mail_obj = (new EntityViewedObject($this->invitation, $this->entity_type))->build();
$mail_obj->from = [$this->entity->user->email, $this->entity->user->present()->name()];
//send email
Mail::to($this->user->email)
->send(new EntityNotificationMailer($mail_obj));
//catch errors
if (count(Mail::failures()) > 0) {
$this->logMailError(Mail::failures());
}
}
private function logMailError($errors)
{
SystemLogger::dispatch(
$errors,
SystemLog::CATEGORY_MAIL,
SystemLog::EVENT_MAIL_SEND,
SystemLog::TYPE_FAILURE,
$this->invoice->client
);
}
}

View File

@ -11,6 +11,7 @@
namespace App\Listeners\Invoice;
use App\Jobs\Mail\EntitySentMailer;
use App\Models\Activity;
use App\Models\ClientContact;
use App\Models\InvoiceInvitation;
@ -57,7 +58,7 @@ class InvoiceEmailedNotification implements ShouldQueue
//This allows us better control of how we
//handle the mailer
EntityMailer::dispatch($invitation, 'invoice', $user, $invitation->company);
EntitySentMailer::dispatch($invitation, 'invoice', $user, $invitation->company);
}
$notification->method = $methods;

View File

@ -11,7 +11,7 @@
namespace App\Listeners\Misc;
use App\Jobs\Mail\EntityMailer;
use App\Jobs\Mail\EntityViewedMailer;
use App\Notifications\Admin\EntityViewedNotification;
use App\Utils\Traits\Notifications\UserNotifies;
use Illuminate\Contracts\Queue\ShouldQueue;
@ -53,11 +53,8 @@ class InvitationViewedListener implements ShouldQueue
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
//Fire mail notification here!!!
//This allows us better control of how we
//handle the mailer
EntityViewedMailer::dispatch($invitation, $entity_name, $company_user->user, $invitation->company);
EntityMailer::dispatch($invitation, 'invoice', $user, $invitation->company);
}
$notification->method = $methods;

View File

@ -36,9 +36,9 @@ class EntityNotificationMailer extends Mailable
*/
public function build()
{
return $this->from($this->mail_obj->from) //todo
return $this->from($this->mail_obj->from[0], $this->mail_obj->from[1]) //todo
->subject($this->mail_obj->subject)
->markdown($this->mail_obj->markdown, ['data' => $this->mail_obj->data])
->markdown($this->mail_obj->markdown, $this->mail_obj->data)
->withSwiftMessage(function ($message) {
$message->getHeaders()->addTextHeader('Tag', $this->mail_obj->tag);
});

View File

@ -71,7 +71,7 @@ class EntitySentObject
$settings = $this->entity->client->getMergedSettings();
$data = [
return [
'title' => $this->getSubject(),
'message' => ctrans(
"texts.notification_{$this->entity_type}_sent",
@ -87,6 +87,5 @@ class EntitySentObject
'logo' => $this->company->present()->logo(),
];
return $data;
}
}

View File

@ -30,7 +30,7 @@ trait UserNotifies
array_push($required_permissions, "all_user_notifications");
}
if (count(array_intersect($required_permissions, $notifications->email)) >=1) {
if (count(array_intersect($required_permissions, $notifications->email)) >=1 || count(array_intersect($required_permissions, "all_user_notifications")) >=1 || count(array_intersect($required_permissions, "all_notifications")) >=1) {
array_push($notifiable_methods, 'mail');
}
@ -54,7 +54,7 @@ trait UserNotifies
array_push($required_permissions, "all_user_notifications");
}
if (count(array_intersect($required_permissions, $notifications->email)) >=1) {
if (count(array_intersect($required_permissions, $notifications->email)) >=1 || count(array_intersect($required_permissions, "all_user_notifications")) >=1 || count(array_intersect($required_permissions, "all_notifications")) >=1) {
array_push($notifiable_methods, 'mail');
}