How to use database for mail settings in Laravel - php

I'd like to keep users away from editing configuration files, so I've made web interface in admin panel for setting up Mail server, username, password, port, encryption..
I was working well in Laravel 4.2, but now when the app has been rewritten into Laravel 5, an error occurs:
Class 'Settings' not found in <b>F:\htdocs\app\config\mail.php</b> on line <b>18</b><br />
For this purpose I've created a service provider, made a facade, put them in config/app.php, Settings::get('var')/Settings::set('var') work perfectly, but not for mail settings.
config/mail.php:
<?php return array(
'driver' => Settings::get('mail_driver'),
'host' => Settings::get('mail_host'),
'port' => Settings::get('mail_port'),
'from' => array('address' => Settings::get('mail_from_address'), 'name' => Settings::get('mail_from_name')),
'encryption' => Settings::get('mail_encryption'),
'username' => Settings::get('mail_username'),
'password' => Settings::get('mail_password'),
'sendmail' => Settings::get('mail_sendmail'),
'pretend' => false,
);
config/app.php:
'providers' => [
...
'App\Providers\SettingsServiceProvider',
...
'aliases' => [
...
'Settings' => 'App\Custom\Facades\Settings',
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Custom\Settings;
class SettingsServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->singleton('settings', function()
{
return new Settings;
});
}
}
<?php namespace App\Custom;
use App\Setting;
class Settings {
public function get($var) {
try{
$setting = Setting::first();
} catch(exception $e)
{
return false;
}
return $setting->$var;
}
public function set($var, $val) {
try{
$setting = Setting::first();
$setting->$var = $val;
$setting->save();
} catch(exception $e)
{
return false;
}
return true;
}
}
<?php
namespace App\Custom\Facades;
use Illuminate\Support\Facades\Facade;
class Settings extends Facade {
protected static function getFacadeAccessor() { return 'settings'; }
}
Any ideas how to implement Laravel mail settings using database?

To archive this I created CustomMailServiceProvider by extending Illuminate\Mail\MailServiceProvider so as to overwrite this method:
protected function registerSwiftTransport(){
$this->app['swift.transport'] = $this->app->share(function($app)
{
return new TransportManager($app);
});
}
Here is the complete solution
I created CustomMailServiceProvider.php in app\Providers
namespace App\Providers;
use Illuminate\Mail\MailServiceProvider;
use App\Customs\CustomTransportManager;
class CustomMailServiceProvider extends MailServiceProvider{
protected function registerSwiftTransport(){
$this->app['swift.transport'] = $this->app->share(function($app)
{
return new CustomTransportManager($app);
});
}
}
I created CustomTransportManager.php in app/customs directory -
NB: app/customs directory doesn't exist in default laravel 5 directory structure, I created it
namespace App\Customs;
use Illuminate\Mail\TransportManager;
use App\Models\Setting; //my models are located in app\models
class CustomTransportManager extends TransportManager {
/**
* Create a new manager instance.
*
* #param \Illuminate\Foundation\Application $app
* #return void
*/
public function __construct($app)
{
$this->app = $app;
if( $settings = Setting::all() ){
$this->app['config']['mail'] = [
'driver' => $settings->mail_driver,
'host' => $settings->mail_host,
'port' => $settings->mail_port,
'from' => [
'address' => $settings->mail_from_address,
'name' => $settings->mail_from_name
],
'encryption' => $settings->mail_encryption,
'username' => $settings->mail_username,
'password' => $settings->mail_password,
'sendmail' => $settings->mail_sendmail,
'pretend' => $settings->mail_pretend
];
}
}
}
And finally, I replaced 'Illuminate\Mail\MailServiceProvider', in config/app.php with 'App\Providers\CustomMailServiceProvider',

I have added
$this->app['config']['services'] = [
'mailgun' => [
'domain' => $settings->mailgun_domain,
'secret' => $settings->mailgun_secret,
]
];
to CustomTransportManager __construct() to include mailgun API credentials that I'm using as mailing service

I configured as mentioned, however got the following error. While I tried your code found that from Laravel 5.4 share method is deprecated and instead informed to use singleton.
Call to undefined method Illuminate\Foundation\Application::share()
here is the below method using singleton instead using share method:
protected function registerSwiftTransport(){
$this->app->singleton('swift.transport', function ($app){
return new CustomTransportManager($app);
});
}

#DigitLimit , method share() has been dropped since Laravel 5.4. I had to work-around this problem using other methods, and I am not sure they are perfect. Here is my registerSwiftTransport() method in CustomMailServiceProvider class.
Firstly, we need to determine if code is not executed while calling app through command line: "if(strpos(php_sapi_name(), 'cli') === false)". If we don't check that and don't prevent setting new params in this case, Artisan will throw us errors in command line. Secondly, we need to get settings from database somehow. I did it using my method getSettingValue(), where first argument is setting key, and second argument is default value if setting is not found. As you see, I assigned settings to $this->app['config']['mail'].
After that, I used singleton() method:
protected function registerSwiftTransport(){
if (strpos(php_sapi_name(), 'cli') === false) {
$this->app['config']['mail'] = [
'driver' => Setting::getSettingValue('mail_driver', '****'),
'host' => Setting::getSettingValue('mail_host', '****'),
'port' => Setting::getSettingValue('mail_port', 25),
'from' => [
'address' => Setting::getSettingValue('mail_from_address', '****'),
'name' => Setting::getSettingValue('mail_from_name', '****'),
],
'encryption' => Setting::getSettingValue('mail_encryption', '***'),
'username' => Setting::getSettingValue('mail_username', '****'),
'password' => Setting::getSettingValue('mail_password', '****'),
];
}
$this->app->singleton('swift.transport', function ($app) {
return new Illuminate\Mail\TransportManager($app);
});
}

Related

How to use serviceProvider into other file laravel

I´m traying to use my new serviceProvider into mail.php because i need get values from database not .env i can show one solution in this site that one person creaed a serviceProvider and get all data that he needed.
My question is, how i can use this provider into mail.php?
my provider:
public function register()
{
if (\Schema::hasTable('app_settings')) {
$mail = DB::table('app_settings')->first();
if ($mail) //checking if table is not empty
{
$config = array(
'driver' => $mail->driver,
'host' => $mail->host,
'port' => $mail->port,
'from' => array('address' => $mail->from_address, 'name' => $mail->from_name),
'encryption' => $mail->encryption,
'username' => $mail->username,
'password' => $mail->password,
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
Config::set('mail', $config);
}
}
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
mail.php
for example: 'driver' => env('MAIL_DRIVER', 'smtp'),
how i can call driver of provider into driver mail.php
thanks for help, rewards
You can't do it, and it's not a good idea.
The simplest way is to override your config after everything was loaded.
You can use your AppServiceProdiver.php file, and in the boot() function :
public function boot()
{
// adapt of course...
$yourConfig = \DB::select('SELECT * FROM your_config_table');
config()->set([
'app.mail.driver' => $yourConfig[0]->value
]);
}

Laravel Tap Formatter: where do I actually format the message?

In my laravel application there's need to format a message I need to send into slack. Hence I set a slack log channel into config/logging.php:
'slack' => [
'driver' => 'slack',
'url' => /*Censored Hook URL*/,
'username' => 'MyApp',
'emoji' => ':gear:',
'level' => 'debug',
],
Also as seen on documentation I can do a monolog formater, hence I did the following:
namespace App\Logging;
class SlackLogFormatter
{
/**
* Customize the given logger instance.
*
* #param \Illuminate\Log\Logger $logger
* #return void
*/
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
$handler->setFormatter(...);
}
}
}
And specified it as tap into my log:
'slack' => [
'driver' => 'slack',
'tap' => [App\Logging\SlackLogFormatter::class]
'url' => /*Censored Hook URL*/,
'username' => 'MyApp',
'emoji' => ':gear:',
'level' => 'debug',
],
But in my formater where do I process the log entry itself? I mean:
The $handler->setFormatter does not seem to be a method of \Illuminate\Log\Logger class.
I cannot find out what method I need to override when I need to provide a custom format. I mean I have the invoke method then afterwards what?
Dimitrios is almost right (or perhaps this worked on older versions) but as Xavier said, if you copy that code exactly you will get the error
Return value of Monolog\Handler\AbstractProcessingHandler::processRecord() must be of the type array, null returned
You are going to want to do something like this instead :
<?php
namespace App\Logging;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\SlackWebhookHandler;
use Request;
class CustomiseFormatter
{
protected $request;
public function __construct(Request $request = null)
{
$this->request = $request;
}
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof SlackWebhookHandler) {
$handler->setFormatter(new LineFormatter(
'[%datetime%] %channel%.%level_name%: %message% %context% %extra%'
));
$handler->pushProcessor([$this, 'processLogRecord']);
}
}
}
public function processLogRecord(array $record): array
{
$record['extra'] += [
'url' => env("APP_URL"),
];
return $record;
}
}
Equally, as with how extra has be modified, you can change any other value.
The formatter for slack should be the following:
namespace App\Logging;
use Monolog\Formatter\LineFormatter;
class SlackLogFormatter
{
/**
* Customize the given logger instance.
*
* #param \Illuminate\Log\Logger $logger
* #return void
*/
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof SlackWebhookHandler) {
$format=""; // Look on the Monolog's Line formatter documentation
$formatter= new LineFormatter($format,"Y-m-d H:i:s");
$handler->pushProcessor(function ($record) {
//Append extra info of formatting here
});
$handler->setFormatter($formatter);
}
}
}
}
And config the slack not to send attachment nessesary for the formatter to work:
'slack' => [
'driver' => 'slack',
'tap' => [App\Logging\SlackLogFormatter::class]
'url' => /*Censored Hook URL*/,
'username' => 'MyApp',
'emoji' => ':gear:',
'level' => 'debug',
'attachment' => FALSE,
],
The setFormatter method takes a new Monolog Formatter as seen in: https://github.com/Seldaek/monolog/blob/master/doc/02-handlers-formatters-processors.md#formatters
Also the pushProcessor allows you to populate extra fields on your message eg. Showing an emoji on your log message:
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof SlackWebhookHandler) {
$format="%emoji% %message%";
$formatter= new LineFormatter($format,"Y-m-d H:i:s");
$handler->pushProcessor(function ($record) {
$record['emoji']=":poop:";
});
$handler->setFormatter($formatter);
}
}
}

How can I access mongodb database connection variable in model with zend framework 3

I have succeed to connect mongodb database with zend framework 3 directly without using any module. I have accessed that connection in controller by using factory. But I want to access this in every modules controllers and models i.e I want to access it globally. How can I access "$this->db" globally. Where to write the "connect" method to access that variable globally. I am new to zend framework 3 and if any one have any solution that will be very helpful to proceed with my project. I am sharing the files I have used.
global.php
use Zend\Session\Storage\SessionArrayStorage;
use Zend\Session\Validator\RemoteAddr;
use Zend\Session\Validator\HttpUserAgent;
return [
'config' => [
'driver' => 'mongodb',
'host' => '127.0.0.1',
'port' => 27017,
'username' => 'test',
'password' => 'test',
'dbname' => 'test',
'connection_string'=> sprintf('mongodb://%s:%d/%s','127.0.0.1','27017','test')
],
// Session configuration.
'session_config' => [
// Session cookie will expire in 1 hour.
'cookie_lifetime' => 60*60*1,
// Session data will be stored on server maximum for 30 days.
'gc_maxlifetime' => 60*60*24*30,
],
// Session manager configuration.
'session_manager' => [
// Session validators (used for security).
'validators' => [
RemoteAddr::class,
HttpUserAgent::class,
]
],
// Session storage configuration.
'session_storage' => [
'type' => SessionArrayStorage::class
],
];
UserControllerFactory.php
<?php
namespace Application\Controller;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Application\Service\AuthManager;
/**
* This is the factory for UserController. Its purpose is to instantiate the
* controller.
*/
class UserControllerFactory implements FactoryInterface{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null){
$config = $container->get('config');
$db_config = $config['config'];
$authManager = $container->get(AuthManager::class);
$authService = $container->get(\Zend\Authentication\AuthenticationService::class);
return new UserController($db_config, $authManager, $authService);
}
}
UserController.php
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;
use Zend\Crypt\Password\Bcrypt;
class UserController extends AbstractActionController{
private $db;
private $authManager;
private $authService;
/**
* Constructor.
*/
public function __construct($config, $authManager, $authService){
$this->connect($config);
$this->authManager = $authManager;
$this->authService = $authService;
}
/**
* Connection.
*/
private function connect($config){
try{
if ( !class_exists('Mongo')){
echo ("The MongoDB PECL extension has not been installed or enabled");
return false;
}
$connection= new \MongoClient($config['connection_string'],
array('username'=>$config['username'],'password'=>$config['password']));
return $this->db = $connection->selectDB($config['dbname']);
}catch(Exception $e) {
return false;
}
}
}

Dynamic mail configuration with values from database [Laravel]

I have created a service provider in my Laravel Application SettingsServiceProvider. This caches the settings table from the database.
$settings = $cache->remember('settings', 60, function() use ($settings)
{
return $settings->pluck('value', 'name')->all();
});
config()->set('settings', $settings);
settings table:
I am able to echo the value from the table like this:
{{ config('settings.sitename') }} //returns Awesome Images
This works fine on any blade files or controllers in App\Http\Controllers
Problem:
I am trying to echo the value to App\config\mail.php like this:
'driver' => config('settings.maildriver'),
'host' => config('settings.mailhost'),
But I'm getting this error:
Missing argument 1 for Illuminate\Support\Manager::createDriver()
Update:
I have created a new service provider MailServiceProvider to override the settings in Mail.php like this:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Config;
class MailServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
Config::set('mail.driver', config('settings.maildriver'));
Config::set('mail.host', config('settings.mailhost'));
Config::set('mail.port', config('settings.mailport'));
Config::set('mail.encryption', config('settings.mailencryption'));
Config::set('mail.username', config('settings.mailusername'));
Config::set('mail.password', config('settings.mailpassword'));
}
}
But still I am getting the same error!!
Is there any way to override default mail configuration (in app/config/mail.php) on-the-fly (e.g. configuration is stored in database) before swiftmailer transport is created?
Struggled for 3 days with this issue finally I figured out a way to solve it.
First I created a table mails and populated it with my values.
Then I created a provider MailConfigServiceProvider.php
<?php
namespace App\Providers;
use Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
class MailConfigServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
if (\Schema::hasTable('mails')) {
$mail = DB::table('mails')->first();
if ($mail) //checking if table is not empty
{
$config = array(
'driver' => $mail->driver,
'host' => $mail->host,
'port' => $mail->port,
'from' => array('address' => $mail->from_address, 'name' => $mail->from_name),
'encryption' => $mail->encryption,
'username' => $mail->username,
'password' => $mail->password,
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
Config::set('mail', $config);
}
}
}
}
And then registered it in the config\app.php
App\Providers\MailConfigServiceProvider::class,
Maybe its usefull to somebody, but I solved it as following;
In a ServiceProvider under the boot-method;
$settings = Cache::remember('settings', 60, function () {
return Setting::pluck('value', 'name')->all();
});
config()->set('settings', $settings); // optional
config()->set('mail', array_merge(config('mail'), [
'driver' => 'mailgun',
'from' => [
'address' => $settings['mail_from_address'],
'name' => $settings['mail_from_name']
]
]));
config()->set('services', array_merge(config('services'), [
'mailgun' => [
'domain' => $settings['mailgun_domain'],
'secret' => $settings['mailgun_secret']
]
]));
I used array_merge with the original config, so we only override the values we need to. Also we can use the Cache-facade in the boot-method.
Following the instructions here is the proper solution to the problem, in case if you're sending multiple emails per request over different SMTP configurations, Config::set() won't work right, the first email will use the correct settings, while all upcoming emails within the same request will use the same configuration of the first one, because the Mail service is provided as a singleton thus only the initial configurations will be used.
This also might affect emails sent over Laravel queue workers due to the same reason.
After research a lot, finally I found the best possible way to dynamic mail configuration.
I am saving my mail configuration data in the settings table, please have a look at the table structure.
Helpers/AaapHelper.php
<?php
namespace App\Helpers;
use App\Setting;
class AppHelper
{
public static function setMailConfig(){
//Get the data from settings table
$settings = Setting::pluck('description', 'label');
//Set the data in an array variable from settings table
$mailConfig = [
'transport' => 'smtp',
'host' => $settings['smtp_host'],
'port' => $settings['smtp_port'],
'encryption' => $settings['smtp_security'],
'username' => $settings['smtp_username'],
'password' => $settings['smtp_password'],
'timeout' => null
];
//To set configuration values at runtime, pass an array to the config helper
config(['mail.mailers.smtp' => $mailConfig]);
}
}
app\Http\Controllers\SettingController.php
<?php
namespace App\Http\Controllers;
use App\Helpers\AppHelper;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
class SettingController extends Controller
{
public function sendMail()
{
try
{
//Set mail configuration
AppHelper::setMailConfig();
$data = ['name' => "Virat Gandhi"];
Mail::send(['text' => 'mail'], $data, function ($message)
{
$message->to('abc#gmail.com', 'Lorem Ipsum')
->subject('Laravel Basic Testing Mail');
$message->from('xyz#gmail.com', $data['name']);
});
return redirect()->back()->with('success', 'Test email sent successfully');
}
catch(\Exception $e)
{
return redirect()->back()->withErrors($e->getMessage());
}
}
}
Explanation
While sending a mail through the sendMail function it will first configure mail through helper.

Error executing unit testing with laravel to load custom settings file

When I run the service from browser it works, but if i execute the test file i get this error:
1) Api\Domain\Tests\ServiceTest::testSetUp
RuntimeException: A facade root has not been set.
/WWW/api/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:210
/WWW/api/src/Api/Infrastructure/Domain/Model/SolrBaseRepository.php:30
/WWW/api/src/Api/Infrastructure/Domain/Model/SolrBaseRepository.php:30
/WWW/api/src/Api/Domain/Tests/ServiceTest.php:80
File confguration: config/solr.php
return array(
'endpoint' => array(
'localhost' => array(
'host' => '192.168.1.100',
'port' => 8080,
'path' => '/solr/clients/',
)
)
);
Solr base repository:
use Illuminate\Support\Facades\Config;
abstract class SolrBaseRepository
protected $client;
/**
* SolrBaseRepository constructor.
*/
public function __construct(){
$this->client = new \Solarium\Client(Config::get('solr'));
}
}
if I change
$this->client = new \Solarium\Client(Config::get('solr'));
by
$this->client = new \Solarium\Client(array(
'endpoint' => array(
'localhost' => array(
'host' => '192.168.1.100',
'port' => 8080,
'path' => '/solr/clients/',
)
)
));
The test works.
ServiceTest: Api/Domain/Tests/ServiceTest.php
class ServiceTest extends \PHPUnit_Framework_TestCase{
public function testSetUp()
{
$this->setUp();
$this->solrServicesRepository = New SolrServicesRepository();
}
}
I have tried to resolve it adding from this page to my ServiceTest.php:
use \Illuminate\Container\Container as Container;
use \Illuminate\Support\Facades\Facade as Facade;
/**
* Setup a new app instance container
*
* #var Illuminate\Container\Container
*/
$app = new Container();
$app->singleton('app', 'Illuminate\Container\Container');
/**
* Set $app as FacadeApplication handler
*/
Facade::setFacadeApplication($app);
But this does not work for mi.
It's been a while, but I found the answer as I got stuck with the same problem.
In your test, you need to call the parent setup function, as it initializes:
class ExampleTest extends TestCase
{
public function setUp()
{
parent::setUp();
$this->faker = Faker\Factory::create();
// other lines of code that you need
}
public function testWithFacades()
{
//now it works
}
}

Categories