Skip to content

Commit 4917105

Browse files
committed
Add email setup command to artisan
1 parent 16cfdf1 commit 4917105

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
namespace Pterodactyl\Console\Commands;
25+
26+
use Illuminate\Console\Command;
27+
28+
class UpdateEmailSettings extends Command
29+
{
30+
/**
31+
* The name and signature of the console command.
32+
*
33+
* @var string
34+
*/
35+
protected $signature = 'pterodactyl:mail';
36+
37+
/**
38+
* The console command description.
39+
*
40+
* @var string
41+
*/
42+
protected $description = 'Sets or updates email settings for the .env file.';
43+
44+
/**
45+
* Create a new command instance.
46+
*
47+
* @return void
48+
*/
49+
public function __construct()
50+
{
51+
parent::__construct();
52+
}
53+
54+
/**
55+
* Execute the console command.
56+
*
57+
* @return mixed
58+
*/
59+
public function handle()
60+
{
61+
$variables = [];
62+
$file = base_path() . '/.env';
63+
if (!file_exists($file)) {
64+
$this->error('Missing environment file! It appears that you have not installed this panel correctly.');
65+
exit();
66+
}
67+
68+
$envContents = file_get_contents($file);
69+
70+
$this->table([
71+
'Option',
72+
'Description'
73+
], [
74+
[
75+
'smtp',
76+
'SMTP Server Email'
77+
],
78+
[
79+
'mail',
80+
'PHP\'s Internal Mail Server'
81+
],
82+
[
83+
'mailgun',
84+
'Mailgun Email Service'
85+
],
86+
[
87+
'mandrill',
88+
'Mandrill Transactional Email Service'
89+
],
90+
[
91+
'postmark',
92+
'Postmark Transactional Email Service'
93+
]
94+
]);
95+
$variables['MAIL_DRIVER'] = $this->choice('Which email driver would you like to use?', [
96+
'smtp',
97+
'mail',
98+
'mailgun',
99+
'mandrill',
100+
'postmark'
101+
]);
102+
103+
switch ($variables['MAIL_DRIVER']) {
104+
case 'smtp':
105+
$variables['MAIL_HOST'] = $this->ask('SMTP Host (e.g smtp.google.com)');
106+
$variables['MAIL_PORT'] = $this->anticipate('SMTP Host Port (e.g 587)', ['587']);
107+
$variables['MAIL_USERNAME'] = $this->ask('SMTP Username');
108+
$variables['MAIL_PASSWORD'] = $this->secret('SMTP Password');
109+
break;
110+
case 'mail':
111+
break;
112+
case 'mailgun':
113+
$variables['MAILGUN_DOMAIN'] = $this->ask('Mailgun Domain');
114+
$variables['MAILGUN_KEY'] = $this->ask('Mailgun Key');
115+
break;
116+
case 'mandrill':
117+
$variables['MANDRILL_SECRET'] = $this->ask('Mandrill Secret');
118+
break;
119+
case 'postmark':
120+
$variables['MAIL_HOST'] = 'smtp.postmarkapp.com';
121+
$variables['MAIL_PORT'] = 587;
122+
$variables['MAIL_USERNAME'] = $this->ask('Postmark API Token');
123+
$variables['MAIL_PASSWORD'] = $variables['MAIL_USERNAME'];
124+
break;
125+
default:
126+
$this->error('No email service was defined!');
127+
exit();
128+
break;
129+
}
130+
131+
$variables['MAIL_FROM'] = $this->ask('Email address emails should originate from');
132+
$variables['MAIL_ENCRYPTION'] = 'tls';
133+
134+
$bar = $this->output->createProgressBar(count($variables));
135+
136+
$this->line('Writing new email environment configuration to file.');
137+
foreach ($variables as $key => $value) {
138+
$newValue = $key . '=' . $value;
139+
140+
if (preg_match_all('/^' . $key . '=(.*)$/m', $envContents) < 1) {
141+
$envContents = $envContents . "\n" . $newValue;
142+
} else {
143+
$envContents = preg_replace('/^' . $key . '=(.*)$/m', $newValue, $envContents);
144+
}
145+
$bar->advance();
146+
}
147+
148+
file_put_contents($file, $envContents);
149+
$bar->finish();
150+
echo "\n";
151+
}
152+
}

app/Console/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Kernel extends ConsoleKernel
1919
\Pterodactyl\Console\Commands\UpdateEnvironment::class,
2020
\Pterodactyl\Console\Commands\RunTasks::class,
2121
\Pterodactyl\Console\Commands\ClearServices::class,
22+
\Pterodactyl\Console\Commands\UpdateEmailSettings::class,
2223
];
2324

2425
/**

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"rm Homestead.yaml.bak",
7676
"php artisan key:generate",
7777
"php artisan pterodactyl:env",
78+
"php artisan pterodactyl:mail",
7879
"php artisan migrate",
7980
"php artisan db:seed",
8081
"php artisan pterodactyl:user"
@@ -84,6 +85,7 @@
8485
"php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
8586
"php artisan key:generate",
8687
"php artisan pterodactyl:env",
88+
"php artisan pterodactyl:mail",
8789
"php artisan migrate",
8890
"php artisan db:seed",
8991
"php artisan pterodactyl:user"

0 commit comments

Comments
 (0)