1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Helpers/Mail/Office365MailTransport.php

64 lines
2.0 KiB
PHP
Raw Normal View History

2022-06-17 07:42:14 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Helpers\Mail;
2022-07-11 13:48:23 +02:00
2022-06-17 10:28:31 +02:00
use Illuminate\Support\Str;
2022-06-17 07:42:14 +02:00
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model\UploadSession;
2022-07-11 13:48:23 +02:00
use Symfony\Component\Mailer\SentMessage;
2022-07-11 04:48:59 +02:00
use Symfony\Component\Mailer\Transport\AbstractTransport;
2022-07-11 13:48:23 +02:00
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\MessageConverter;
2022-06-17 07:42:14 +02:00
2022-07-11 04:48:59 +02:00
class Office365MailTransport extends AbstractTransport
2022-06-17 07:42:14 +02:00
{
2022-07-11 13:48:23 +02:00
2022-06-17 07:42:14 +02:00
public function __construct()
{
2022-07-11 13:48:23 +02:00
parent::__construct();
2022-06-17 07:42:14 +02:00
}
2022-07-11 13:48:23 +02:00
protected function doSend(SentMessage $message): void
2022-06-17 07:42:14 +02:00
{
2022-07-11 04:48:59 +02:00
2022-07-11 13:48:23 +02:00
$symfony_message = MessageConverter::toEmail($message->getOriginalMessage());
2022-06-17 07:42:14 +02:00
$graph = new Graph();
2022-07-31 11:11:32 +02:00
$token = $symfony_message->getHeaders()->get('gmailtoken')->getValue();
$symfony_message->getHeaders()->remove('gmailtoken');
2022-06-17 07:42:14 +02:00
2022-06-17 10:28:31 +02:00
$graph->setAccessToken($token);
2022-06-17 07:42:14 +02:00
2022-06-17 12:13:16 +02:00
try {
2022-07-15 09:03:32 +02:00
$graphMessage = $graph->createRequest('POST', '/users/'.$symfony_message->getFrom()[0]->getAddress().'/sendmail')
2022-07-15 08:55:10 +02:00
->attachBody(base64_encode($message->toString()))
->addHeaders(['Content-Type' => 'text/plain'])
2022-06-17 12:13:16 +02:00
->setReturnType(\Microsoft\Graph\Model\Message::class)
->execute();
} catch (\Exception $e) {
2022-06-17 12:13:16 +02:00
sleep(5);
2022-07-15 09:03:32 +02:00
$graphMessage = $graph->createRequest('POST', '/users/'.$symfony_message->getFrom()[0]->getAddress().'/sendmail')
2022-07-15 08:55:10 +02:00
->attachBody(base64_encode($message->toString()))
->addHeaders(['Content-Type' => 'text/plain'])
2022-06-17 12:13:16 +02:00
->setReturnType(\Microsoft\Graph\Model\Message::class)
->execute();
}
2022-07-15 08:55:10 +02:00
2022-06-17 07:42:14 +02:00
}
2022-07-15 08:55:10 +02:00
public function __toString(): string
2022-07-11 04:48:59 +02:00
{
return 'office365';
}
}