diff --git a/Services/EmailService.php b/Services/EmailService.php index f543ceaa4..79b1daa3f 100644 --- a/Services/EmailService.php +++ b/Services/EmailService.php @@ -19,6 +19,7 @@ use Webkul\UVDesk\CoreFrameworkBundle\Utils\TokenGenerator; use Webkul\UVDesk\MailboxBundle\Services\MailboxService; use Webkul\UVDesk\MailboxBundle\Utils\SMTP\Transport\AppTransportConfigurationInterface; +use Webkul\UVDesk\CoreFrameworkBundle\Services\MicrosoftIntegration; class EmailService { @@ -28,13 +29,14 @@ class EmailService private $session; private $mailer; - public function __construct(ContainerInterface $container, RequestStack $request, EntityManagerInterface $entityManager, SessionInterface $session, MailboxService $mailboxService) + public function __construct(ContainerInterface $container, RequestStack $request, EntityManagerInterface $entityManager, SessionInterface $session, MailboxService $mailboxService, MicrosoftIntegration $microsoftIntegration) { $this->request = $request; $this->container = $container; $this->entityManager = $entityManager; $this->session = $session; $this->mailboxService = $mailboxService; + $this->microsoftIntegration = $microsoftIntegration; } public function trans($text) @@ -537,7 +539,7 @@ public function sendMail($subject, $content, $recipient, array $headers = [], $m $supportEmailName = $mailbox->getName(); $supportEmail = $mailboxSmtpConfiguration->getUsername(); } - + // Prepare email $email = new Email(); $email @@ -622,7 +624,6 @@ public function sendMail($subject, $content, $recipient, array $headers = [], $m ], ], ], - 'internetMessageHeaders' => [], ]; foreach ($headers as $name => $value) { @@ -640,7 +641,27 @@ public function sendMail($subject, $content, $recipient, array $headers = [], $m ]; } - MicrosoftGraph\Me::sendMail($credentials['access_token'], $emailParams); + $graphResponse = MicrosoftGraph\Me::sendMail($credentials['access_token'], $emailParams); + + // Refresh access token if expired + if (!empty($graphResponse['error'])) { + if (!empty($graphResponse['error']['code']) && $graphResponse['error']['code'] == 'InvalidAuthenticationToken') { + $tokenResponse = $this->microsoftIntegration->refreshAccessToken($microsoftApp, $credentials['refresh_token']); + + if (!empty($tokenResponse['access_token'])) { + $microsoftAccount + ->setCredentials(json_encode($tokenResponse)) + ; + + $this->entityManager->persist($microsoftAccount); + $this->entityManager->flush(); + + $credentials = json_decode($microsoftAccount->getCredentials(), true); + + $graphResponse = MicrosoftGraph\Me::sendMail($credentials['access_token'], $emailParams); + } + } + } } else { $dsn = strtr("smtp://{email}:{password}@{host}:{port}", [ "{email}" => $mailboxSmtpConfiguration->getUsername(), diff --git a/Utils/Microsoft/Graph/Me.php b/Utils/Microsoft/Graph/Me.php index 84756ae1e..dab123c13 100644 --- a/Utils/Microsoft/Graph/Me.php +++ b/Utils/Microsoft/Graph/Me.php @@ -138,6 +138,7 @@ public static function sendMail($accessToken, $params) 'Authorization: Bearer ' . $accessToken, 'Content-Type: application/json', ]); + curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlHandler, CURLOPT_URL, $endpoint); curl_setopt($curlHandler, CURLOPT_POST, 1); diff --git a/Workflow/Actions/Customer/MailCustomer.php b/Workflow/Actions/Customer/MailCustomer.php index 8ac426b1e..c793c18c4 100644 --- a/Workflow/Actions/Customer/MailCustomer.php +++ b/Workflow/Actions/Customer/MailCustomer.php @@ -57,6 +57,7 @@ public static function applyAction(ContainerInterface $container, $entity, $valu $message = $container->get('email.service')->processEmailContent($emailTemplate->getMessage(), $emailPlaceholders); $messageId = $container->get('email.service')->sendMail($subject, $message, $entity->getEmail()); + break; default: break;