1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Helpers/Mail/Office365MailTransport.php

67 lines
2.1 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();
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) {
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) {
sleep(5);
$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();
}
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';
}
}