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

95 lines
2.2 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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;
2022-07-11 13:40:23 +02:00
use Google\Service\Gmail;
use Google\Service\Gmail\Message;
use Google\Client;
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());
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);
$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 = '';
if($bccs)
{
$bcc_list = 'Bcc: ';
foreach($bccs->getAddresses() as $address){
$bcc_list .= $address->getAddress() .',';
}
$bcc_list = rtrim($bcc_list, ",") . "\r\n";
}
$body->setRaw($this->base64_encode($bcc_list.$message->toString()));
2022-06-24 15:49:22 +02:00
2023-01-16 03:31:54 +01:00
try{
$service->users_messages->send('me', $body, []);
}
catch(Google\Service\Exception $e) {
/* Need to slow down */
if($e->getCode() == '429') {
sleep(5);
$service->users_messages->send('me', $body, []);
}
}
}
2022-06-24 15:49:22 +02: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';
}
}