i am working on a cakephp 2.3. i want to load Email component into my class which is in Lib folder. same case for Vendor files too at the moment what i am doing is this..
App::uses('EmailComponent', 'Controller/Component');
class Email {
public static function sendemail($toEmail,$template,$subject) {
$this->Email->smtpOptions = array(
'port'=>'25',
'timeout'=>'30',
'host' => 'host',
'username'=>'username',
'password'=>'password'
);
$this->Email->template = $template;
$this->Email->from = 'no-reply#hello.com';
$this->Email->to = $toEmail;
$this->Email->subject = $subject;
$this->Email->sendAs = 'both';
$this->Email->delivery = 'smtp';
$this->Email->send();
}
i am not able to use $this.. i am getting this error
$this when not in object context
You don't do that, components are controller "extensions", they are not ment to be used without them.
For emailing purposes use the CakeEmail class (the E-Mail component is deprecated anyways).
App::uses('CakeEmail', 'Network/Email');
// ...
$Email = new CakeEmail(array(
'port' => 25,
'timeout' => 30,
'host' => 'host',
'username' => 'username',
'password' => 'password',
'transport' => 'Smtp'
));
$Email->template($template)
->emailFormat('both')
->from('no-reply#hello.com')
->to($toEmail)
->subject($subject)
->send();
$this can be use in class for their on variables and method define within class
In cakephp $this is used in controller when the $components array is defined within controller.
Try this
$email = EmailComponent();
$email->$smtpOptions= array();
I was able to overcome this in one of my Lib classes with Cake 3 using the following...
$oRegistry = new ComponentRegistry();
$oComponent = $oRegistry->load('PluginName.ComponentName');
I'm unsure if this creates any kinda discord with the state with the app's component registry; but, for my purposes, it seems to work.
Hope this helps someone :)
Related
I am developing a site in cakephp2.5. I have two plugin Webmaster and debugKit. When I write
CakePlugin::load('Webmaster', array('bootstrap' => false, 'routes' => false));
CakePlugin::load('webmaster');
CakePlugin::load( 'DebugKit');
The site works correctly on local system but not on live server. However if I remove one of the above Webmaster it shows error on local system and also on live
Error: The application is trying to load a file from the webmaster plugin
Error: Make sure your plugin webmaster is in the app\Plugin directory and was loaded
I also tried but no luck. Struggling from 2 days. Also seen these links
link1
link2
Here is my WebmasterAppController
<?php
App::uses('AppController', 'Controller');
class WebmasterAppController extends AppController{
public $theme= "beyond";
//public $layout=NULL;
public function beforeFilter(){
//$this->Auth->allow('login');
$this->Auth->loginAction= array('controller'=>'users', 'action'=>'login');
$this->Auth->loginRedirect= array('controller'=>'users', 'action'=>'index');
$this->Auth->loginError= 'Invalid Email/Password!';
$this->Auth->authError= 'You are not authorised to access!';
$this->Auth->logoutRedirect= array('controller'=>'users', 'action'=>'login');
AuthComponent::$sessionKey = 'Auth.Webmaster';
//we don't need to load debug kit
$this->Components->unload('DebugKit.Toolbar');
parent::beforeFilter();
}
}
And here is AppController
class AppController extends Controller{
public $cakeDescription = "CakePhp | ";
public $theme = "alpus";
public $ext = 'html';
public $helpers = array('NiceForms', 'coolFun');
public $components = array(
'DebugKit.Toolbar',
'Common',
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'pages',
'action' => 'dashboard'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login',
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'fields' => array('username' => 'email')
)
),
'sessionKey' => 'Admin'
)
);
public function beforeFilter(){
if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') {
$this->theme = 'smart';
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'admin_login', 'plugin' => false);
}
$this->Auth->allow('register');
}
}
Edit 1:
For cakephp 3.0 we can use
public function beforeFilter(Event $event) {
$this->Auth->sessionKey ='Auth.User';
}
in AppController.php to set different sessionKey for frontend and backend.
Why do you have multiple load calls for the same plugin anways? There should be only one per plugin!
That being said, mind your casing, the second CakePlugin::load() call uses webmaster instead of Webmaster. Plugin names should start with an uppercased letter, just like the corresponding directory name.
Your local filesystem is most probably case insensitive, so it can find the plugin directory even though the casing doesn't match.
Update It looks like I intially got you wrong, if CakePHP would tell you to load the plugin webmaster without you already having added a CakePlugin::load('webmaster') call, then you must have used the lowercased webmaster somewhere else in your code.
I am using CakePHP v2.x. My current Database is MySQL. My need is to connect to other Database within a method/function written under controller. So what I did so far is add another DB connection array $test in Config/database.php.
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'test',
'password' => 'test1',
'database' => 'test_portal',
'prefix' => ''
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'dfffd_23',
'password' => 'dsfsd324',
'database' => 'testdbuser',
'prefix' => ''
//'encoding' => 'utf8',
);
}
I need to connect a table named 'aezips'.So I created a new Model;
class Aezips extends AppModel {
public $name = 'Aezip';
public $useDbConfig = 'test';
//public $useTable = 'aezips';
}
In my controller added ;
public $uses = array('Aezip');
Controller having a function/method named add,
public function add() {
$this->layout = NULL;
$this->autoRender = false; //will prevent render of view
if($this->RequestHandler->isAjax()){
Configure::write('debug', 0); //it will avoid any extra output
}
//$this->Aezip->useDbConfig('test');
$t = $this->Aezip->find('all'); //To fetch data from aezips table
print_r($t);
print_r($this->data);
}
But I can't connect to Aezip table and it doesn't shows any error. Both Database residing from same server but different cpanel account.
You seem to have used the wrong name for your Model (plural instead of singular);
class Aezips extends AppModel {}
Should be:
class Aezip extends AppModel {}
And the right filename should be app/Model/Aezip.php
Also, be sure to enable 'debug' inside app/Config/core.php so that you can see the queries that are performed. Also, this will refresh the CakePHP caches more often, which will reduce the risk of using 'cached' Model definitions.
You can debug your variables using debug($variable); in stead of print_r($variable);
I've written static pages component for my application, where admins can dynamically add/edit/remove static content pages. these are saved in the database.
(e.g. you can create a page called "about" and can visit it at myapplication/about)
This is my routing for these pages:
$page = new StaticPage();
$slugs = $page->find('list', array(
'fields' => array('slug'),
'recursive' => -1,
'order' => 'StaticPage.slug DESC',
));
Router::connect('/:slug',
array('controller' => 'static_pages', 'action' => 'display'),
array(
'pass' => array('slug'),
'slug' => implode($slugs, '|')
)
);
Now i have the problem, that when you create a page which slug matches an existing controller (e.g. users), it overwrites the Route to the UsersController.
so i need something like a blacklist or similar: i began to write a validation rule, where i want to check if that controller exists. for cake 1.3 there was a function "loadController" which return false, if the controller did not exist, but for cake 2.x there is no such an function. am i missing this somehow? does it have a new name or is in a utility library now?
Or are there better ways to solve this?
you should try this : http://www.cleverweb.nl/cakephp/list-all-controllers-in-cakephp-2/
and by getting the list of all controllers you can easily exclude the name of controllers
This is my validation method for now:
$route = Router::parse($check['slug']);
$controllerName = Inflector::camelize($route['controller'] . 'Controller');
$aCtrlClasses = App::objects('controller');
foreach ($aCtrlClasses as $controller) {
if ($controller != 'AppController') {
// Load the controller
App::import('Controller', str_replace('Controller', '', $controller));
// Load the ApplicationController (if there is one)
App::import('Controller', 'AppController');
$controllers[] = $controller;
}
}
if (in_array($controllerName, $controllers)) {
return false;
} else {
return true;
}
I'm trying to send an email from AppController in my CakePHP 2.0 app. It works fine if I send it from the PagesController, but I need to be able to send from AppController as well.
I have:
function sendSystemEmail($to = EMAIL_CONTACT, $from = EMAIL_FROM, $subject = null, $body = null, $view = null, $vars = null) {
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->viewVars(array(
'body' => $body,
'vars' => $vars
));
$email->template($view)
->emailFormat('html')
->from($from)
->to($to)
->subject($subject)
->send();
return;
}
When I use this, I don't get any errors, but the email doesn't arrive. I can't see that there's any different between this and the code I have in PagesController, so I'm assuming that there's something that AppController doesn't have access to maybe? I can't figure out what though!
Sharon, you're not putting the configuration method.. this parameter can put in the constructor of classEmail too, this way..
$email = new CakeEmail('pop3'); //can be smtp or the protocole you are using..
or
$email->config('pop3')
and you need to define the connection here..
./app/Config/email.php
class EmailConfig {
public $pop3 = array(
'transport' => 'Mail',
'from' => 'you#localhost',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
In Cake 1.3, the EmailComponent did what it should do. The new Cake Email class in 2.0 turned out to be a frustration....No emails sent, No errors....vague documentation...
I have tried all possible variants, tried it with my SMTP, Mail() and Gmail, nothing happens. Hereby my latest attempt:
Controller snippet:
//on top of page
App::uses('CakeEmail', 'Network/Email');
//in method
$email = new CakeEmail();
$email->template('contact_email')
->emailFormat('text')
->to('my#gmail.com')
->from('other#gmail.com')
->send();
Email.php Config file:
class EmailConfig {
//It also does not work with a constructor
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'my#gmail.com',
'password' => '***',
'transport' => 'Smtp'
);
Can someone please post WORKING code for the Email Class. Many thanks
I think you have to load your config from Config/email.php explicitly, it is not loaded automatically, not even the default config:
$email = new CakeEmail();
$email->config('default');
//or in constructor::
$email = new CakeEmail('default');
In my opinion you should use this:
$email = new CakeEmail('gmail');
This is my email config file . I didnt do any change here
class EmailConfig {
public $default = array(
'transport' => 'Mail',
'from' => 'Admin <no-reply#example.com>',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
}
This is how i send the mail
$email = new CakeEmail();
$result = $email->template('welcome')
->emailFormat('text')
->to($NewUser['email'])
->from('admin#example.com')
->send();
var_dump($result);