1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Helpers/Mail/GmailTransport.php

88 lines
2.3 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Helpers\Mail;
2023-02-16 02:36:09 +01:00
use Google\Client;
2022-07-11 13:40:23 +02:00
use Google\Service\Gmail;
use Google\Service\Gmail\Message;
2022-06-24 15:49:22 +02:00
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Component\Mime\MessageConverter;
2022-07-06 02:08:09 +02:00
/**
* GmailTransport.
*/
2022-06-24 15:49:22 +02:00
class GmailTransport extends AbstractTransport
{
2022-06-24 15:49:22 +02:00
public function __construct()
{
2022-06-24 15:49:22 +02:00
parent::__construct();
}
2022-06-24 15:49:22 +02:00
protected function doSend(SentMessage $message): void
{
2022-08-03 04:35:38 +02:00
nlog("In Do Send");
2022-06-24 15:49:22 +02:00
$message = MessageConverter::toEmail($message->getOriginalMessage());
2023-08-07 07:07:52 +02:00
/** @phpstan-ignore-next-line **/
2022-07-31 11:11:32 +02:00
$token = $message->getHeaders()->get('gmailtoken')->getValue();
$message->getHeaders()->remove('gmailtoken');
2022-06-24 15:49:22 +02:00
2022-07-11 13:40:23 +02:00
$client = new Client();
$client->setClientId(config('ninja.auth.google.client_id'));
$client->setClientSecret(config('ninja.auth.google.client_secret'));
$client->setAccessToken($token);
2024-01-14 05:05:00 +01:00
2022-07-11 13:40:23 +02:00
$service = new Gmail($client);
2022-06-24 15:49:22 +02:00
2022-07-11 13:40:23 +02:00
$body = new Message();
2022-08-03 04:35:38 +02:00
$bccs = $message->getHeaders()->get('Bcc');
$bcc_list = '';
2023-02-16 02:36:09 +01:00
if ($bccs) {
2022-08-03 04:35:38 +02:00
$bcc_list = 'Bcc: ';
2024-01-14 05:05:00 +01:00
2023-08-07 07:07:52 +02:00
/** @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 .= $address->getAddress() .',';
}
$bcc_list = rtrim($bcc_list, ",") . "\r\n";
2023-02-16 02:36:09 +01:00
}
2022-08-03 04:35:38 +02:00
$body->setRaw($this->base64_encode($bcc_list.$message->toString()));
2022-06-24 15:49:22 +02:00
2023-02-16 02:36:09 +01:00
try {
2023-01-16 03:31:54 +01:00
$service->users_messages->send('me', $body, []);
2023-02-16 02:36:09 +01:00
} catch(\Google\Service\Exception $e) {
2023-01-16 03:31:54 +01:00
/* Need to slow down */
2023-02-16 02:36:09 +01:00
if ($e->getCode() == '429') {
nlog("429 google - retrying ");
2023-01-16 03:31:54 +01:00
$service->users_messages->send('me', $body, []);
}
}
}
2024-01-14 05:05:00 +01:00
2022-07-11 13:40:23 +02:00
private function base64_encode($data)
{
return rtrim(strtr(base64_encode($data), ['+' => '-', '/' => '_']), '=');
}
public function __toString(): string
2022-06-24 15:49:22 +02:00
{
return 'gmail';
}
}