From c8a9b3efc0349269debacf4ca1b47fd35d6d93e5 Mon Sep 17 00:00:00 2001 From: Antoine Augusti Date: Fri, 26 Feb 2016 12:46:13 +0100 Subject: [PATCH] Switch from Mandrill to Mailgun --- .env.example | 1 - .../Mail/Facades/MandrillClient.php | 22 --- app/TeenQuotes/Mail/MailServiceProvider.php | 17 +- app/TeenQuotes/Mail/MailSwitcher.php | 8 +- app/TeenQuotes/Mail/Mandrill.php | 80 --------- .../Mail/MandrillServiceProvider.php | 56 ------ .../Mail/Transport/MandrillTransport.php | 32 ---- .../Console/UnsubscribeUsersCommand.php | 162 ------------------ .../NewslettersServiceProvider.php | 1 - app/config/app.php | 2 - app/config/mail.php | 2 +- composer.json | 1 - 12 files changed, 6 insertions(+), 378 deletions(-) delete mode 100644 app/TeenQuotes/Mail/Facades/MandrillClient.php delete mode 100644 app/TeenQuotes/Mail/Mandrill.php delete mode 100644 app/TeenQuotes/Mail/MandrillServiceProvider.php delete mode 100644 app/TeenQuotes/Mail/Transport/MandrillTransport.php delete mode 100644 app/TeenQuotes/Newsletters/Console/UnsubscribeUsersCommand.php diff --git a/.env.example b/.env.example index 7b16e765..10d6b936 100644 --- a/.env.example +++ b/.env.example @@ -5,7 +5,6 @@ CACHE_DRIVER=file MAILGUN_DOMAIN=dummy MAILGUN_SECRET=dummy MAILGUN_PUBKEY=dummy -MANDRILL_SECRET=dummy MAILCHIMP_SECRET=dummy EASYREC_APIKEY=dummy diff --git a/app/TeenQuotes/Mail/Facades/MandrillClient.php b/app/TeenQuotes/Mail/Facades/MandrillClient.php deleted file mode 100644 index 492dd29b..00000000 --- a/app/TeenQuotes/Mail/Facades/MandrillClient.php +++ /dev/null @@ -1,22 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace TeenQuotes\Mail\Facades; - -use Illuminate\Support\Facades\Facade; - -class MandrillClient extends Facade -{ - protected static function getFacadeAccessor() - { - return 'mandrill'; - } -} diff --git a/app/TeenQuotes/Mail/MailServiceProvider.php b/app/TeenQuotes/Mail/MailServiceProvider.php index f7ef0d08..d6257287 100644 --- a/app/TeenQuotes/Mail/MailServiceProvider.php +++ b/app/TeenQuotes/Mail/MailServiceProvider.php @@ -11,8 +11,6 @@ namespace TeenQuotes\Mail; -use TeenQuotes\Mail\Transport\MandrillTransport; - class MailServiceProvider extends \Illuminate\Mail\MailServiceProvider { public function register() @@ -21,6 +19,7 @@ public function register() $this->registerViewComposers(); + // Register a custom mailer to automatically inline CSS $this->app->bindShared('mailer', function ($app) use ($me) { $me->registerSwiftMailer(); @@ -53,20 +52,6 @@ public function register() }); } - /** - * Register the Mandrill Swift Transport instance. - * - * @param array $config - */ - protected function registerMandrillTransport($config) - { - $mandrill = $this->app['config']->get('services.mandrill', []); - - $this->app->bindShared('swift.transport', function () use ($mandrill) { - return new MandrillTransport($mandrill['secret']); - }); - } - private function registerViewComposers() { // Welcome email diff --git a/app/TeenQuotes/Mail/MailSwitcher.php b/app/TeenQuotes/Mail/MailSwitcher.php index 2e65c1ea..385687bd 100644 --- a/app/TeenQuotes/Mail/MailSwitcher.php +++ b/app/TeenQuotes/Mail/MailSwitcher.php @@ -48,9 +48,9 @@ public function __construct($driver) Config::set('mail.from', Config::get('mail.from.smtp')); break; - case 'mandrill': - // Switch to Mandrill - Config::set('mail.driver', 'mandrill'); + case 'mailgun': + // Switch to Mailgun + Config::set('mail.driver', 'mailgun'); Config::set('mail.from', Config::get('mail.from')); break; } @@ -68,7 +68,7 @@ public function __construct($driver) */ public static function getAvailableDrivers() { - return ['smtp', 'mandrill']; + return ['smtp', 'mailgun']; } /** diff --git a/app/TeenQuotes/Mail/Mandrill.php b/app/TeenQuotes/Mail/Mandrill.php deleted file mode 100644 index 2f00b1f9..00000000 --- a/app/TeenQuotes/Mail/Mandrill.php +++ /dev/null @@ -1,80 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace TeenQuotes\Mail; - -use Illuminate\Support\Collection; -use Mandrill as M; -use TeenQuotes\Users\Repositories\UserRepository; - -class Mandrill -{ - /** - * The client for the Mandrill API. - * - * @var M - */ - private $api; - - /** - * @var UserRepository - */ - private $userRepo; - - public function __construct(M $api, UserRepository $userRepo) - { - $this->api = $api; - $this->userRepo = $userRepo; - } - - /** - * Get email addresses that have already have an hard bounce. - * - * @return array - */ - public function getHardBouncedEmails() - { - $result = $this->api->rejects->getList('', false); - $collection = new Collection($result); - - $hardBounced = $collection->filter(function ($a) { - return $a['reason'] == 'hard-bounce'; - }); - - return $hardBounced->lists('email'); - } - - /** - * Get users that have already been affected by an hard bounce. - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public function getHardBouncedUsers() - { - return $this->userRepo->getByEmails( - $this->getHardBouncedEmails() - ); - } - - /** - * Delete an email address from the rejection list. - * - * @param string $email - * - * @return bool Whether the address was deleted successfully - */ - public function deleteEmailFromRejection($email) - { - $result = $this->api->rejects->delete($email); - - return $result['deleted']; - } -} diff --git a/app/TeenQuotes/Mail/MandrillServiceProvider.php b/app/TeenQuotes/Mail/MandrillServiceProvider.php deleted file mode 100644 index e6a5c547..00000000 --- a/app/TeenQuotes/Mail/MandrillServiceProvider.php +++ /dev/null @@ -1,56 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace TeenQuotes\Mail; - -use Illuminate\Support\ServiceProvider; -use Mandrill as BaseMandrill; - -class MandrillServiceProvider extends ServiceProvider -{ - /** - * Indicates if loading of the provider is deferred. - * - * @var bool - */ - protected $defer = false; - - /** - * Bootstrap the application events. - */ - public function boot() - { - // - } - - /** - * Register the service provider. - */ - public function register() - { - $this->app->bind('mandrill', function () { - $mandrillSecret = $this->app['config']->get('services.mandrill.secret'); - $mandrillAPI = new BaseMandrill($mandrillSecret); - - return new Mandrill($mandrillAPI, $this->app->make('TeenQuotes\Users\Repositories\UserRepository')); - }); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return ['mandrill']; - } -} diff --git a/app/TeenQuotes/Mail/Transport/MandrillTransport.php b/app/TeenQuotes/Mail/Transport/MandrillTransport.php deleted file mode 100644 index 65a864c1..00000000 --- a/app/TeenQuotes/Mail/Transport/MandrillTransport.php +++ /dev/null @@ -1,32 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace TeenQuotes\Mail\Transport; - -use Illuminate\Mail\Transport\MandrillTransport as BaseMandrillTransport; -use Swift_Mime_Message; - -class MandrillTransport extends BaseMandrillTransport -{ - public function send(Swift_Mime_Message $message, &$failedRecipients = null) - { - $client = $this->getHttpClient(); - - $client->post('https://mandrillapp.com/api/1.0/messages/send-raw.json', [ - 'body' => [ - 'key' => $this->key, - 'raw_message' => (string) $message, - // see https://mandrillapp.com/api/docs/messages.JSON.html#method=send-raw - 'async' => true, - ], - ]); - } -} diff --git a/app/TeenQuotes/Newsletters/Console/UnsubscribeUsersCommand.php b/app/TeenQuotes/Newsletters/Console/UnsubscribeUsersCommand.php deleted file mode 100644 index f44f0135..00000000 --- a/app/TeenQuotes/Newsletters/Console/UnsubscribeUsersCommand.php +++ /dev/null @@ -1,162 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace TeenQuotes\Newsletters\Console; - -use Indatus\Dispatcher\Scheduling\Schedulable; -use Indatus\Dispatcher\Scheduling\ScheduledCommand; -use Log; -use MandrillClient; -use TeenQuotes\Mail\UserMailer; -use TeenQuotes\Newsletters\NewslettersManager; -use TeenQuotes\Users\Repositories\UserRepository; - -class UnsubscribeUsersCommand extends ScheduledCommand -{ - /** - * The console command name. - * - * @var string - */ - protected $name = 'newsletter:deleteUsers'; - - /** - * The console command description. - * - * @var string - */ - protected $description = 'Unsubscribe users from newsletters.'; - - /** - * @var \TeenQuotes\Users\Repositories\UserRepository - */ - private $userRepo; - - /** - * @var \TeenQuotes\Newsletters\NewslettersManager - */ - private $newslettersManager; - - /** - * @var \TeenQuotes\Mail\UserMailer - */ - private $userMailer; - - /** - * Create a new command instance. - */ - public function __construct(UserRepository $userRepo, NewslettersManager $newslettersManager, UserMailer $userMailer) - { - parent::__construct(); - - $this->newslettersManager = $newslettersManager; - $this->userMailer = $userMailer; - $this->userRepo = $userRepo; - } - - /** - * When a command should run. - * - * @param \Indatus\Dispatcher\Scheduling\Schedulable - * - * @return \Indatus\Dispatcher\Scheduling\Schedulable - */ - public function schedule(Schedulable $scheduler) - { - return $scheduler - ->daily() - ->hours(17) - ->minutes(30); - } - - /** - * Choose the environment(s) where the command should run. - * - * @return array Array of environments' name - */ - public function environment() - { - return ['production']; - } - - /** - * Execute the console command. - */ - public function fire() - { - // Retrieve inactive users - $nonActiveUsers = $this->userRepo->getNonActiveHavingNewsletter(); - - $hardBouncedUsers = $this->getHardBouncedUsers(); - - // Merge all users that need to be unsubscribed from newsletters - $allUsers = $nonActiveUsers->merge($hardBouncedUsers); - - // Unsubscribe these users from newsletters - $this->newslettersManager->deleteForUsers($allUsers); - - // Send an email to each user to notice them - $nonActiveUsers->each(function ($user) { - // Log this info - $this->writeToLog('Unsubscribing user from newsletters: '.$user->login.' - '.$user->email); - - // Send the actual email - $this->userMailer->unsubscribeUserFromNewsletter($user); - }); - } - - /** - * Get users that have already been affected by a hard bounce. - * - * @return \Illuminate\Database\Eloquent\Collection - */ - private function getHardBouncedUsers() - { - $users = MandrillClient::getHardBouncedUsers(); - - // Delete each user from the existing rejection list - $instance = $this; - $users->each(function ($user) use ($instance) { - MandrillClient::deleteEmailFromRejection($user->email); - - // Log this info - $instance->writeToLog('Removing user from the rejection list: '.$user->login.' - '.$user->email); - }); - - return $users; - } - - private function writeToLog($line) - { - $this->info($line); - Log::info($line); - } - - /** - * Get the console command arguments. - * - * @return array - */ - protected function getArguments() - { - return []; - } - - /** - * Get the console command options. - * - * @return array - */ - protected function getOptions() - { - return []; - } -} diff --git a/app/TeenQuotes/Newsletters/NewslettersServiceProvider.php b/app/TeenQuotes/Newsletters/NewslettersServiceProvider.php index a4e56f31..15544632 100644 --- a/app/TeenQuotes/Newsletters/NewslettersServiceProvider.php +++ b/app/TeenQuotes/Newsletters/NewslettersServiceProvider.php @@ -56,7 +56,6 @@ private function registerCommands() { $commands = [ 'newsletters.console.sendNewsletter' => $this->getNamespaceConsole().'SendNewsletterCommand', - 'newsletters.console.unsubscribeUsers' => $this->getNamespaceConsole().'UnsubscribeUsersCommand', ]; foreach ($commands as $key => $class) { diff --git a/app/config/app.php b/app/config/app.php index 5940693b..3a8c38d5 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -154,7 +154,6 @@ 'TeenQuotes\Comments\CommentsServiceProvider', 'TeenQuotes\Countries\CountriesServiceProvider', 'TeenQuotes\Mail\MailServiceProvider', - 'TeenQuotes\Mail\MandrillServiceProvider', 'TeenQuotes\Newsletters\NewsletterListServiceProvider', 'TeenQuotes\Newsletters\NewslettersServiceProvider', 'TeenQuotes\Notifiers\AdminNotifierServiceProvider', @@ -228,7 +227,6 @@ 'Log' => 'Illuminate\Support\Facades\Log', 'Mail' => 'Illuminate\Support\Facades\Mail', 'MailSwitcher' => 'TeenQuotes\Mail\MailSwitcher', - 'MandrillClient' => 'TeenQuotes\Mail\Facades\MandrillClient', 'Paginator' => 'Illuminate\Support\Facades\Paginator', 'Password' => 'Illuminate\Support\Facades\Password', 'Queue' => 'Illuminate\Support\Facades\Queue', diff --git a/app/config/mail.php b/app/config/mail.php index 37a70fc1..b24dfe4b 100644 --- a/app/config/mail.php +++ b/app/config/mail.php @@ -24,7 +24,7 @@ | */ - 'driver' => 'mandrill', + 'driver' => 'mailgun', /* |-------------------------------------------------------------------------- diff --git a/composer.json b/composer.json index 960a8754..e24bcd46 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,6 @@ "laracasts/testdummy": "~1.0", "stojg/crop": "~1.0", "laracasts/validation": "~1.0", - "mandrill/mandrill": "~1.0", "mailchimp/mailchimp": "~2.0", "vlucas/phpdotenv": "~1.0", "iron-io/iron_mq": "~1.0",