I need to use different Pusher account for a specific part of my web app, so I tried to override the config using the following:
public function send(Request $request)
{
$general_pusher = config('broadcasting.connections.pusher');
$message = Message::create([
'from' => auth()->id(),
'to' => $request->contact_id,
'text' => $request->text
]);
\Config::set('broadcasting.connections.pusher', config('cfg.internal_chat_pusher'));
logger(config('broadcasting.connections.pusher'));
broadcast(new NewMessage($message));
\Config::set('broadcasting.connections.pusher', config('cfg.internal_chat_pusher'));
return response()->json($message);
}
then, I tried to test using a random value for which Laravel should raise an error, but no error and the messages get sent and received, but using the original/old pusher config values.
as you can see I used the logger instruction which give me the following:
[2020-09-19 14:06:09] local.DEBUG: array (
'driver' => 'pusher',
'key' => '1',
'secret' => '1',
'app_id' => '1',
'options' =>
array (
'cluster' => '1',
'useTLS' => true,
),
)
but, even though the logger command output the values that should give the error, I get no error, and messages get sent and received perfectly.
how to use different pusher account for a specific event?
Update:
I tried to edit the brodcasting.php as follow:
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'useTLS' => true,
],
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
'log' => [
'driver' => 'log',
],
'null' => [
'driver' => 'null',
],
'internal_chat_pusher' => [
'driver' => 'pusher',
'key' => env('INTERNAL_CHAT_PUSHER_APP_KEY'),
'secret' => env('INTERNAL_CHAT_PUSHER_APP_SECRET'),
'app_id' => env('INTERNAL_CHAT_PUSHER_APP_ID'),
'options' => [
'cluster' => env('INTERNAL_CHAT_PUSHER_APP_CLUSTER', 'ap2'),
'useTLS' => true,
]
],
],
];
then in the send function inside the controller:
public function send(Request $request)
{
$message = Message::create([
'from' => auth()->id(),
'to' => $request->contact_id,
'text' => $request->text
]);
Broadcast::driver('internal_chat_pusher');
broadcast(new NewMessage($message));
return response()->json($message);
}
}
but still, the messages are sent using the original/old account configs.
Both of these options should work, but having a separate driver in your brodcasting.php is a cleaner way to go.
You've got the code right, but from the command line you must also remember to:
php artisan queue:restart
This will take the new code into account. Been burned by this a dozen times or more myself.
Related
Hi i am trying to access database name by using config but unfortunately it's not working please help me how can i resolve that thanks.
Property model
public function editedBy()
{
return $this->belongsToMany('App\User',config('database.connections.web.database'), 'property_id', 'user_id')->withTimestamps();
}
app/config/database/
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'web' => [
'driver' => 'mysql',
'host' => env('DB_WEB_HOST'),
'port' => env('DB_WEB_PORT'),
'database' => env('DB_WEB_DATABASE'),
'username' => env('DB_WEB_USERNAME'),
'password' => env('DB_WEB_PASSWORD'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
You can try using to database name using env('DB_DATABASE'). Its displaying that you have change the default variables so, it may be like this in your case.
public function editedBy()
{
return $this->belongsToMany('App\User',env('DB_WEB_DATABASE'), 'property_id', 'user_id')->withTimestamps();
}
or if you really want to use config this is the example.
public function getDatabaseName()
{
$databaseName = Config::get('database.connections.'.Config::get('database.default'));
dd($databaseName['database']);
}
In my Laravel 7.6 app I use sendgrid for email sending with code in control like :
\Mail
::to($newContactUs->author_email)->
send(new SendgridMail('emails/contact_us_was_sent', $newContactUs->author_email, '', $subject, $additiveVars, $attachFiles));
with class in app/Mail/SendgridMail.php :
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Sichikawa\LaravelSendgridDriver\SendGrid;
use App\Settings;
use App\Http\Traits\funcsTrait;
class SendgridMail extends Mailable
{
use Queueable, SerializesModels;
use SendGrid;
use funcsTrait;
private $m_view_name;
private $m_to;
private $m_cc;
private $m_subject;
private $m_additiveVars;
private $m_attachFiles;
public function __construct( $view_name, $to= [], $cc= '', $subject= '', $additiveVars= [], $attachFiles= [] )
{
$this->m_view_name= $view_name;
$this->m_to= $to;
$this->m_cc= $cc;
$this->m_subject= $subject;
$all_emails_copy = \Config::get('app.all_emails_copy');
if ( empty($this->m_cc) and !empty($all_emails_copy)) {
$this->m_cc= $all_emails_copy;
}
$additiveVars['site_home_url'] = \URL::to('/');
$additiveVars['site_name'] = Settings::getValue('site_name');
$additiveVars['noreply_email'] = Settings::getValue('noreply_email');
$additiveVars['support_signature'] = Settings::getValue('support_signature');
$additiveVars['medium_slogan_img_url'] = config('app.url').config('app.medium_slogan_img_url');
$this->m_additiveVars= $additiveVars;
$this->m_attachFiles= $attachFiles;
}
/**
* Build the message.
*
* #return $this
*/
public function build( )
{
$mailObject= $this
->view( $this->m_view_name)
->subject($this->m_subject)
->to([$this->m_to])
->cc([$this->m_cc])
->with( $this->m_additiveVars )
->sendgrid( $this->m_additiveVars );
foreach( $this->m_attachFiles as $next_attach_file) {
if ( file_exists($next_attach_file) ) {
$mailObject->attach($next_attach_file);
}
}
return $mailObject;
}
}
and template resources/views/emails/contact_us_was_sent.blade.php:
...
<div class="wrapper">
#inject('viewFuncs', 'App\library\viewFuncs')
<h4 class="email_title">
Hello, {!! $to_user_name !!} !
</h4>
...
#include( 'emails.app_footer')
#include( 'emails.emails_style')
</div>
and it works for me now, but now with "Multiple Mail Drivers" I added mailtrap to my .env :
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=NNNNNNNN
MAIL_PASSWORD=NNNNNNNN
and I want to use mailtrap while testing the app using the same
template resources/views/emails/contact_us_was_sent.blade.php
and switching from mailtrap to sendgrid as easy as possible.
I tried something like :
\Mail::mailer('smtp')
->to($newContactUs->author_email)
->send( \Mail('emails/contact_us_was_sent', $newContactUs->author_email, '', $subject, $additiveVars, $attachFiles) );
But got error as \Mail does not support templates.
Are there something to use support templates for mail Method? Some wrapper?
Updated:
Priorly I worked with sendgrid and for this in file config/mail.php I
wrote all sendgrid parameters.
Now I want to write 2 emeil servers and fi=or this in .env I wrote:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=mailtrip_id
MAIL_PASSWORD=mailtrip_password
SENDGRID_HOST=smtp.sendgrid.net
SENDGRID_PORT=587
SENDGRID_ENCRYPTION=tls
SENDGRID_USERNAME=sendgrid_user
SENDGRID_PASSWORD=sendgrid_user_password
and I remade config/mail.php (I got a sample from):
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
'log_channel' => env('MAIL_LOG_CHANNEL'),
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST'),
'port' => env('MAIL_PORT'),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'sendgrid' => [
'transport' => 'sendgrid',
'host' => env('SENDGRID_HOST', 'smtp.sendgrid.net'),
'port' => env('SENDGRID_PORT', 587),
'encryption' => env('SENDGRID_ENCRYPTION', 'tls'),
'username' => env('SENDGRID_USERNAME'),
'password' => env('SENDGRID_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'ses' => [
'transport' => 'ses',
],
'mailgun' => [
'transport' => 'mailgun',
],
'postmark' => [
'transport' => 'postmark',
],
'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
'array' => [
'transport' => 'array',
],
],
];
I am not sure that this config file is valid? Are mail config parameters are read from mailers array ?
Looks like default(mailtrip) mail config is used always. Is it invalid format ?
In my control Ido:
$email_mode= 'live';
// $email_mode= 'debug';
if( $email_mode== 'debug' ) {
\Log::info( '-10 Send to mailtrap ::' );
\Mail
::mailer('smtp')
->to('myemail#yahoo.com') // DEBUG
->send(new TestEmail); //
\Log::info( '-10 Send to mailtrap AFTER::' );
}
// sendgrid
if( $email_mode== 'live') {
\Log::info( '-11 Send to sendgrid ::' );
\Mail
::mailer('sendgrid')
->to('myemail#yahoo.com') // DEBUG
->send(new SendgridMail('emails/contact_us_was_sent', $newContactUs->author_email, '', $subject, $additiveVars, $attachFiles));
\Log::info( '-11 Send to sendgrid AFTER::' );
}
I check in logs that live flow is run but anyway I got email at mailtrap.
Thanks!
The short answer is you are using \Mail() which is a native php function
see: https://www.php.net/manual/en/function.mail.php and try to embed that in a laravel Mailer.
This is not how you should use it, this is really not advised.
Some more detail:
As you write mailables, already you should consider, the Mailable should not define the driver, so it's not encouraged to name it SendGridMailA.. see it as MailA send with Mail::mailer($mailDriver)
See:https://laravel.com/docs/7.x/mail#writing-mailables
Long answer, see this video, explains how to implement multiple mail drivers:
https://www.youtube.com/watch?v=HCONO0cwsoI
However it looks like you using it as a debug method. This is not why multiple mail drivers where introduced in laravel 7. It's more like you should use a different driver for bulk mails for example and for password resets...
That makes sense..
For debuging, it more usefull to make the default driver, depend on the APP_DEBUG configuration for example, or introduce an own ENV var, to toggle your production app in debug mode...
I found decision with modifying config/mail.php :
<?php
return [
'default' => env('MAIL_MAILER', 'sendgrid'),
'mailers' => [
'mailtrap' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST'),
'port' => env('MAIL_PORT'),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'sendgrid' => [
'transport' => 'smtp',
'host' => env('SENDGRID_HOST', 'smtp.sendgrid.net'),
'port' => env('SENDGRID_PORT', 587),
'encryption' => env('SENDGRID_ENCRYPTION', 'tls'),
'username' => env('SENDGRID_USERNAME'),
'password' => env('SENDGRID_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
and having 2 groups 'mailtrap' and 'sendgrid' in .env I wrote in control :
\Mail
::mailer(true ? 'sendgrid' : 'mailtrap')
->to($newContactUs->author_email)
->send(new SendgridMail(
'emails/contact_us_was_sent',
$newContactUs->author_email, '', $subject,
$additiveVars,
$attachFiles)
);
I am having a super hard time getting redis to be configured. It was working, but I upgraded my servers and composer updated- now I getting "redis is not configured correctly"
define('_CACHE_SERVER', '*********.cache.amazonaws.com:6379');
'_cake_core_' => [
'className' => 'Redis',
'servers'=> [ _CACHE_SERVER ],
'prefix' => 'mrg_cake_core_',
'serialize' => 'php',
'duration' => '+2 minutes',
],
I have written a blank php and connected to redis with just simple $redis = new Redis(); and got $redis->lastSave(); Seems like something changed in CAKE. Very confused.
Thank you ndm, I changed the config to:
define('_CACHE_SERVER', '************.cache.amazonaws.com');
'_cake_core_' => [
'className' => 'Redis',
'host' => _CACHE_SERVER,
'port' => 6379,
'prefix' => 'myapp_cake_core_',
'duration' => '+1 years',
'url' => env('CACHE_CAKECORE_URL', null),
],
I'am using Laravel 5.5 with pusher to make a real time notification , the notification made from the Api
after i made the configuration
in the Api
public function store(Request $request)
{
$advertising = Advertising::create($request->all());
$admins = \App\Admin::all();
\Notification::send( $admins, new \App\Notifications\AdvertisingAdded($advertising) );
return $advertising;
}
in AdvertisingAdded
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\BroadcastMessage;
use App\Advertising;
class AdvertisingAdded extends Notification
{
use Queueable;
//-must be public fir pusher
public $advertising;
public function __construct(Advertising $advertising)
{
$this->advertising = $advertising;
}
public function via($notifiable)
{
return ['database','broadcast'];
}
public function toArray($notifiable)
{
return [
'msg' => 'Advertising '.$this->advertising->title_ar.' is added ',
'advertising_id' => $this->advertising->id
];
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'msg' => 'Advertising '.$this->advertising->title_ar.' is added ',
'advertising_id' => $this->advertising->id
]);
}
}
when i post from postman i get an error
Illuminate \ Broadcasting \ BroadcastException No message
error image
i followed this video https://www.youtube.com/watch?v=i6Rdkv-DLwk
i solve my problem by : making the encrypted: false
Add curl options to broadcasting.php
`'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'encrypted' => true,
'curl_options' => [
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
]
],`
I solved this problem. In config/broadcasting.php use this code
'options' => [
'cluster' => 'eu',
'useTLS' => false
],
make useTLS false
In laravel 7, configured like below to config/broadcasting.php and and run artisan command cache:clear. solved for me.
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => 'mt1',
'useTLS' => false,
],
],
I solved my problem by setting my .env file
Set:
APP_URL=http://localhost
DB_HOST=localhost
And run
php artisan config:cache
I'm working on a module (a simple cms) with Kohana 3.2 and i'm getting this exception "Error reading session data."
I'm using native session and the funny thing is if i set a "default" group database connection the error isn't showed... (i'm using a custom connection group and i've set this database connection group to the user,role and user_token models).
here's my config file
auth.php
return array(
'driver' => 'orm',
'hash_method' => 'sha256',
'hash_key' => 'just a test 1',
'lifetime' => 1209600,
'session_type' => 'native',
'session_key' => 'just a test 2',
// Username/password combinations for the Auth File driver
'users' => array(
// 'luca' => 'e12afe0d3ead3d36191d86229d27057d96d9f2e063fe6f3e86699aaab5310d42'
// 'admin' => 'b3154acf3a344170077d11bdb5fff31532f679a1919e716a02',
),
);
session.php
return array(
'native' => array(
'name' => 'session_native',
'lifetime' => 43200,
),
'cookie' => array(
'name' => 'session_cookie',
'encrypted' => TRUE,
'lifetime' => 43200,
),
'database' => array(
'name' => 'session_database',
'encrypted' => TRUE,
'lifetime' => 43200,
'group' => Pencil::db_group(),
'table' => 'sessions',
'columns' => array(
'session_id' => 'session_id',
'last_active' => 'last_active',
'contents' => 'contents'
),
'gc' => 500,
),
);
You set encrypted to true, so you need an encrypt key. In your config/encrypt.php add this:
<?php
return array(
'default' => array(
'key' => 'MY_RANDOM_KEY_I_MADE_UP_ALL_BY_MYSELF',
),
);
I would keep session_key set to 'auth_user' instead of your random key as well. I think key in that circumstance is not the same as a hash key.
Check your logs in application/logs to see if anything else is missing.