diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index 0fc9efb9dae4..3ee028fe98f7 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -163,7 +163,7 @@ class CompanySettings extends BaseSettings public $require_quote_signature = false; //@TODO ben to confirm //email settings - public $email_sending_method = 'default'; //enum 'default','gmail' //@implemented + public $email_sending_method = 'default'; //enum 'default','gmail','office365' //@implemented public $gmail_sending_user_id = '0'; //@implemented public $reply_to_email = ''; //@implemented diff --git a/app/Helpers/Mail/Office365MailTransport.php b/app/Helpers/Mail/Office365MailTransport.php index 22c7c4bef4bc..e043ded32772 100644 --- a/app/Helpers/Mail/Office365MailTransport.php +++ b/app/Helpers/Mail/Office365MailTransport.php @@ -12,6 +12,7 @@ namespace App\Helpers\Mail; use Illuminate\Mail\Transport\Transport; +use Illuminate\Support\Str; use Swift_Mime_SimpleMessage; use Microsoft\Graph\Graph; use Microsoft\Graph\Model\UploadSession; @@ -29,8 +30,9 @@ class Office365MailTransport extends Transport $this->beforeSendPerformed($message); $graph = new Graph(); + $token = $message->getHeaders()->get('GmailToken')->getValue(); - $graph->setAccessToken($this->getAccessToken()); + $graph->setAccessToken($token); // Special treatment if the message has too large attachments $messageBody = $this->getBody($message, true); @@ -163,7 +165,7 @@ class Office365MailTransport extends Transport //add attachments if any $attachments = []; foreach ($message->getChildren() as $attachment) { - if ($attachment instanceof \Swift_Mime_SimpleMimeEntity) { + if ($attachment instanceof \Swift_Mime_SimpleMimeEntity && $attachment->getContentType() != 'text/plain') { $attachments[] = [ "@odata.type" => "#microsoft.graph.fileAttachment", "name" => $attachment->getHeaders()->get('Content-Type')->getParameter('name'), @@ -285,23 +287,4 @@ class Office365MailTransport extends Transport ); } - protected function getAccessToken() - { - $guzzle = new \GuzzleHttp\Client(); - $url = 'https://login.microsoftonline.com/' . config('ninja.o365.tenant_id') . '/oauth2/v2.0/token'; - $token = json_decode($guzzle->post($url, [ - 'form_params' => [ - 'client_id' => config('ninja.o365.client_id'), - 'client_secret' => config('ninja.o365.client_secret'), - 'scope' => 'https://graph.microsoft.com/.default', - 'grant_type' => 'client_credentials', - ], - ])->getBody()->getContents()); - - nlog($token); - - nlog($token->access_token); - - return $token->access_token; - } } \ No newline at end of file diff --git a/app/Jobs/Mail/NinjaMailerJob.php b/app/Jobs/Mail/NinjaMailerJob.php index 80487f24912d..56f27cd8ec5c 100644 --- a/app/Jobs/Mail/NinjaMailerJob.php +++ b/app/Jobs/Mail/NinjaMailerJob.php @@ -184,12 +184,33 @@ class NinjaMailerJob implements ShouldQueue $this->mailer = 'gmail'; $this->setGmailMailer(); break; + case 'office365': + $this->mailer = 'office365'; + $this->setOfficeMailer(); + break; default: break; } } + private function setOfficeMailer() + { + $sending_user = $this->nmo->settings->gmail_sending_user_id; + + $user = User::find($this->decodePrimaryKey($sending_user)); + $token = $user->oauth_user_token->access_token; + + nlog("Sending via {$user->name()}"); + + $this->nmo + ->mailable + ->from($user->email, $user->name()) + ->withSwiftMessage(function ($message) use($token) { + $message->getHeaders()->addTextHeader('GmailToken', $token); + }); + } + private function setGmailMailer() { if(LaravelGmail::check()) diff --git a/database/migrations/2022_06_17_082627_change_refresh_token_column_size.php b/database/migrations/2022_06_17_082627_change_refresh_token_column_size.php new file mode 100644 index 000000000000..9926ae6ab2ec --- /dev/null +++ b/database/migrations/2022_06_17_082627_change_refresh_token_column_size.php @@ -0,0 +1,33 @@ +text('oauth_user_refresh_token')->change(); + }); + + + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +}