1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Helpers/Mail/Office365MailTransport.php

78 lines
2.3 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 07:42:14 +02:00
use Microsoft\Graph\Graph;
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\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
{
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 13:48:23 +02:00
$symfony_message = MessageConverter::toEmail($message->getOriginalMessage());
2022-06-17 07:42:14 +02:00
$graph = new Graph();
2024-01-14 05:05:00 +01:00
/** @phpstan-ignore-next-line **/
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-08-03 04:35:38 +02:00
$bccs = $symfony_message->getHeaders()->get('Bcc');
$bcc_list = '';
2023-02-16 02:36:09 +01:00
if ($bccs) {
/** @phpstan-ignore-next-line **/
2023-02-16 02:36:09 +01:00
foreach ($bccs->getAddresses() as $address) {
2022-08-03 04:35:38 +02:00
$bcc_list .= 'Bcc: "'.$address->getAddress().'" <'.$address->getAddress().'>\r\n';
2022-06-17 12:13:16 +02:00
}
2023-02-16 02:36:09 +01:00
}
2022-08-03 04:35:38 +02:00
try {
$graphMessage = $graph->createRequest('POST', '/users/'.$symfony_message->getFrom()[0]->getAddress().'/sendmail')
->attachBody(base64_encode($bcc_list.$message->toString()))
->addHeaders(['Content-Type' => 'text/plain'])
->setReturnType(\Microsoft\Graph\Model\Message::class)
->execute();
} catch (\Exception $e) {
2024-04-19 02:37:24 +02:00
sleep(rand(5,10));
try {
2022-08-03 04:35:38 +02:00
$graphMessage = $graph->createRequest('POST', '/users/'.$symfony_message->getFrom()[0]->getAddress().'/sendmail')
->attachBody(base64_encode($bcc_list.$message->toString()))
->addHeaders(['Content-Type' => 'text/plain'])
->setReturnType(\Microsoft\Graph\Model\Message::class)
->execute();
2024-04-19 02:37:24 +02:00
} catch (\Exception $e) {
}
2022-08-03 04:35:38 +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';
}
}