From 90ff7833a2d739346accc418ba71d3a12e63129c Mon Sep 17 00:00:00 2001 From: DariusIII Date: Mon, 2 Oct 2017 12:39:04 +0200 Subject: [PATCH] Use laravel Mail facade, remove phpmailer, update seeting.example.php accordingly, remove Utility sendEmail function --- Changelog | 2 + app/Mail/AccountChange.php | 37 ++++ app/Mail/ContactUs.php | 39 +++++ app/Mail/ForgottenPassword.php | 41 +++++ app/Mail/PasswordReset.php | 41 +++++ app/Mail/SendInvite.php | 40 +++++ composer.json | 1 - composer.lock | 87 +-------- nntmux/Users.php | 30 ++-- nntmux/config/Configure.php | 128 +++++++------- nntmux/config/settings.example.php | 165 +++--------------- nntmux/utility/Utility.php | 104 ----------- public/admin/user-edit.php | 6 +- public/pages/contact-us.php | 13 +- public/pages/forgottenpassword.php | 129 +++++++------- .../views/emails/accountChange.blade.php | 13 ++ resources/views/emails/contactUs.blade.php | 9 + resources/views/emails/email_layout.blade.php | 49 ++++++ .../views/emails/forgottenPassword.blade.php | 14 ++ .../views/emails/passwordReset.blade.php | 13 ++ resources/views/emails/sendinvite.blade.php | 15 ++ 21 files changed, 492 insertions(+), 484 deletions(-) create mode 100644 app/Mail/AccountChange.php create mode 100644 app/Mail/ContactUs.php create mode 100644 app/Mail/ForgottenPassword.php create mode 100644 app/Mail/PasswordReset.php create mode 100644 app/Mail/SendInvite.php create mode 100644 resources/views/emails/accountChange.blade.php create mode 100644 resources/views/emails/contactUs.blade.php create mode 100644 resources/views/emails/email_layout.blade.php create mode 100644 resources/views/emails/forgottenPassword.blade.php create mode 100644 resources/views/emails/passwordReset.blade.php create mode 100644 resources/views/emails/sendinvite.blade.php diff --git a/Changelog b/Changelog index ccfbc5170..7c9ccdcf1 100755 --- a/Changelog +++ b/Changelog @@ -1,3 +1,5 @@ +2017-10-02 DariusIII + * Chg: Use laravel Mail facade, remove phpmailer, update seeting.example.php accordingly, remove Utility sendEmail function 2017-10-01 DariusIII * Chg: Update swiftmailer to latest version * Chg: Update cs of SABnzbd class diff --git a/app/Mail/AccountChange.php b/app/Mail/AccountChange.php new file mode 100644 index 000000000..2a106fcc2 --- /dev/null +++ b/app/Mail/AccountChange.php @@ -0,0 +1,37 @@ +user = $user; + } + + /** + * Build the message. + * + * @return $this + * @throws \Exception + */ + public function build() + { + return $this->from(Settings::settingValue('site.main.email'))->subject('Account Changed')->view('emails.accountChange')->with(['account' => $this->user->role->name, 'username' => $this->user->username, 'site' => Settings::settingValue('site.main.title'),]); + } +} diff --git a/app/Mail/ContactUs.php b/app/Mail/ContactUs.php new file mode 100644 index 000000000..d9ea0b513 --- /dev/null +++ b/app/Mail/ContactUs.php @@ -0,0 +1,39 @@ +mailFrom = $mailFrom; + $this->mailBody = $mailBody; + } + + /** + * Build the message. + * + * @return $this + * @throws \Exception + */ + public function build() + { + return $this->from($this->mailFrom)->subject('Contact form submitted')->view('emails.contactUs')->with(['mailBody' => $this->mailBody,]); + } +} diff --git a/app/Mail/ForgottenPassword.php b/app/Mail/ForgottenPassword.php new file mode 100644 index 000000000..5b64abfc3 --- /dev/null +++ b/app/Mail/ForgottenPassword.php @@ -0,0 +1,41 @@ +user = User::query()->where('id', $userId)->first(); + $this->resetLink = $resetLink; + } + + /** + * Build the message. + * + * @return $this + * @throws \Exception + */ + public function build() + { + return $this->from(Settings::settingValue('site.main.email'))->subject('Forgotten password reset')->view('emails.forgottenPassword')->with(['resetLink' => $this->resetLink, 'site' => Settings::settingValue('site.main.title'),]); + } +} diff --git a/app/Mail/PasswordReset.php b/app/Mail/PasswordReset.php new file mode 100644 index 000000000..2d5e98f9b --- /dev/null +++ b/app/Mail/PasswordReset.php @@ -0,0 +1,41 @@ +user = User::query()->where('id', $userId)->first(); + $this->newPass = $newPass; + } + + /** + * Build the message. + * + * @return $this + * @throws \Exception + */ + public function build() + { + return $this->from(Settings::settingValue('site.main.email'))->subject('Password reset')->view('emails.forgottenPassword')->with(['newPass' => $this->newPass, 'userName' => $this->user->username, 'site' => Settings::settingValue('site.main.title'),]); + } +} diff --git a/app/Mail/SendInvite.php b/app/Mail/SendInvite.php new file mode 100644 index 000000000..6fea3e567 --- /dev/null +++ b/app/Mail/SendInvite.php @@ -0,0 +1,40 @@ +user = User::query()->where('id', $userId)->first(); + $this->invite = $invite; + } + + /** + * Build the message. + * + * @return $this + * @throws \Exception + */ + public function build() + { + return $this->from(Settings::settingValue('site.main.email'))->subject('Invite received')->view('emails.sendinvite')->with(['invite' => $this->invite, 'username' => $this->user->username, 'site' => Settings::settingValue('site.main.title'), 'email' => $this->user->email]); + } +} diff --git a/composer.json b/composer.json index 3cafea37c..e5c31d58b 100755 --- a/composer.json +++ b/composer.json @@ -137,7 +137,6 @@ "laravel/tinker": "~1.0", "monolog/monolog": "^1.22", "php-tmdb/api": "~2.1", - "phpmailer/phpmailer": "~5.2", "predis/predis": "^1.1", "ramsey/uuid": "^3.7", "roave/security-advisories": "dev-master", diff --git a/composer.lock b/composer.lock index d5fe65d17..042a60c9e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "07c3e07213202de2314c65983fe0d681", + "content-hash": "94196be2e996be2788d268ca1f0b4fce", "packages": [ { "name": "adrenth/thetvdb2", @@ -139,7 +139,7 @@ } ], "description": "PHP Wrapper for Accessing the Steam Storefront API", - "time": "2017-08-27 15:14:06" + "time": "2017-08-27T15:14:06+00:00" }, { "name": "barracudanetworks/forkdaemon-php", @@ -1473,7 +1473,7 @@ } ], "description": "A PHP library that acts as a wrapper for the GiantBomb API.", - "time": "2017-05-29 12:05:57" + "time": "2017-05-29T12:05:57+00:00" }, { "name": "dnoegel/php-xdg-base-dir", @@ -2633,7 +2633,7 @@ } ], "description": "TVMaze-API-Wrapper", - "time": "2016-05-04 01:31:56" + "time": "2016-05-04T01:31:56+00:00" }, { "name": "kbjr/Git.php", @@ -2659,7 +2659,7 @@ "source": "https://github.com/kbjr/Git.php/tree/master", "issues": "https://github.com/kbjr/Git.php/issues" }, - "time": "2015-03-31 14:10:29" + "time": "2015-03-31T14:10:29+00:00" }, { "name": "kevinrob/guzzle-cache-middleware", @@ -3359,83 +3359,6 @@ ], "time": "2017-09-09T13:44:00+00:00" }, - { - "name": "phpmailer/phpmailer", - "version": "v5.2.25", - "source": { - "type": "git", - "url": "https://github.com/PHPMailer/PHPMailer.git", - "reference": "2baf20b01690fba8cf720c1ebcf9b988eda50915" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/2baf20b01690fba8cf720c1ebcf9b988eda50915", - "reference": "2baf20b01690fba8cf720c1ebcf9b988eda50915", - "shasum": "" - }, - "require": { - "ext-ctype": "*", - "php": ">=5.0.0" - }, - "require-dev": { - "doctrine/annotations": "1.2.*", - "jms/serializer": "0.16.*", - "phpdocumentor/phpdocumentor": "2.*", - "phpunit/phpunit": "4.8.*", - "symfony/debug": "2.8.*", - "symfony/filesystem": "2.8.*", - "symfony/translation": "2.8.*", - "symfony/yaml": "2.8.*", - "zendframework/zend-cache": "2.5.1", - "zendframework/zend-config": "2.5.1", - "zendframework/zend-eventmanager": "2.5.1", - "zendframework/zend-filter": "2.5.1", - "zendframework/zend-i18n": "2.5.1", - "zendframework/zend-json": "2.5.1", - "zendframework/zend-math": "2.5.1", - "zendframework/zend-serializer": "2.5.*", - "zendframework/zend-servicemanager": "2.5.*", - "zendframework/zend-stdlib": "2.5.1" - }, - "suggest": { - "league/oauth2-google": "Needed for Google XOAUTH2 authentication" - }, - "type": "library", - "autoload": { - "classmap": [ - "class.phpmailer.php", - "class.phpmaileroauth.php", - "class.phpmaileroauthgoogle.php", - "class.smtp.php", - "class.pop3.php", - "extras/EasyPeasyICS.php", - "extras/ntlm_sasl_client.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "LGPL-2.1" - ], - "authors": [ - { - "name": "Jim Jagielski", - "email": "jimjag@gmail.com" - }, - { - "name": "Marcus Bointon", - "email": "phpmailer@synchromedia.co.uk" - }, - { - "name": "Andy Prevost", - "email": "codeworxtech@users.sourceforge.net" - }, - { - "name": "Brent R. Matzelle" - } - ], - "description": "PHPMailer is a full-featured email creation and transfer class for PHP", - "time": "2017-08-28T11:12:07+00:00" - }, { "name": "predis/predis", "version": "v1.1.1", diff --git a/nntmux/Users.php b/nntmux/Users.php index d940819c9..286a17d37 100755 --- a/nntmux/Users.php +++ b/nntmux/Users.php @@ -2,6 +2,9 @@ namespace nntmux; +use App\Mail\AccountChange; +use App\Mail\SendInvite; +use Illuminate\Support\Facades\Mail; use nntmux\db\DB; use Carbon\Carbon; use App\Models\User; @@ -461,18 +464,14 @@ class Users } /** - * @param $msgsubject - * @param $msgbody - * * @return int - * @throws \Exception */ - public function updateExpiredRoles($msgsubject, $msgbody): int + public function updateExpiredRoles(): int { - $data = User::query()->whereDate('rolechangedate', '<', Carbon::now())->select(['id', 'email'])->get(); + $data = User::query()->whereDate('rolechangedate', '<', Carbon::now())->get(); foreach ($data as $u) { - Utility::sendEmail($u['email'], $msgsubject, $msgbody, Settings::settingValue('site.main.email')); + Mail::to($u['email'])->send(new AccountChange($u)); User::query()->where('id', $u['id'])->update(['user_roles_id' => self::ROLE_USER, 'rolechangedate' => null]); } @@ -1081,24 +1080,17 @@ class Users } /** - * @param $sitetitle - * @param $siteemail - * @param $serverurl + * @param $serverUrl * @param $uid - * @param $emailto - * + * @param $emailTo * @return string - * @throws \Exception */ - public function sendInvite($sitetitle, $siteemail, $serverurl, $uid, $emailto): string + public function sendInvite($serverUrl, $uid, $emailTo): string { - $sender = $this->getById($uid); $token = self::hashSHA1(uniqid('', true)); - $subject = $sitetitle.' Invitation'; - $url = $serverurl.'register?invitecode='.$token; - $contents = $sender['username'].' has sent an invite to join '.$sitetitle.' to this email address. To accept the invitation click the following link. '.$url; + $url = $serverUrl.'register?invitecode='.$token; - Utility::sendEmail($emailto, $subject, $contents, $siteemail); + Mail::to($emailTo)->send(new SendInvite($uid, $url)); $this->addInvite($uid, $token); return $url; diff --git a/nntmux/config/Configure.php b/nntmux/config/Configure.php index d7eddae36..88986f5e3 100755 --- a/nntmux/config/Configure.php +++ b/nntmux/config/Configure.php @@ -24,19 +24,19 @@ namespace nntmux\config; class Configure { private static $environments = [ - 'indexer' => [ - '.env' => true, - 'settings' => false, - ], - 'install' => [ - '.env' => true, - 'settings' => false, - ], - 'smarty' => [ - '.env' => true, - 'settings' => false, - ], - ]; + 'indexer' => [ + '.env' => true, + 'settings' => false, + ], + 'install' => [ + '.env' => true, + 'settings' => false, + ], + 'smarty' => [ + '.env' => true, + 'settings' => false, + ], + ]; /** * Configure constructor. @@ -82,63 +82,63 @@ class Configure if (! file_exists($file) && $throwException) { $errorCode = (int) ($filename === '.env'); throw new \RuntimeException( - "Unable to load configuration file '$file'. Make sure it has been created and contains correct settings.", - $errorCode - ); + "Unable to load configuration file '$file'. Make sure it has been created and contains correct settings.", + $errorCode + ); } if ($file !== NN_ROOT.'.env' && file_exists($file)) { require_once $file; } switch ($filename) { - case '.env': - $this->defaultSSL(); - break; - case 'settings': - $settings_file = NN_CONFIGS.'settings.php'; - if (is_file($settings_file)) { - require_once $settings_file; - if (PHP_SAPI === 'cli') { - $current_settings_file_version = 4; // Update this when updating settings.example.php - if (! defined('NN_SETTINGS_FILE_VERSION') || - NN_SETTINGS_FILE_VERSION != $current_settings_file_version - ) { - echo "\033[0;31mNotice: Your $settings_file file is either out of date or you have not updated". - " NN_SETTINGS_FILE_VERSION to $current_settings_file_version in that file.\033[0m". - PHP_EOL; - } - unset($current_settings_file_version); - } - } elseif (! defined('ITEMS_PER_PAGE')) { - define('ITEMS_PER_PAGE', '50'); - define('ITEMS_PER_COVER_PAGE', '20'); - define('NN_ECHOCLI', true); - define('NN_DEBUG', false); - define('NN_LOGGING', false); - define('NN_LOGINFO', false); - define('NN_LOGNOTICE', false); - define('NN_LOGWARNING', false); - define('NN_LOGERROR', false); - define('NN_LOGFATAL', false); - define('NN_LOGQUERIES', false); - define('NN_LOGAUTOLOADER', false); - define('NN_QUERY_STRIP_WHITESPACE', false); - define('NN_RENAME_PAR2', true); - define('NN_RENAME_MUSIC_MEDIAINFO', true); - define('NN_CACHE_EXPIRY_SHORT', 300); - define('NN_CACHE_EXPIRY_MEDIUM', 600); - define('NN_CACHE_EXPIRY_LONG', 900); - define('NN_PREINFO_OPEN', false); - define('NN_FLOOD_CHECK', false); - define('NN_FLOOD_WAIT_TIME', 5); - define('NN_FLOOD_MAX_REQUESTS_PER_SECOND', 5); - define('NN_USE_SQL_TRANSACTIONS', true); - define('NN_RELEASE_SEARCH_TYPE', 0); - define('NN_MAX_PAGER_RESULTS', '125000'); - } - unset($settings_file); - break; - } + case '.env': + $this->defaultSSL(); + break; + case 'settings': + $settings_file = NN_CONFIGS.'settings.php'; + if (is_file($settings_file)) { + require_once $settings_file; + if (PHP_SAPI === 'cli') { + $current_settings_file_version = 5; // Update this when updating settings.example.php + if (! defined('NN_SETTINGS_FILE_VERSION') || + NN_SETTINGS_FILE_VERSION !== $current_settings_file_version + ) { + echo "\033[0;31mNotice: Your $settings_file file is either out of date or you have not updated". + " NN_SETTINGS_FILE_VERSION to $current_settings_file_version in that file.\033[0m". + PHP_EOL; + } + unset($current_settings_file_version); + } + } elseif (! defined('ITEMS_PER_PAGE')) { + define('ITEMS_PER_PAGE', '50'); + define('ITEMS_PER_COVER_PAGE', '20'); + define('NN_ECHOCLI', true); + define('NN_DEBUG', false); + define('NN_LOGGING', false); + define('NN_LOGINFO', false); + define('NN_LOGNOTICE', false); + define('NN_LOGWARNING', false); + define('NN_LOGERROR', false); + define('NN_LOGFATAL', false); + define('NN_LOGQUERIES', false); + define('NN_LOGAUTOLOADER', false); + define('NN_QUERY_STRIP_WHITESPACE', false); + define('NN_RENAME_PAR2', true); + define('NN_RENAME_MUSIC_MEDIAINFO', true); + define('NN_CACHE_EXPIRY_SHORT', 300); + define('NN_CACHE_EXPIRY_MEDIUM', 600); + define('NN_CACHE_EXPIRY_LONG', 900); + define('NN_PREINFO_OPEN', false); + define('NN_FLOOD_CHECK', false); + define('NN_FLOOD_WAIT_TIME', 5); + define('NN_FLOOD_MAX_REQUESTS_PER_SECOND', 5); + define('NN_USE_SQL_TRANSACTIONS', true); + define('NN_RELEASE_SEARCH_TYPE', 0); + define('NN_MAX_PAGER_RESULTS', '125000'); + } + unset($settings_file); + break; + } } private function defaultSSL() diff --git a/nntmux/config/settings.example.php b/nntmux/config/settings.example.php index 001b84f7d..ea494c69f 100755 --- a/nntmux/config/settings.example.php +++ b/nntmux/config/settings.example.php @@ -18,9 +18,9 @@ use nntmux\utility\Utility; * * @note Developers: When updating settings.example.php, up this version * and $current_settings_file_version in nntmux\config\Configure.php - * @version 4 + * @version 5 */ -define('NN_SETTINGS_FILE_VERSION', 4); +define('NN_SETTINGS_FILE_VERSION', 5); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////// Web Settings ////////////////////////////////////////////////////////// @@ -183,13 +183,13 @@ define('NN_CACHE_TYPE', 0); * http://php.net/manual/en/memcached.addserver.php */ define('NN_CACHE_HOSTS', serialize( - [ - 'Server1' => [ - 'host' => '127.0.0.1', - 'port' => 11211, - 'weight' => 0, - ], - ] + [ + 'Server1' => [ + 'host' => '127.0.0.1', + 'port' => 11211, + 'weight' => 0, + ], + ] )); /* @@ -424,128 +424,17 @@ define('NN_SQL_DELETE_LOW_PRIORITY', false); */ define('NN_SQL_DELETE_QUICK', false); -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -////////////////////////////////////////////// PHPMailer Settings ////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -/* - * Simple constant to let us know this file is included and we should use PHPMailer library. - * Uncomment the line below after setting the other constants. - */ -define('PHPMAILER_ENABLED', false); - -/* - * Global "From" Address. - * This address will be set as the From: address on every email sent by NN. - * - * @example 'noreply@example.com' - * @note Depending on server configurations, it may not respect this value. - * @default '' (uses the contact email configured in 'Edit Site' settings) - */ -define('PHPMAILER_FROM_EMAIL', ''); - -/* - * Global "From" Name. - * Along with the email above, this will display as the name. - * - * @example 'KingCat' - * @note Depending on server configurations, it may not respect this value. - * @default '' (uses the site title configured in 'Edit Site' settings) - */ -define('PHPMAILER_FROM_NAME', ''); - -/* - * Global "Reply-to" Address. - * This address will be set as the Reply-to: address on every email sent by NN. - * - * @example 'support@example.com' - * @note It's a good idea to set this to your support email account (if possible) - * @default '' (uses the contact email configured in 'Edit Site' settings) - */ -define('PHPMAILER_REPLYTO', ''); - -/* - * Always BCC. - * This email address will be blind carbon copied on every email sent from this site. - * - * @note This has very specific uses, don't enable unless you're sure you want to get the deluge. - * @default '' - */ -define('PHPMAILER_BCC', ''); - -/* - * Should we use a SMTP server to send mail? - * If false, it will use your default settings from php.ini. - * - * @note If set to true, be sure to set the server settings below. - * @default false - */ -define('PHPMAILER_USE_SMTP', false); - -/********************************************************************************* - * The following options require PHPMAILER_USE_SMTP to be true: * - *********************************************************************************/ - -/* - * This is the hostname to use if connecting to a SMTP server. - * - * @note You can specify main and backup hosts, delimit with a semicolon. (i.e. 'main.host.com;backup.host.com') - * @default '' - */ -define('PHPMAILER_SMTP_HOST', ''); - -/* - * TLS & SSL Support for your SMTP server. - * - * @note Possible values: false, 'tls', 'ssl' - * @default 'tls' - */ -define('PHPMAILER_SMTP_SECURE', 'tls'); - -/* - * SMTP Port - * - * @note Usually this is 25, 465, or 587 - * @default 587 - */ -define('PHPMAILER_SMTP_PORT', 587); - -/* - * Does your SMTP host require authentication? - * - * @note Be sure to set credentials below if changing to true. - * @default false - */ -define('PHPMAILER_SMTP_AUTH', false); - -/********************************************************************************* - * The following options require both PHPMAILER_USE_SMTP & PHPMAILER_SMTP_AUTH to be true: * - *********************************************************************************/ - -/* - * SMTP username for authentication. - * - * @default '' - */ -define('PHPMAILER_SMTP_USER', ''); - -/* - * SMTP password for authentication. - * - * @default '' - */ -define('PHPMAILER_SMTP_PASSWORD', ''); - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////// PHP CLI Settings /////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (Utility::isCLI()) { - /* - * Your server's local timezone. - * @note Uncomment to enable. - * @see https://secure.php.net/manual/en/timezones.php - * @version 4 - */ + /* + * Your server's local timezone. + * @note Uncomment to enable. + * @see https://secure.php.net/manual/en/timezones.php + * @version 4 + */ //ini_set('date.timezone', 'America/New_York'); /* @@ -623,12 +512,12 @@ if (Utility::isCLI()) { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// } else { - /* - * Your server's local timezone. - * @note Uncomment to enable. - * @see https://secure.php.net/manual/en/timezones.php - * @version 4 - */ + /* + * Your server's local timezone. + * @note Uncomment to enable. + * @see https://secure.php.net/manual/en/timezones.php + * @version 4 + */ //ini_set('date.timezone', 'America/New_York'); /* @@ -714,12 +603,12 @@ if (Utility::isCLI()) { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (extension_loaded('xdebug')) { - /* - * Display colors on xdebug CLI output? - * 0 - off, 1 - on only if on a TTY with ansi support, 2 - on regardless of TTY or ansi support. - * @default 0 - * @version 4 - */ + /* + * Display colors on xdebug CLI output? + * 0 - off, 1 - on only if on a TTY with ansi support, 2 - on regardless of TTY or ansi support. + * @default 0 + * @version 4 + */ ini_set('xdebug.cli_color', '0'); /* diff --git a/nntmux/utility/Utility.php b/nntmux/utility/Utility.php index 4367c7f69..f018ca806 100755 --- a/nntmux/utility/Utility.php +++ b/nntmux/utility/Utility.php @@ -3,7 +3,6 @@ namespace nntmux\utility; use nntmux\db\DB; -use nntmux\Logger; use nntmux\ColorCLI; use Ramsey\Uuid\Uuid; use App\Models\Settings; @@ -704,109 +703,6 @@ class Utility ]; } - // Central function for sending site email. - - /** - * @param $to - * @param $subject - * @param $contents - * @param $from - * - * @return bool - * @throws \nntmux\LoggerException - * @throws \InvalidArgumentException - * @throws \Exception - * @throws \phpmailerException - */ - public static function sendEmail($to, $subject, $contents, $from): bool - { - //Setup the body first since we need it regardless of sending method. - $eol = PHP_EOL; - - $body = ''.$eol; - $body .= ''.$eol; - $body .= $contents; - $body .= ''.$eol; - $body .= ''.$eol; - - if (PHPMAILER_ENABLED === true) { - $mail = new \PHPMailer; - - // Check to make sure the user has their settings correct. - if (PHPMAILER_USE_SMTP === true) { - if ((! defined('PHPMAILER_SMTP_HOST') || PHPMAILER_SMTP_HOST === '') || - (! defined('PHPMAILER_SMTP_PORT') || PHPMAILER_SMTP_PORT === '') - ) { - throw new \phpmailerException( - 'You opted to use SMTP but the PHPMAILER_SMTP_HOST and/or PHPMAILER_SMTP_PORT is/are not defined correctly! Either fix the missing/incorrect values or change PHPMAILER_USE_SMTP to false in the www/settings.php' - ); - } - - // If the user enabled SMTP & Auth but did not setup credentials, throw an exception. - if (defined('PHPMAILER_SMTP_AUTH') && PHPMAILER_SMTP_AUTH === true) { - if ((! defined('PHPMAILER_SMTP_USER') || PHPMAILER_SMTP_USER === '') || - (! defined('PHPMAILER_SMTP_PASSWORD') || PHPMAILER_SMTP_PASSWORD === '') - ) { - throw new \phpmailerException( - 'You opted to use SMTP and SMTP Auth but the PHPMAILER_SMTP_USER and/or PHPMAILER_SMTP_PASSWORD is/are not defined correctly. Please set them in www/settings.php' - ); - } - } - } - - //Finally we can send the mail. - $mail->isHTML(true); - - if (PHPMAILER_USE_SMTP) { - $mail->isSMTP(); - - $mail->Host = PHPMAILER_SMTP_HOST; - $mail->Port = PHPMAILER_SMTP_PORT; - - $mail->SMTPSecure = PHPMAILER_SMTP_SECURE; - - if (PHPMAILER_SMTP_AUTH) { - $mail->SMTPAuth = true; - $mail->Username = PHPMAILER_SMTP_USER; - $mail->Password = PHPMAILER_SMTP_PASSWORD; - } - } - - $fromEmail = (PHPMAILER_FROM_EMAIL === '') ? Settings::settingValue('site.main.email') : PHPMAILER_FROM_EMAIL; - $fromName = (PHPMAILER_FROM_NAME === '') ? Settings::settingValue('site.main.title') : PHPMAILER_FROM_NAME; - $replyTo = (PHPMAILER_REPLYTO === '') ? $from : PHPMAILER_REPLYTO; - - (PHPMAILER_BCC !== '') ? $mail->addBCC(PHPMAILER_BCC) : null; - - $mail->setFrom($fromEmail, $fromName); - $mail->addAddress($to); - $mail->addReplyTo($replyTo); - $mail->Subject = $subject; - $mail->Body = $body; - $mail->AltBody = $mail->html2text($body, true); - - $sent = $mail->send(); - - if (! $sent) { - (new Logger())->log(__CLASS__, __FUNCTION__, $mail->ErrorInfo, Logger::LOG_ERROR); - throw new \phpmailerException('Unable to send mail. Error: '.$mail->ErrorInfo); - } - - return $sent; - } - - // We don't use PHPMAILER so send the email using PHP mail function - $headers = 'From: '.$from.$eol; - $headers .= 'Reply-To: '.$from.$eol; - $headers .= 'Return-Path: '.$from.$eol; - $headers .= 'X-Mailer: newznab'.$eol; - $headers .= 'MIME-Version: 1.0'.$eol; - $headers .= 'Content-type: text/html; charset=iso-8859-1'.$eol; - $headers .= $eol; - - return mail($to, $subject, $body, $headers); - } - /** * Return file type/info using magic numbers. * Try using `file` program where available, fallback to using PHP's finfo class. diff --git a/public/admin/user-edit.php b/public/admin/user-edit.php index dc7c4b629..4f6e8abae 100644 --- a/public/admin/user-edit.php +++ b/public/admin/user-edit.php @@ -2,10 +2,10 @@ require_once dirname(__DIR__).DIRECTORY_SEPARATOR.'smarty.php'; +use App\Mail\AccountChange; +use Illuminate\Support\Facades\Mail; use nntmux\Users; -use App\Models\Settings; use App\Models\UserRole; -use nntmux\utility\Utility; $page = new AdminPage(); $users = new Users(); @@ -71,7 +71,7 @@ switch ($action) { if ($_POST['role'] !== '') { $newRole = UserRole::query()->where('id', $_POST['role'])->value('name'); $email = $_POST['email'] ?? $_GET['email']; - Utility::sendEmail($email, 'Account changed', 'Your account role has been changed to '.$newRole, Settings::settingValue('site.main.email')); + Mail::to($email)->send(new AccountChange($_POST['id'])); } } diff --git a/public/pages/contact-us.php b/public/pages/contact-us.php index 9efa21f65..87c14bf61 100644 --- a/public/pages/contact-us.php +++ b/public/pages/contact-us.php @@ -1,8 +1,9 @@ getError() === false) { $email = $_POST['useremail']; - $mailto = Settings::settingValue('site.main.email'); - $mailsubj = 'Contact Form Submitted'; - $mailhead = "From: $email\n"; - $mailbody = "Values submitted from contact form:\n"; + $mailTo = Settings::settingValue('site.main.email'); + $mailBody = "Values submitted from contact form:\n"; foreach ($_POST as $key => $value) { if ($key !== 'submit') { - $mailbody .= "$key : $value
\r\n"; + $mailBody .= "$key : $value
\r\n"; } } if (! preg_match("/\n/i", $_POST['useremail'])) { - Utility::sendEmail($mailto, $mailsubj, $mailbody, $email); + Mail::to($mailTo)->send(new ContactUs($email, $mailBody)); } $msg = "

Thank you for getting in touch with ".Settings::settingValue('site.main.title').'.

'; } diff --git a/public/pages/forgottenpassword.php b/public/pages/forgottenpassword.php index 260fe736a..5db4c55d9 100644 --- a/public/pages/forgottenpassword.php +++ b/public/pages/forgottenpassword.php @@ -1,8 +1,9 @@ users->isLoggedIn()) { header('Location: '.WWW_TOP.'/'); @@ -14,76 +15,72 @@ $captcha = new Captcha($page); $email = $sent = $confirmed = ''; switch ($action) { - case 'reset': - if (! isset($_REQUEST['guid'])) { - $page->smarty->assign('error', 'No reset code provided.'); - break; - } + case 'reset': + if (! isset($_REQUEST['guid'])) { + $page->smarty->assign('error', 'No reset code provided.'); + break; + } - $ret = $page->users->getByPassResetGuid($_REQUEST['guid']); - if (! $ret) { - $page->smarty->assign('error', 'Bad reset code provided.'); - break; - } else { - // - // reset the password, inform the user, send out the email - // - $page->users->updatePassResetGuid($ret['id'], ''); - $newpass = $page->users->generatePassword(); - $page->users->updatePassword($ret['id'], $newpass); + $ret = $page->users->getByPassResetGuid($_REQUEST['guid']); + if (! $ret) { + $page->smarty->assign('error', 'Bad reset code provided.'); + break; + } - $to = $ret['email']; - $subject = Settings::settingValue('site.main.title').' Password Reset'; - $contents = 'Your password has been reset to '.$newpass; - $onscreen = 'Your password has been reset to '.$newpass.' and sent to your e-mail address.'; - Utility::sendEmail($to, $subject, $contents, Settings::settingValue('site.main.email')); - $page->smarty->assign('notice', $onscreen); - $confirmed = true; - break; - } + // + // reset the password, inform the user, send out the email + // + $page->users->updatePassResetGuid($ret['id'], ''); + $newpass = $page->users->generatePassword(); + $page->users->updatePassword($ret['id'], $newpass); - break; - case 'submit': + $to = $ret['email']; + $onscreen = 'Your password has been reset to '.$newpass.' and sent to your e-mail address.'; + Mail::to($to)->send(new PasswordReset($ret['id'], $newpass)); + $page->smarty->assign('notice', $onscreen); + $confirmed = true; + break; - if ($captcha->getError() === false) { - $email = $_POST['email'] ?? ''; - if (empty($email)) { - $page->smarty->assign('error', 'Missing Email'); - } else { - // - // Check users exists and send an email - // - $ret = $page->users->getByEmail($email); - if (! $ret) { - $page->smarty->assign('error', 'The email address is not recognised.'); - $sent = true; - break; - } else { - // - // Generate a forgottenpassword guid, store it in the user table - // - $guid = md5(uniqid('', false)); - $page->users->updatePassResetGuid($ret['id'], $guid); + break; + case 'submit': - // - // Send the email - // - $to = $ret['email']; - $subject = Settings::settingValue('site.main.title').' Forgotten Password Request'; - $contents = 'Someone has requested a password reset for this email address. To reset the password use the following link. '.PHP_EOL.PHP_EOL.$page->serverurl.'forgottenpassword?action=reset&guid='.$guid; - Utility::sendEmail($to, $subject, $contents, Settings::settingValue('site.main.email')); - $sent = true; - break; - } - } - break; - } + if ($captcha->getError() === false) { + $email = $_POST['email'] ?? ''; + if (empty($email)) { + $page->smarty->assign('error', 'Missing Email'); + } else { + // + // Check users exists and send an email + // + $ret = $page->users->getByEmail($email); + if (! $ret) { + $page->smarty->assign('error', 'The email address is not recognised.'); + $sent = true; + break; + } + // + // Generate a forgottenpassword guid, store it in the user table + // + $guid = md5(uniqid('', false)); + $page->users->updatePassResetGuid($ret['id'], $guid); + // + // Send the email + // + $to = $ret['email']; + $resetLink = $page->serverurl.'forgottenpassword?action=reset&guid='.$guid; + Mail::to($to)->send(new ForgottenPassword($ret['id'], $resetLink)); + $sent = true; + break; + } + break; + } } -$page->smarty->assign([ - 'email' => $email, - 'confirmed' => $confirmed, - 'sent' => $sent, - ] +$page->smarty->assign( + [ + 'email' => $email, + 'confirmed' => $confirmed, + 'sent' => $sent, + ] ); $page->title = 'Forgotten Password'; diff --git a/resources/views/emails/accountChange.blade.php b/resources/views/emails/accountChange.blade.php new file mode 100644 index 000000000..e56114831 --- /dev/null +++ b/resources/views/emails/accountChange.blade.php @@ -0,0 +1,13 @@ +@extends('emails.email_layout') + +@section('title') + Your account level has been changed +@endsection + +@section('content') + Dear {{ $username }}, +
+ Your account has been changed to {{ $account }} +


+ Greetings from {{ $site }} +@endsection diff --git a/resources/views/emails/contactUs.blade.php b/resources/views/emails/contactUs.blade.php new file mode 100644 index 000000000..ec71d462f --- /dev/null +++ b/resources/views/emails/contactUs.blade.php @@ -0,0 +1,9 @@ +@extends('emails.email_layout') + +@section('title') + Contact form submitted +@endsection + +@section('content') + {{ $mailBody }} +@endsection diff --git a/resources/views/emails/email_layout.blade.php b/resources/views/emails/email_layout.blade.php new file mode 100644 index 000000000..73819be96 --- /dev/null +++ b/resources/views/emails/email_layout.blade.php @@ -0,0 +1,49 @@ + + + + + @yield('title') + + + + + + + +
+
+ @yield('content') +
+
+ + \ No newline at end of file diff --git a/resources/views/emails/forgottenPassword.blade.php b/resources/views/emails/forgottenPassword.blade.php new file mode 100644 index 000000000..ec5498ce3 --- /dev/null +++ b/resources/views/emails/forgottenPassword.blade.php @@ -0,0 +1,14 @@ +@extends('emails.email_layout') + +@section('title') + Forgotten Password +@endsection + +@section('content') + + Someone has requested a password reset for this email address. +
+ To reset the password use the following link: {{$resetLink}}
+


+ Greetings from {{ $site }} +@endsection diff --git a/resources/views/emails/passwordReset.blade.php b/resources/views/emails/passwordReset.blade.php new file mode 100644 index 000000000..92af3c262 --- /dev/null +++ b/resources/views/emails/passwordReset.blade.php @@ -0,0 +1,13 @@ +@extends('emails.email_layout') + +@section('title') + Forgotten Password +@endsection + +@section('content') + Dear {{ $userName }}, +
+ Your password has been reset to: {{ $newPass }} +


+ Greetings from {{ $site }} +@endsection diff --git a/resources/views/emails/sendinvite.blade.php b/resources/views/emails/sendinvite.blade.php new file mode 100644 index 000000000..2eb65f284 --- /dev/null +++ b/resources/views/emails/sendinvite.blade.php @@ -0,0 +1,15 @@ +@extends('emails.email_layout') + +@section('title') + You have received an invite +@endsection + +@section('content') + Dear {{ $email }}, +
+ you have received an invite from {{ $username }} to register on {{ $site }} +
+ To register click on following link: {{$invite}}
+


+ Greetings from {{ $site }} +@endsection