Skip to content

Commit

Permalink
Merge branch '1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Kumar committed Dec 28, 2022
2 parents b951e01 + 3e7db95 commit 29cc49a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
29 changes: 25 additions & 4 deletions Services/EmailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -622,7 +624,6 @@ public function sendMail($subject, $content, $recipient, array $headers = [], $m
],
],
],
'internetMessageHeaders' => [],
];

foreach ($headers as $name => $value) {
Expand All @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions Utils/Microsoft/Graph/Me.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions Workflow/Actions/Customer/MailCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 29cc49a

Please sign in to comment.