using sfMailer and Dispatcher inside a task - php

I created a task in symfony 1.4, and I want to create a mailer based on the app.yml config.
this is my PHP code :
$config = sfConfig::get('app_mailers_mailer_a');
$this->_mymailer = new sfMailer($this->dispatcher, $config['param']);
when I execute the task, and vardump the $config variable, I get my config information (host, port, etc..).
The problem is in the second line, It stops their, without any error messages, so I think that the way I use the dispatcher (or $config['param']) is incorrect.
Thanks

I found the solution, the problem was not $config or $this->dispatcher, but the problem was that I forgot to initialize the sfMailer, so I added those lines before;
Swift::registerAutoload();
sfMailer::initialize();
Cheers ;)

Related

Cakephp email official example not working?

I am new to Cakephp (and I have asked a few question on Cakephp here recently) and I am learning by trying out the example on the official site. The following is the link.
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
I added the to my Posts controller which I created before following the official example code.
public function emailTesting(){
$Email = new CakeEmail();
$Email->config('default');
$Email->from(array('me#example.com' => 'My Site'));
$Email->to('myWorkingEmail#gmail.com');
$Email->subject('Testing Subject');
$Email->send('Testing email content');
}
According to the configuration section,
It is not required to create app/Config/email.php, CakeEmail can be used without it and use respective methods to set all configurations separately or load an array of configs.
So, I think the above code should work even if I don't create app/Config/email.php. Then,when I try to open the page
http://localhost/cakephpTesting/emailTesting/
It gives a blank page with no error message (the debug is already 2, which should shows all kind of error message if there is any error) but I didn't receive any email. I also tried different email service provider like Yahoo and my school email. All of them fails, so I think it should be the problem of my code?
So, anybody got any idea on what have I missed?
As mentioned in your question, it is not required to create a app/Config/email.php, IF you use respective methods to set all configurations separately or load an array of configs.
You are using Email::config() method to load a $default config array which is supposed to be defined somewhere. As good practise, it can be defined in the app/Config/email.php or define it in your controller's action (don't recommend the latter). CakeEmail needs to know the transport type, host, port, etc.
Read the section after the line that you have mentioned in your question which tells you how to either use the constructor of CakeEmail or use the $config array.

Using Kohana in File outside Kohana

Here's the situation:
I have a catch-all on my domain email (so *#domain.com) redirecting to a piping script located at /home/domain/scripts/piper.php. This piper script is not within the Kohana ORM, but all of my other files are. I want to try to use Kohana inside this piper.php file.
I have tried (unsuccessfully) all of the following:
Including Kohana
I couldn't figure out what needed to be included, and more importantly how to override the url variable that Kohana uses to determine the right controller. Also, this is a catch-all piper, so it isn't using HTTP (to my knowledge), so much as executing a command.
Piping
I tried piping to the following:
/home/domain/public_html/index.php --uri="piper"
But cPanel makes this impossible, as you can only specify the destination script, and not the proper flags and such (unless I am missing something).
PHP exec()
I tried using the following line:
exec("php /home/domain/public_html/index.php --uri=\"/piper\"")
I was hoping that the stdin data would be maintained across the exec() command, but I could never get it to recognize the uri command, though I can run this on my localhost and it works just fine.
I was using http://www.coderelic.com/2011/10/creating-cron-jobs-in-kohana-3-x-is-a-piece-of-cake/ as a reference, but can't get anything to work.
I'm happy with either one of these solutions such that I can see an incoming email, parse it, then send emails based on the parameters.
Let me know if you need more information! I'm le stumped.
/home/domain/public_html/index.php --uri="piper" would be a valid way to do it. If your host sucks and doesn't let you specify that, put it into a bash script instead and reference that.
If you are on any recent version of kohana (3.2 or 3.3), a better way to do this would be to use Minion to run the command line task. This is what Minion was designed for.
All you need to do is to:
modify your piper.php script to be a valid PHP class;
place it in /application/classes/ folder;
Kohana will automatically load your class file (like include) during initialization.
Then you can use your piper class as usual class by $piper = new Piper; ....
UPD
You have to serve your emails trough Kohana.
Create controller, for example pipe (route it with /pipe URL):
public function action_pipe() {
$pipe = new Pipe; // This creates new Pipe object (your emails serving class)
$pipe->serve(); // Sserve emails within `serve()` method of Pipe class
}
Although admittedly, I'm not sure if these other answers are correct because I can't figure out how to reproduce the results.
What ended up working for my situation was to create a Controller_Piper class that is called in the /home/domain/scripts/piper.php. What I did was to copy the code from /home/domain/public_html/index.php and changed the following:
echo Request::factory("/piper")
->execute()
->send_headers(TRUE)
->body();
This loads the piper controller and executes everything very nicely. Not sure if it's the cleanest, but it does work.

Yii - CHttpRequesterror while functional unittesting in module

When I'm trying to execute a functional unittest of a module within my Yii code, I keep receiving the following error:
CException: CHttpRequest is unable to determine the request URI.
At first, I though it was because it couldn't find the module. However, If I change the url to a wrong one, I get a correct error,s tating it couldn't find the view.
This is how my testing code looks like
public function testViewControllerModule()
{
ob_start();
Yii::app()->runController('module/controller/view');
}
Any ideas on what I might be missing?
bool.devs answer works so far.
This blog post explains the origin of the exception pretty well:
http://mattmccormick.ca/2012/09/14/unit-testing-url-routes-in-yii-framework/
In my case, I generalized the solution and have set the following variables in /www/protected/tests/bootstrap.php:
...
$_SERVER['SCRIPT_FILENAME'] = 'index-test.php';
$_SERVER['SCRIPT_NAME'] = '/index-test.php';
$_SERVER['REQUEST_URI'] = 'index-test.php';
Yii::createWebApplication($config);
Consider using 'index-test.php' instead of 'index.php' because it contains the config 'test.php' which is responsible for fixtures and maybe other test relevated configurations.
If someone has better suggestions feel free to comment :)
Kind regards
I think it's because you haven't set any server variables, i.e $_SERVER and you might be doing something like this in your controller:
Yii::app()->request ....
So before you run your test, make sure you use a fixture for the server variables also. I think this should suffice for now:
$_SERVER=array(
'REQUEST_URI'=>'index.php', // the other fields should follow
);
However to run functional tests i would recommend using SeleniumRC, you won't have to do these workarounds then, and can simulate user clicks also, i think.
Read the initial guide to Functional Testing , read the selenium rc phpunit guide, and also the CWebTestCase documentation.
Notes: You might still have to use fixtures for some variables, and i don't have much experience in testing(which is bad), so i'm not very sure if i am completely correct about selenium.

How to include a codeigniter project into another php file?

I have a project based on codeigniter. And I should use one class that extended from a codeigniter controller in another php file. But I didn't find the solution about how to teach another php file to see whole CI-project. Beyond that needed class can not inherit when i call it from other place.
I'm not 100% sure if this helps get you in the right direction, but kudos if it does!
Codeigniter routes the application depending on the environment state of the URI. What you need to do is set the environment and include the index view file like so:
$_SERVER["REQUEST_URI"] = "cms/2";
//Set GET action,method params etc
require_once "path/to/index.php";
When you load CI Index file it reads the SERVER Variable and others which you may have to find and execute the controller and method, I would also advise that you modify the library/view file as it may exit upon output causing your script to exit.
Also you may wis hto look into ob_start() to catch the buffer.

Conditional Component Loading in CakePHP

I'm using the DebugKit component in my project but I want to turn it on only on the staging server and have it not load when running from the production server.
I know I can turn it off with the debug config value but I want to keep that value at 1 for both servers.
I tried conditionally defining a contstant 'DEBUG_KIT' in bootstrap.php as either the component name (ie. 'DebugKit.Toolbar') or null. Then using that constant in the var $component definition at the top of the app_controller file. Well, Cake doesn't like having a null in the component array and barfs. Doesn't like an empty string either.
I feel like I'm missing something but can't quite see the forest for the trees. Thanks in advance!
I think the fundamental purpose of DebugKit is tied to being in debug mode, so I can understand that the tools don't provide the capacity to be disabled without also disabling debug mode.
That said, if you absolutely must do this, I think your best bet is to directly modify app/plugins/debugkit/controllers/components/toolbar.php, supplementing the existing debug-mode check in ToolbarComponent::initialize with a check against your constant.
(For what it's worth, I think you'd be better off turning debug-mode off on your production server, and using errors/warnings logged in /app/tmp/logs/error.log to identify issues that have slipped through your testing.)
I do something similar in my apps: I would use the __construct method to detect the presence DEBUG_KIT and add it to the $components array. This function gets called before the $components array is processed, so you can add/remove components transparently.
In your app_controller
function __construct(){
if(DEBUG_KIT){
$this->components[] = 'DebugKit.Toolbar'
}
parent::__construct();
}
If you have a _construct function in any or your individual controllers, remember to include parent::_construct(); otherwise you'll 'break the chain'.
Hope this helps
First, thanks to Adam Giles for a great answer. I hadn't thought to look at the __construct() callback. That just may be a better way than I found. And to Daniel Wright, point made sir. I'll probably change my production server to 0 debug soon and start watching the error logs.
I found my own answer shortly after posting this question. The DebugKit has an 'autoRun' parameter that will turn it on and off. So, I first set a global constant in bootstrap.php like this:
define( 'IS_DEV', ($_SERVER['SERVER_NAME'] == 'staging.example.com') );
Then in app_controller.php, I use it to set the 'autoRun' parameter in the $components statement.
var $components = array( 'DebugKit.Toolbar'=>array('autoRun'=>IS_DEV) );
This seems to work pretty well so far.

Categories