How to execute a Shell method in a Controller? - cakephp

I am using a cakephp3 plugin to manage a mail queue to send newsletter (Lorenzo Cakephp Email Queue). It works fine, but I need to use a Shell method with the bake command to send the newsletter.
It is not a problem with a cron job, but I would like to be able to send test mail without waiting for the cron job.
I tried to call the bake command with a php exec command, but it does not work (maybe a right problem ?) but I don't have access to the server to manage the rights. But, as the method and the controller are both in cakephp, I think it's weird to do it with a console (external) bake call...
Is there a way to call a Shell method from a Controller, which would be the "cleanest" and easiest way ?
Thanks all for you help !

which would be the "cleanest" and easiest way ?
A shell should be considered similar to a controller - controllers don't call controllers.
Consider putting the cli logic in a common place and have the cli and controller action call that, not each other. The relevant cli code is a few lines of code, but it's not actually doing anything complex, just looping on the emails to be sent, and sending them one by one.
I need to use a Shell method with the bake command to send the newsletter
Note that bake is a cli, it is not "the" cli, and forms no part of the runtime usage of that plugin.
bin/cake EmailQueue.sender -h
It may not work currently, because you're using the wrong cli commands to try and run it from your controller action - though it's not a very good idea/design to do it that way anyway.

Related

Symfony 6 can't send email with mailer (without configured database)

Hello guys i just started to building web using Symfony6 - im trying to send email using mailer however it somehow require database to be configured (+ some special table created for messages...).Maybe there is some workaround so it would work without DB.. - thing is in Symfony 5 there was no problem with that.
If you comment the default configuration in config/packages/messenger.yaml
#Symfony\Component\Mailer\Messenger\SendEmailMessage: async
or set it to null your email should be sent immediately.
By default it's configured to work in async mode via messenger, that's why #glukose 's answer works. It sets sync mode and emails are sent immediately that way.
Your messenger is configured to work via Doctrine, that's why it requires DB. Like this:
MESSENGER_TRANSPORT_DSN=doctrine://default
You can use many other options, like Redis or AMQP, async mode is used for a reason after all.
Anyway async mode won't work without workers started with command php bin/console messenger:consume async. That was my problem because I wasn't aware about what was used. No errors, but emails are not actually sent with Symfony mailer!

angularjs ngfacebook batch request

Can anyone who knows how to use the angularjs ngFacebook module help me to perform a facebook batch request? Is it possible to do it with this module?
What I need exactly is to get the user events from facebook, for that I have to do 4 different request:
$facebook.api('/me/events/attending').then(function(response) {//code here});
$facebook.api('/me/events/created').then(function(response) {//code here});
$facebook.api('/me/events/maybe').then(function(response) {//code here});
I think I could batch this request, I just don't know if it's possible to do using this module.
Also the most tricky part would be that, for each event returned I would need to get the owner, and with the owner.id to get his picture, right now what I do is:
$facebook.api('/me/events/attending?fields=owner').then(function(response) {
//And here I do a "for" into the events to request for each owner picture
});
Of course it doesn't seem the best way to do it, but I have searched a lot for the solution and I couldn't make anything work.
I think you should be able to request all user events, inluding the owner info:
GET /me/events?fields=id,name,owner{id,picture},rsvp_status
You can determine the "status" of the event to the user by the rsvp_status (attending, maybe, declined, no_reply) field.
See
https://developers.facebook.com/docs/graph-api/reference/v2.3/event#read
https://developers.facebook.com/docs/graph-api/reference/user/events/
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.0#fieldexpansion
I'm not sure of the batch request protocol that Facebook uses but you could try this module.
https://github.com/jonsamwell/angular-http-batcher
If it doesn't support it add an issue and I'll looking into implementing it. Disclosure: I created this angular http batching library.

CakePHP - Session in Console?

I write a CLI API for my Application which sends E-Mails. For sending different languages i tried changing the current language in the session (which works fine in a different controller) but in the CLI it prints:
Fatal Error Error: Call to a member function read() on a non-object
Which remains to
$currentLang = $this->Session->read('Config.language');
Any way to use the session?
There is no component or helper for CLI.
If one needs the session (for testing!), one would use CakeSession::read().
Note: session is something web-frontend-based and you never need it in CLI as there is no way to actually handle sessions there. You need to use a different env() based approach there.

How can I script WordPress for non-GUI automated Pages/Posts-import?

This is probably a "cannot see the forest because of the trees" situation,
but how do I create a script which does the automated import of Posts/Pages, without a hook in the WP Website-GUI (e.g. in Theme's functions.php). It should be standalone triggerable by calling the script name via webserver.
via this API-call wp_insert_post()
You want to use the WordPress API (http://codex.wordpress.org/XML-RPC_wp) to connect. You can do this with almost any scripting language, but since you mention running it on the webserver, and WordPress is written in PHP, we'll go with that language for now.
Check out this tutorial:
http://life.mysiteonline.org/archives/161-Automatic-Post-Creation-with-Wordpress,-PHP,-and-XML-RPC.html
He shows example of how to create a script that will insert a post into your WordPress blog. The script can be given execute permissions and ran via the command line or a cron job.
You will have to code the logic to get the post from wherever your data is stored, though.

cakephp: running controller action as a cron job not working

Am trying to run a controller action as a cron job, but it is giving me the message, "could not open input file"
To do the above, i used this link, http://bakery.cakephp.org/articles/view/calling-controller-actions-from-cron-and-the-command-line....
But not working for me, I also tried to place the cron dispatcher in /app/webroot, but still not working for me.
thanks...
PHP cannot load your cron_dispatcher.php. Make sure your cron entry is correct, i.e. points to the correct full path of your PHP file. Also make sure that you have your access rights correctly. It's possible that the file exists but that when PHP runs under cron, it is not allowed to read the file.
PS: Have you considered using CakePHP's Shell functionality instead of this dispatcher? It was designed for CLI use.
Call direct your action
wget http://www.example.com/homes/my
in my action wirte your code
set in cpanel ....

Resources