2019-12-04 05:52:04 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-12-04 05:52:04 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-12-04 05:52:04 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-12-04 05:52:04 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Helpers\Mail;
|
|
|
|
|
2021-02-11 04:43:48 +01:00
|
|
|
use App\Utils\TempFile;
|
2021-02-17 01:25:30 +01:00
|
|
|
use Dacastro4\LaravelGmail\Facade\LaravelGmail;
|
2019-12-04 05:52:04 +01:00
|
|
|
use Dacastro4\LaravelGmail\Services\Message\Mail;
|
|
|
|
use Illuminate\Mail\Transport\Transport;
|
2019-12-04 06:26:07 +01:00
|
|
|
use Swift_Mime_SimpleMessage;
|
2019-12-04 05:52:04 +01:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* GmailTransport.
|
2019-12-04 05:52:04 +01:00
|
|
|
*/
|
|
|
|
class GmailTransport extends Transport
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The Gmail instance.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @var Mail
|
2019-12-04 05:52:04 +01:00
|
|
|
*/
|
|
|
|
protected $gmail;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new Gmail transport instance.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Mail $gmail
|
|
|
|
* @param string $token
|
2019-12-04 05:52:04 +01:00
|
|
|
*/
|
2021-02-16 13:24:36 +01:00
|
|
|
public function __construct(Mail $gmail)
|
2019-12-04 05:52:04 +01:00
|
|
|
{
|
|
|
|
$this->gmail = $gmail;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
|
|
|
|
{
|
2021-02-17 03:21:47 +01:00
|
|
|
/* For some reason the Injected Mail class carries cached tokens, so we need to reinit the Mail class*/
|
|
|
|
$this->gmail = null;
|
|
|
|
$this->gmail = new Mail;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-06-17 15:13:49 +02:00
|
|
|
nlog($message->getBcc());
|
|
|
|
|
2021-02-17 03:21:47 +01:00
|
|
|
/*We should nest the token in the message and then discard it as needed*/
|
2021-02-17 01:25:30 +01:00
|
|
|
$token = $message->getHeaders()->get('GmailToken')->getValue();
|
2021-02-16 13:24:36 +01:00
|
|
|
|
2021-02-16 13:56:12 +01:00
|
|
|
$message->getHeaders()->remove('GmailToken');
|
2021-02-16 13:24:36 +01:00
|
|
|
|
2019-12-04 05:52:04 +01:00
|
|
|
$this->beforeSendPerformed($message);
|
|
|
|
|
2021-02-17 01:25:30 +01:00
|
|
|
$this->gmail->using($token);
|
2019-12-04 05:52:04 +01:00
|
|
|
$this->gmail->to($message->getTo());
|
|
|
|
$this->gmail->from($message->getFrom());
|
|
|
|
$this->gmail->subject($message->getSubject());
|
2019-12-04 06:26:07 +01:00
|
|
|
$this->gmail->message($message->getBody());
|
2021-02-17 03:20:25 +01:00
|
|
|
|
2019-12-04 05:52:04 +01:00
|
|
|
$this->gmail->cc($message->getCc());
|
|
|
|
$this->gmail->bcc($message->getBcc());
|
|
|
|
|
2021-02-11 08:58:37 +01:00
|
|
|
foreach ($message->getChildren() as $child)
|
|
|
|
{
|
2020-05-20 08:59:29 +02:00
|
|
|
|
2021-02-11 08:32:49 +01:00
|
|
|
nlog("trying to attach");
|
|
|
|
|
2021-02-11 08:58:37 +01:00
|
|
|
if($child->getContentType() != 'text/plain')
|
|
|
|
{
|
2021-02-11 08:32:49 +01:00
|
|
|
|
2021-02-11 08:58:37 +01:00
|
|
|
$this->gmail->attach(TempFile::filePath($child->getBody(), $child->getHeaders()->get('Content-Type')->getParameter('name') ));
|
2021-02-11 08:32:49 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-11 08:58:37 +01:00
|
|
|
}
|
2021-02-11 08:32:49 +01:00
|
|
|
|
2019-12-04 05:52:04 +01:00
|
|
|
$this->gmail->send();
|
|
|
|
|
|
|
|
$this->sendPerformed($message);
|
|
|
|
|
|
|
|
return $this->numberOfRecipients($message);
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|