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

65 lines
1.6 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-06-24 15:49:22 +02:00
nlog("in Do Send");
$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();
$body->setRaw($this->base64_encode($message->toString()));
2022-06-24 15:49:22 +02:00
2022-07-11 13:40:23 +02:00
$service->users_messages->send('me', $body, []);
2022-06-24 15:49:22 +02:00
}
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';
}
}