mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 03:57:32 -05:00 
			
		
		
		
	Gmail Email via API (#3124)
* Working on GMail Driver * working on custom Gmail Mail Provider * Add total_taxes to quotes and recurring_* * Implementation of new mailserviceprovider to allow native use of Mail:: with gmail email sending
This commit is contained in:
		
							parent
							
								
									1d2ec4c4ad
								
							
						
					
					
						commit
						1b6ec9699a
					
				
							
								
								
									
										67
									
								
								app/Helpers/Mail/GmailTransport.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								app/Helpers/Mail/GmailTransport.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,67 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Invoice Ninja (https://invoiceninja.com)
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @link https://github.com/invoiceninja/invoiceninja source repository
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @license https://opensource.org/licenses/AAL
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace App\Helpers\Mail;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use App\Libraries\MultiDB;
 | 
				
			||||||
 | 
					use App\Models\User;
 | 
				
			||||||
 | 
					use Dacastro4\LaravelGmail\Services\Message\Mail;
 | 
				
			||||||
 | 
					use Illuminate\Mail\Transport\Transport;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * GmailTransport
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					class GmailTransport extends Transport
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * The Gmail instance.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @var \Dacastro4\LaravelGmail\Services\Message\Mail
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    protected $gmail;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * The GMail OAuth Token
 | 
				
			||||||
 | 
					     * @var string token
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    protected $token;
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Create a new Gmail transport instance.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					     * @return void
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public function __construct(Mail $gmail, string $token)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->gmail = $gmail;
 | 
				
			||||||
 | 
					        $this->token = $token
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->beforeSendPerformed($message);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->gmail->using($this->token);
 | 
				
			||||||
 | 
					        $this->gmail->to($message->getTo());
 | 
				
			||||||
 | 
					        $this->gmail->from($message->getFrom());
 | 
				
			||||||
 | 
					        $this->gmail->subject($message->getSubject());
 | 
				
			||||||
 | 
					        //$this->gmail->message($message->getBody());
 | 
				
			||||||
 | 
					        $this->gmail->message($message->toString());
 | 
				
			||||||
 | 
					        $this->gmail->cc($message->getCc());
 | 
				
			||||||
 | 
					        $this->gmail->bcc($message->getBcc());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->gmail->send();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->sendPerformed($message);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $this->numberOfRecipients($message);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
@ -11,62 +11,34 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace App\Helpers\Mail;
 | 
					namespace App\Helpers\Mail;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use App\Libraries\MultiDB;
 | 
				
			||||||
use App\Models\User;
 | 
					use App\Models\User;
 | 
				
			||||||
 | 
					use App\Providers\MailServiceProvider;
 | 
				
			||||||
 | 
					use Illuminate\Support\Facades\Config;
 | 
				
			||||||
use Illuminate\Support\Facades\Mail;
 | 
					use Illuminate\Support\Facades\Mail;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * GmailTransportConfig
 | 
					 * GmailTransportConfig
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
class GmailTransportConfig
 | 
					class GmailTransportConfig
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public function test()
 | 
					    public function test()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					 | 
				
			||||||
// $transport = (new Swift_SmtpTransport('smtp.googlemail.com', 465, 'ssl'))
 | 
					 | 
				
			||||||
//   ->setUsername('YOUR_GMAIL_USERNAME')
 | 
					 | 
				
			||||||
//   ->setPassword('YOUR_GMAIL_PASSWORD')
 | 
					 | 
				
			||||||
// ;
 | 
					 | 
				
			||||||
// 
 | 
					 | 
				
			||||||
//		$transport = \Swift_SmtpTransport::newInstance($host, $port);
 | 
					 | 
				
			||||||
		// set encryption
 | 
					 | 
				
			||||||
		// if (isset($encryption)) $transport->setEncryption($encryption);
 | 
					 | 
				
			||||||
		// // set username and password
 | 
					 | 
				
			||||||
		// if (isset($username))
 | 
					 | 
				
			||||||
		// {
 | 
					 | 
				
			||||||
		//     $transport->setUsername($username);
 | 
					 | 
				
			||||||
		//     $transport->setPassword($password);
 | 
					 | 
				
			||||||
		// }
 | 
					 | 
				
			||||||
 
 | 
					 | 
				
			||||||
// 
 | 
					 | 
				
			||||||
// 
 | 
					 | 
				
			||||||
// // Create the Transport
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// // Create the Mailer using your created Transport
 | 
					 | 
				
			||||||
// $mailer = new Swift_Mailer($transport);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/********************* We may need to fetch a new token on behalf of the client ******************************/
 | 
					/********************* We may need to fetch a new token on behalf of the client ******************************/
 | 
				
			||||||
 | 
							$query = [
 | 
				
			||||||
 | 
							    'email' => 'david@invoiceninja.com',
 | 
				
			||||||
 | 
							];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $query = [
 | 
						   	$user = MultiDB::hasUser($query);
 | 
				
			||||||
            'email' => 'david@invoicninja.com',
 | 
					 | 
				
			||||||
            'oauth_provider_id'=>'google'
 | 
					 | 
				
			||||||
        ];
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $user = MultiDB::hasUser($query);
 | 
							Config::set('mail.driver', 'gmail');
 | 
				
			||||||
 | 
							Config::set('services.gmail.token', $user->oauth_user_token);
 | 
				
			||||||
		$transport = (new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls'))
 | 
							(new MailServiceProvider(app()))->register();   
 | 
				
			||||||
		    ->setAuthMode('XOAUTH2')
 | 
					 | 
				
			||||||
		    ->setUsername($user->email)
 | 
					 | 
				
			||||||
		    ->setPassword($user->oauth_user_token);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// set new swift mailer
 | 
					 | 
				
			||||||
		Mail::setSwiftMailer(new \Swift_Mailer($transport));
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Mail::to('david@romulus.com.au')
 | 
						    Mail::to('david@romulus.com.au')
 | 
				
			||||||
	    ->send('test');
 | 
						    ->send(new SupportMessageSent('a cool message'));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -81,3 +53,4 @@ class GmailTransportConfig
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										18
									
								
								app/Helpers/Mail/GmailTransportManager.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								app/Helpers/Mail/GmailTransportManager.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace App\Helpers\Mail;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use App\Helpers\Mail\GmailTransport;
 | 
				
			||||||
 | 
					use Dacastro4\LaravelGmail\Services\Message\Mail;
 | 
				
			||||||
 | 
					use Illuminate\Mail\TransportManager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class GmailTransportManager extends TransportManager
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    protected function createGmailDriver()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $token = $this->app['config']->get('services.gmail.token', []);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return new GmailTransport(Mail $mail, string $token);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										17
									
								
								app/Providers/MailServiceProvider.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								app/Providers/MailServiceProvider.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					<?php 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace App\Providers;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use App\Helpers\Mail\GmailTransportManager;
 | 
				
			||||||
 | 
					use Illuminate\Mail\MailServiceProvider as MailProvider;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class MailServiceProvider extends MailProvider
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    protected function registerSwiftTransport()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->app->singleton('swift.transport', function ($app) {
 | 
				
			||||||
 | 
					            return new GmailTransportManager($app);
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -21,6 +21,7 @@
 | 
				
			|||||||
        "php": ">=7.3",
 | 
					        "php": ">=7.3",
 | 
				
			||||||
        "anahkiasen/former": "^4.2",
 | 
					        "anahkiasen/former": "^4.2",
 | 
				
			||||||
        "asgrim/ofxparser": "^1.2",
 | 
					        "asgrim/ofxparser": "^1.2",
 | 
				
			||||||
 | 
					        "dacastro4/laravel-gmail": "^3.2",
 | 
				
			||||||
        "davejamesmiller/laravel-breadcrumbs": "5.x",
 | 
					        "davejamesmiller/laravel-breadcrumbs": "5.x",
 | 
				
			||||||
        "fideloper/proxy": "^4.0",
 | 
					        "fideloper/proxy": "^4.0",
 | 
				
			||||||
        "google/apiclient": "^2.0",
 | 
					        "google/apiclient": "^2.0",
 | 
				
			||||||
 | 
				
			|||||||
@ -148,7 +148,8 @@ return [
 | 
				
			|||||||
        Illuminate\Filesystem\FilesystemServiceProvider::class,
 | 
					        Illuminate\Filesystem\FilesystemServiceProvider::class,
 | 
				
			||||||
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
 | 
					        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
 | 
				
			||||||
        Illuminate\Hashing\HashServiceProvider::class,
 | 
					        Illuminate\Hashing\HashServiceProvider::class,
 | 
				
			||||||
        Illuminate\Mail\MailServiceProvider::class,
 | 
					        App\Providers\MailServiceProvider::class,
 | 
				
			||||||
 | 
					        //Illuminate\Mail\MailServiceProvider::class,
 | 
				
			||||||
        Illuminate\Notifications\NotificationServiceProvider::class,
 | 
					        Illuminate\Notifications\NotificationServiceProvider::class,
 | 
				
			||||||
        Illuminate\Pagination\PaginationServiceProvider::class,
 | 
					        Illuminate\Pagination\PaginationServiceProvider::class,
 | 
				
			||||||
        Illuminate\Pipeline\PipelineServiceProvider::class,
 | 
					        Illuminate\Pipeline\PipelineServiceProvider::class,
 | 
				
			||||||
 | 
				
			|||||||
@ -12,7 +12,7 @@ return [
 | 
				
			|||||||
    | your application here. By default, Laravel is setup for SMTP mail.
 | 
					    | your application here. By default, Laravel is setup for SMTP mail.
 | 
				
			||||||
    |
 | 
					    |
 | 
				
			||||||
    | Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
 | 
					    | Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
 | 
				
			||||||
    |            "sparkpost", "log", "array"
 | 
					    |            "sparkpost", "log", "array", "gmail"
 | 
				
			||||||
    |
 | 
					    |
 | 
				
			||||||
    */
 | 
					    */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -60,7 +60,6 @@ return [
 | 
				
			|||||||
        'paypal' => env('PAYPAL_KEYS', ''),
 | 
					        'paypal' => env('PAYPAL_KEYS', ''),
 | 
				
			||||||
        'travis' => env('TRAVIS', false),
 | 
					        'travis' => env('TRAVIS', false),
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
 | 
					 | 
				
			||||||
    'contact' => [
 | 
					    'contact' => [
 | 
				
			||||||
        'email' => env('MAIL_FROM_ADDRESS'),
 | 
					        'email' => env('MAIL_FROM_ADDRESS'),
 | 
				
			||||||
        'from_name' => env('MAIL_FROM_NAME'),
 | 
					        'from_name' => env('MAIL_FROM_NAME'),
 | 
				
			||||||
 | 
				
			|||||||
@ -33,7 +33,9 @@ return [
 | 
				
			|||||||
    'sparkpost' => [
 | 
					    'sparkpost' => [
 | 
				
			||||||
        'secret' => env('SPARKPOST_SECRET'),
 | 
					        'secret' => env('SPARKPOST_SECRET'),
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
 | 
					    'gmail' => [
 | 
				
			||||||
 | 
					        'token' => '',
 | 
				
			||||||
 | 
					    ],
 | 
				
			||||||
    'postmark' => env('POSTMARK_API_TOKEN', ''),
 | 
					    'postmark' => env('POSTMARK_API_TOKEN', ''),
 | 
				
			||||||
    'postmark_ticket' => env('POSTMARK_API_TICKET_TOKEN'),
 | 
					    'postmark_ticket' => env('POSTMARK_API_TICKET_TOKEN'),
 | 
				
			||||||
    'postmark_ticket_2' => env('POSTMARK_API_TICKET_TOKEN_2'),
 | 
					    'postmark_ticket_2' => env('POSTMARK_API_TICKET_TOKEN_2'),
 | 
				
			||||||
 | 
				
			|||||||
@ -511,6 +511,9 @@ class CreateUsersTable extends Migration
 | 
				
			|||||||
            $t->string('tax_name3')->nullable();
 | 
					            $t->string('tax_name3')->nullable();
 | 
				
			||||||
            $t->decimal('tax_rate3', 13, 3)->default(0);
 | 
					            $t->decimal('tax_rate3', 13, 3)->default(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $t->decimal('total_taxes', 13, 3)->default(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $t->string('custom_value1')->nullable();
 | 
					            $t->string('custom_value1')->nullable();
 | 
				
			||||||
            $t->string('custom_value2')->nullable();
 | 
					            $t->string('custom_value2')->nullable();
 | 
				
			||||||
            $t->string('custom_value3')->nullable();
 | 
					            $t->string('custom_value3')->nullable();
 | 
				
			||||||
@ -573,6 +576,9 @@ class CreateUsersTable extends Migration
 | 
				
			|||||||
            $t->string('tax_name3')->nullable();
 | 
					            $t->string('tax_name3')->nullable();
 | 
				
			||||||
            $t->decimal('tax_rate3', 13, 3)->default(0);
 | 
					            $t->decimal('tax_rate3', 13, 3)->default(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $t->decimal('total_taxes', 13, 3)->default(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $t->string('custom_value1')->nullable();
 | 
					            $t->string('custom_value1')->nullable();
 | 
				
			||||||
            $t->string('custom_value2')->nullable();
 | 
					            $t->string('custom_value2')->nullable();
 | 
				
			||||||
            $t->string('custom_value3')->nullable();
 | 
					            $t->string('custom_value3')->nullable();
 | 
				
			||||||
@ -636,6 +642,8 @@ class CreateUsersTable extends Migration
 | 
				
			|||||||
            $t->string('tax_name3')->nullable();
 | 
					            $t->string('tax_name3')->nullable();
 | 
				
			||||||
            $t->decimal('tax_rate3', 13, 3)->default(0);
 | 
					            $t->decimal('tax_rate3', 13, 3)->default(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $t->decimal('total_taxes', 13, 3)->default(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $t->boolean('uses_inclusive_taxes')->default(0);
 | 
					            $t->boolean('uses_inclusive_taxes')->default(0);
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            $t->string('custom_value1')->nullable();
 | 
					            $t->string('custom_value1')->nullable();
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user