Merge pull request #4928 from turbo124/v5-develop

Gmail Multi Mailer
This commit is contained in:
David Bomba 2021-02-17 11:25:49 +11:00 committed by GitHub
commit 0985363280
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 132 additions and 25 deletions

View File

@ -12,6 +12,7 @@
namespace App\Helpers\Mail; namespace App\Helpers\Mail;
use App\Utils\TempFile; use App\Utils\TempFile;
use Dacastro4\LaravelGmail\Facade\LaravelGmail;
use Dacastro4\LaravelGmail\Services\Message\Mail; use Dacastro4\LaravelGmail\Services\Message\Mail;
use Illuminate\Mail\Transport\Transport; use Illuminate\Mail\Transport\Transport;
use Swift_Mime_SimpleMessage; use Swift_Mime_SimpleMessage;
@ -43,17 +44,21 @@ class GmailTransport extends Transport
{ {
/*We should nest the token in the message and then discard it as needed*/ /*We should nest the token in the message and then discard it as needed*/
$token = $message->getHeaders()->get('GmailToken'); $token = $message->getHeaders()->get('GmailToken')->getValue();
$user_id = $message->getHeaders()->get('UserId')->getValue();
LaravelGmail::setUserId($user_id);
nlog("gmail transporter token = {$token}"); nlog("gmail transporter token = {$token}");
$message->getHeaders()->remove('GmailToken'); $message->getHeaders()->remove('GmailToken');
$message->getHeaders()->remove('UserId');
nlog("inside gmail sender with token {$token}"); nlog("inside gmail sender with token {$token}");
$this->beforeSendPerformed($message); $this->beforeSendPerformed($message);
$this->gmail->using($this->token); $this->gmail->using($token);
$this->gmail->to($message->getTo()); $this->gmail->to($message->getTo());
$this->gmail->from($message->getFrom()); $this->gmail->from($message->getFrom());
$this->gmail->subject($message->getSubject()); $this->gmail->subject($message->getSubject());
@ -82,6 +87,7 @@ class GmailTransport extends Transport
$this->sendPerformed($message); $this->sendPerformed($message);
return $this->numberOfRecipients($message); return $this->numberOfRecipients($message);
} }
} }

View File

@ -38,7 +38,7 @@ use Illuminate\Support\Str;
/*Multi Mailer implemented*/ /*Multi Mailer implemented*/
class EmailEntity extends BaseMailerJob implements ShouldQueue class EmailEntity implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@ -105,9 +105,6 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue
/* Set DB */ /* Set DB */
MultiDB::setDB($this->company->db); MultiDB::setDB($this->company->db);
/* Set the correct mail driver */
$this->setMailDriver();
$nmo = new NinjaMailerObject; $nmo = new NinjaMailerObject;
$nmo->mailable = new TemplateEmail($this->email_entity_builder,$this->invitation->contact); $nmo->mailable = new TemplateEmail($this->email_entity_builder,$this->invitation->contact);
$nmo->company = $this->company; $nmo->company = $this->company;

View File

@ -83,10 +83,10 @@ class BaseMailerJob implements ShouldQueue
* just for this request. * just for this request.
*/ */
config(['mail.driver' => 'gmail']); // config(['mail.driver' => 'gmail']);
config(['services.gmail.token' => $user->oauth_user_token->access_token]); // config(['services.gmail.token' => $user->oauth_user_token->access_token]);
config(['mail.from.address' => $user->email]); // config(['mail.from.address' => $user->email]);
config(['mail.from.name' => $user->present()->name()]); // config(['mail.from.name' => $user->present()->name()]);
//(new MailServiceProvider(app()))->register(); //(new MailServiceProvider(app()))->register();

View File

@ -34,6 +34,7 @@ use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Mail;
use Turbo124\Beacon\Facades\LightLogs; use Turbo124\Beacon\Facades\LightLogs;
use Dacastro4\LaravelGmail\Facade\LaravelGmail;
/*Multi Mailer implemented*/ /*Multi Mailer implemented*/
@ -105,12 +106,14 @@ class NinjaMailerJob implements ShouldQueue
App::forgetInstance('translator'); App::forgetInstance('translator');
App::forgetInstance('mail.manager'); //singletons must be destroyed! App::forgetInstance('mail.manager'); //singletons must be destroyed!
App::forgetInstance('mailer'); App::forgetInstance('mailer');
App::forgetInstance('laravelgmail');
/* Inject custom translations if any exist */ /* Inject custom translations if any exist */
Lang::replace(Ninja::transformTranslations($this->nmo->settings)); Lang::replace(Ninja::transformTranslations($this->nmo->settings));
switch ($this->nmo->settings->email_sending_method) { switch ($this->nmo->settings->email_sending_method) {
case 'default': case 'default':
config(['mail.driver' => config('mail.default')]);
break; break;
case 'gmail': case 'gmail':
$this->setGmailMailer(); $this->setGmailMailer();
@ -118,10 +121,15 @@ class NinjaMailerJob implements ShouldQueue
default: default:
break; break;
} }
(new MailServiceProvider(app()))->register();
} }
private function setGmailMailer() private function setGmailMailer()
{ {
if(LaravelGmail::check())
LaravelGmail::logout();
$sending_user = $this->nmo->settings->gmail_sending_user_id; $sending_user = $this->nmo->settings->gmail_sending_user_id;
$user = User::find($this->decodePrimaryKey($sending_user)); $user = User::find($this->decodePrimaryKey($sending_user));
@ -141,22 +149,18 @@ class NinjaMailerJob implements ShouldQueue
* just for this request. * just for this request.
*/ */
// config(['mail.driver' => 'gmail']); config(['mail.driver' => 'gmail']);
// config(['services.gmail.token' => $user->oauth_user_token->access_token]); (new MailServiceProvider(app()))->register();
// config(['mail.from.address' => $user->email]);
// config(['mail.from.name' => $user->present()->name()]);
// (new MailServiceProvider(app()))->register();
// nlog("after registering mail service provider");
// nlog(config('services.gmail.token'));
$token = $user->oauth_user_token->access_token; $token = $user->oauth_user_token->access_token;
$user_id = $user->oauth_user_id;
$this->nmo $this->nmo
->mailable ->mailable
->from($user->email, $user->present()->name()) ->from($user->email, $user->present()->name())
->withSwiftMessage(function ($message) use($token) { ->withSwiftMessage(function ($message) use($token, $user_id) {
$message->getHeaders()->addTextHeader('GmailToken', $token); $message->getHeaders()->addTextHeader('GmailToken', $token);
$message->getHeaders()->addTextHeader('UserId', $user_id);
}); });
} }

View File

@ -61,6 +61,7 @@ class SendVerificationNotification implements ShouldQueue
// $event->user->notify(new VerifyUser($event->user, $event->company)); // $event->user->notify(new VerifyUser($event->user, $event->company));
Ninja::registerNinjaUser($event->user); Ninja::registerNinjaUser($event->user);
} catch (Exception $e) { } catch (Exception $e) {
nlog("I couldn't send the email " . $e->getMessage()); nlog("I couldn't send the email " . $e->getMessage());
} }

View File

@ -75,7 +75,7 @@ class TemplateEmail extends Mailable
]) ])
->withSwiftMessage(function ($message) use($company){ ->withSwiftMessage(function ($message) use($company){
$message->getHeaders()->addTextHeader('Tag', $company->company_key); $message->getHeaders()->addTextHeader('Tag', $company->company_key);
});; });
//conditionally attach files //conditionally attach files
if ($settings->pdf_email_attachment !== false && ! empty($this->build_email->getAttachments())) { if ($settings->pdf_email_attachment !== false && ! empty($this->build_email->getAttachments())) {

View File

@ -52,7 +52,7 @@ class InvoiceObserver
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company); WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company);
} }
UnlinkFile::dispatchNow(config('filesystems.default'), $invoice->client->invoice_filepath() . $invoice->number.'.pdf'); // UnlinkFile::dispatchNow(config('filesystems.default'), $invoice->client->invoice_filepath() . $invoice->number.'.pdf');
} }

View File

@ -147,7 +147,7 @@ class ApplyPayment
event(new InvoiceWasUpdated($this->invoice, $this->invoice->company, Ninja::eventVars())); event(new InvoiceWasUpdated($this->invoice, $this->invoice->company, Ninja::eventVars()));
if ((int)$this->invoice->balance == 0) { if ((int)$this->invoice->balance == 0) {
// $this->invoice->service()->deletePdf(); $this->invoice->service()->deletePdf();
event(new InvoiceWasPaid($this->invoice, $payment, $this->payment->company, Ninja::eventVars())); event(new InvoiceWasPaid($this->invoice, $payment, $this->payment->company, Ninja::eventVars()));
} }
} }

View File

@ -266,7 +266,7 @@ class InvoiceService
//$this->invoice = $this->invoice->calc()->getInvoice(); //$this->invoice = $this->invoice->calc()->getInvoice();
// $this->deletePdf(); $this->deletePdf();
return $this; return $this;
} }

99
config/gmail.php Normal file
View File

@ -0,0 +1,99 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Gmail Configuration
|--------------------------------------------------------------------------
|
|
|
| Scopes Available:
|
| * all - Read, send, delete, and manage your email
| * compose - Manage drafts and send emails
| * insert - Insert mail into your mailbox
| * labels - Manage mailbox labels
| * metadata - View your email message metadata such as labels and headers, but not the email body
| * modify - View and modify but not delete your email
| * readonly - View your email messages and settings
| * send - Send email on your behalf
| * settings_basic - Manage your basic mail settings
| * settings_sharing - Manage your sensitive mail settings, including who can manage your mail
|
| Leaving the scopes empty fill use readonly
|
| Credentials File Name
|
*/
'project_id' => env('GOOGLE_PROJECT_ID'),
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect_url' => env('GOOGLE_REDIRECT_URI', '/'),
'state' => null,
'scopes' => [
'readonly',
'modify',
],
/*
|--------------------------------------------------------------------------
| Additional Scopes [URL Style]
|--------------------------------------------------------------------------
|
| 'additional_scopes' => [
| 'https://www.googleapis.com/auth/drive',
| 'https://www.googleapis.com/auth/documents'
| ],
|
|
*/
'additional_scopes' => [
],
'access_type' => 'offline',
'approval_prompt' => 'force',
/*
|--------------------------------------------------------------------------
| Credentials File Name
|--------------------------------------------------------------------------
|
| :email to use, clients email on the file
|
|
*/
'credentials_file_name' => env('GOOGLE_CREDENTIALS_NAME', 'gmail-json'),
/*
|--------------------------------------------------------------------------
| Allow Multiple Credentials
|--------------------------------------------------------------------------
|
| Allow the application to store multiple credential json files.
|
|
*/
'allow_multiple_credentials' => env('GOOGLE_ALLOW_MULTIPLE_CREDENTIALS', false),
/*
|--------------------------------------------------------------------------
| Allow Encryption for json Files
|--------------------------------------------------------------------------
|
| Use Laravel Encrypt in json Files
|
|
*/
'allow_json_encrypt' => env('GOOGLE_ALLOW_JSON_ENCRYPT', false),
];