Magento isn't really designed to do this, which means you can use controller objects as you would any other object, but there will be complications. Since these methods are meant to be called from an HTTP context, they'll very often do things with the request and response objects, or rely on some bit of session state that just doesn't exist when you're running things from the command line.
That said, the following will work
// require you file
if(!class_exists('Mage_Customer_AccountController')) //in case the class already exists
{
require_once('Mage/Customer/controllers/AccountController.php');
}
// instantiate your controller, using the `Mage:app()` object to grab the required request and response
$controller = new Mage_Customer_AccountController(
Mage::app()->getRequest(),
Mage::app()->getResponse()
);
// grab request and response object to manipulate as needed
// (i.e. controller action expects post variables, etc.)
$request = $controller->getRequest();
$response = $controller->getResponse();
//manipulate things as per above
//call the action
$controller->someAction();
To answer your question, strictly from the coding point of view, you can do what Magento does:
// @see Mage_Core_Controller_Varien_Router_Standard::match
// instantiate controller class
$controllerInstance = Mage::getControllerInstance($controllerClassName, $request, $front->getResponse());
so you could do something like:
// The action is overwritten in the action controller's constructor; to make sure
// there are no nasty surprises, we're going to undo this.
$originalAction = Mage::app()->getFrontController()->getAction();
// The action controller's constructor expects request and response objects;
$controllerInstance = Mage::getControllerInstance(
'TD_SMTPemail_Smtp_TestController',
new Mage_Core_Controller_Request_Http(), // you can replace this with the actual request
new Mage_Core_Controller_Response_Http()
);
$controllerInstance->testAction();
// and undo
Mage::app()->getFrontController()->setAction($originalAction);
Now, from the logical point of view, adding this kind of logic in the template is highly not recommended. The templates are only for output; they can use some logic, but the logic has to be contained in their block class, so in the template you can only call $this->???
methods; with the exception of some minor helpers maybe, something like currency formatting for instance.
If the controller action that sends the emails is yours, you could refactor it, move the email sending logic to a model, then you can use that model in both situations, the original controller and the current controller. If the controller is not yours, then you migth wanna take a look at what models it uses and re-use those.
Cheers
Best Answer
You can use the code below your own Action in controller to call url from Ajax. Using phtml file.
In your (Like vendor/module/Block/Example.php) block put below code.
Put below code view/fronted/templates/example.phtml
Put below code view/fronted/web/js/example.js
Your own controller like vendor/module/Controller/Index/View.php