I got this eror while trying to send email with cakeMail. Although I have following code from the cookbook 1 on 1.
Here are some snippets from my controller:
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
...
...
...
public function index() {
....
$Email = new cakeEmail('smtp');
$Email->template('MassMail')
->emailFormat('html')
->viewVars([
'content' => $content
])
->from(['info#forkom-jerman.org' => 'Forkom Jerman'])
->to($to)
->subject($subject)
->replyTo('forkom.jerman#gmail.com')
->transport('smtp');
if ($Email->send()) {
$this->Flash->set('Email Telah terkirim');
} else {
$this->Flash->set('Email tidak bisa terkirim');
}
}
On the email.php on config folder:
class EmailConfig {
public $smtp = array(
'transport' => 'Smtp',
'from' => 'info#forkom-jerman.org',
'host' => 'send.one.com',
'port' => 465,
'username' => '*****#gmail.com',
'password' => '******',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
}
Please tell me where it went wrong.
The class to use is CakeEmail instead of cakeEmail.
You need to instantiate it like this:
$Email = new CakeEmail('smtp');
Related
I am wrestling with CakePHP trying to create a basic login functionality. So far CakePHP is winning. I followed the basic Blog tutorial and based on that I am try to create a similair login thingy.
The only difference I have is that I not using an Users model, but a custom Employers model and I use email/password instead of username/password.
Yet all I get is "Your username or password was incorrect."
AppController.php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'schedules',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'employers',
'action' => 'login',
'home'
),
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'email',
'password' => 'password'
),
'userModel' => 'Employer',
'passwordHasher' => 'Blowfish'
)
)
)
);
}
EmployersController.php
<?php
App::uses('AppController', 'Controller');
class EmployersController extends AppController {
public $helpers = array('Form');
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('login');
}
public function login()
{
$layout = 'login';
$this->layout = $layout;
if($this->request->is('post'))
{
debug($this->Auth->login());
if($this->Auth->login())
{
return $this->redirect($this->Auth->redirect());
}
else
{
$this->Session->setFlash('Your username or password was incorrect.');
}
debug($this->request->data['Employers']['password']);
}
}
}
Login.ctp
<div id="login-container">
<h1>Login</h1>
<?php
echo $this->Form->create('Employers');
echo $this->Form->input('email', array('label' => false, 'placeholder' => 'Email'));
echo $this->Form->input('password', array('label' => false, 'placeholder' => 'Password'));
echo $this->Form->submit();
echo $this->Form->end();
?>
When I debug $this->request->data, the data is structered like data["Employers"]['email'] & data["Employers"]['password']. This is propably not right, since my model is called Employer.
Is this correct and does the login functionality break on this and if so, how can I fix that?
Or is there something else I am overlooking.
In your login.ctp,
It should be echo $this->Form->create('Employer'); not with s, so remove s and try it.
Hope it helps.
In my model I'm sending an email once the User is created:
Model/User.php:
<?php
App::uses('AppModel', 'Model');
App::uses('CakeEmail', 'Network/Email');
class User extends AppModel {
...
private function sendWelcomeMail($name, $email, $password) {
$Email = new CakeEmail('smtp');
$Email->viewVars(array('name' => $name, 'password' => $password));
$Email->template(('welcome'));
$Email->emailFormat('html');
$Email->from(array('info#staycomputer.de' => 'Stay Computer'));
$Email->to($email);
$Email->subject('Stay Serviceordersystem: Willkommen / Welcome');
$Email->send();
}
}
Config/email.php:
public $smtp = array(
'transport' => 'Smtp',
'from' => array('info#***.de' => 'Stay Computer'),
'host' => '***',
'port' => 25,
'timeout' => 30,
'username' => '***',
'password' => '***',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
It's working fine on my testing system but not on production system (1&1 webhosting):
Error: An Internal Error Has Occurred.
According to error.log:
Error: [SocketException] Connection timed out Request URL ...
and
15:52:36 Error: Fatal Error (256): [CakeException] Unknown status code #0 /homepage/30/d20974062/htdocs/StaywebDB/serviceordersystem/lib/Cake/Error/ExceptionRenderer.php(212) ...
There is only 1 difference:
In production system I use the built in re-writing function.
Switching to gmail worked like a charm. Thanks for the recommondations.
I've tried every simple cakedc search setup, and followed code samples from previous posts on this but it just won't seem to cooperate for me. I'm not sure, but there doesn't seem to be any search query generated with the search string, judging from a query log I have running on my database.
When I try to search, from /books/search, I get no results on a title I know is in the database and the URL changes to /books/index/title:sweet (I'm not sure if that is relevant).
Any help would be much appreciated.
Model
public $actsAs = array('Search.Searchable');
public $primaryKey = 'isbn13';
public $hasMany = array(
'Contributor' => array (
'className' => 'Contributor',
'foreignKey' => 'isbn13'
),
'Override' => array (
'className' => 'Override',
'foreignKey' => 'isbn13'
)
);
public $filterArgs = array(
'title' => array('type' => 'query', 'method' => 'filterTitle'),
'main_desc' => array('type' => 'value')
);
public function filterTitle($data, $field = null) {
if (empty($data['title'])) {
return array();
}
$titleField = '%' . $data['title'] . '%';
return array(
'OR' => array(
$this->alias . '.title LIKE' => $titleField,
));
}
Controller
class BooksController extends AppController {
public $helpers = array ('Html', 'Form');
public $paginate = array();
public $components = array('Search.Prg');
public $presetVars = true;
public function index() {
//get books
$this->set('books', $this->Book->find('all'));
//search
/* $this->Prg->commonProcess();
$this->paginate['conditions'] = $this->Book->parseCriteria($this->Prg->parsedParams());
$this->set('books');
*/
}
public function search($books = NULL) {
$this->Prg->commonProcess();
$this->paginate['conditions'] = $this->Book->parseCriteria($this->passedArgs);
$this->set('books', $this->paginate());
}
View
<h1>Search Results</h1>
<?php
echo $this->Form->create('Book' , array(
'url' => array_merge(array('action' => 'search'), $this->params['pass'])
));
echo $this->Form->input('title', array('div' => false, 'empty' => true)); // empty creates blank option.
echo $this->Form->submit(__('Search', true), array('div' => false));
echo $this->Form->end();
Routes
Router::connect('/', array('controller' => 'books', 'action' => 'index'));
Router::connect('/index/*', array('controller' => 'books', 'action' => 'search'
));
Router::connect('/view/:url', array('controller' => 'books', 'action' => 'view', array('url' => '[\w\W]*')
));
Router::connect('/edit/:url', array('controller' => 'books', 'action' => 'edit', array('url' => '[\w\W]*')
));
Router::connect('/whatsnew/*', array('controller' => 'books', 'action' => 'whatsnew'
));
Router::connect('/search/*', array('controller' => 'books', 'action' => 'search'
));
I've renamed the search-master folder to Search and put it in my plugins directory and In my bootstrap.php I've loaded the plugin
CakePlugin::load('Search');
Fixed! Needed
$this->set('books', $this->paginate($this->Book->parseCriteria($this->passedArgs)));'
instead of
$this->set('books', $this->paginate());'
in the controller. Not sure why though..
As you might know, when you create a new project with CI you'll have to manually enter base url, encryption key in config/config.php. I'm trying to overcome this and is hence looking for a way to read those values from a database instead - making the installation for a customer and the set up time as a whole decrease a lot.
A customer isn't able to edit a PHP file's variables, but is most likely able to, with some guidance, enter base url and have a encryption key automatically filled in by the system.
Is there any way to accomplish this?
Of course! Add a hook - post_controller and set these config values through that file.
config/hooks.php
$hook['pre_controller'][] = array( 'class' => 'MyOtherClass',
'function' => 'MyOtherfunction',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks');
hooks/myotherclass.php
<?
class MyOtherClass {
function MyOtherfunction() {
$CI =& get_instance();
$row = $CI->db->get_where('configs', array('site_id' => 1))->row();
$CI->config->set_item('base_url', $row->base_url);
}
}
Basicly you set these values before they're used in any controller or similiar.
As far Codeigniter 3.1.7 pre_controller won't work because it will return NULL value while initializing $CI =& get_instance();
To solve this:
Change the hook point from pre_controller to post_controller_constructor
Modified source:
config/hooks.php
$hook['post_controller_constructor'][] = array( 'class' => 'MyOtherClass',
'function' => 'MyOtherfunction',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks');
and in hooks/myotherclass.php
<?
class MyOtherClass {
function MyOtherfunction() {
$CI =& get_instance();
$row = $CI->db->get_where('configs', array('site_id' => 1))->row();
$CI->config->set_item('base_url', $row->base_url);
}
}
Now it will work!
class name extends CI_Controller {
public $DB1;
public $MSSQL;
public function __construct()
{
parent::__construct();
$userid = $this->session->userdata('userids');
$this->load->model('kubix');
$result = $this->kubix->databaseconncetion($userid);
foreach ($result as $deta)
{
$this->MSSQL=$db['MSSQL'] = array(
'hostname' => $deta["Db_base_ip"],
'username' => $deta["Db_username"],
'password' => $deta["db_password"],
'database' => $deta["Db_base_name"],
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE);
$this->DB1 = $this->load->database($this->MSSQL, TRUE);
$sql = "SELECT TOP 1 * FROM AccountsFinancialYearMaster where Active=1 ";
$query = $this->DB1->query( $sql);
$result= $query->result_array();
RETURN $result;
This is a fairly long question but I have know idea where it's going wrong. I am making an ajax login script using CakePHP 2.0 but it keeps failing. I will post all of my code, and i hope someone has the time to go through it.
This is my sql Database
AccountID AccountEmail AccountPassword AccountActive
1 chris#hotmail.co.uk pass 0
2 chris#gmail.com pass 1
This is my relevant Model Code
class Account extends AppModel {
public $name = 'Account';
public $validate = array(
'AccountEmail' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Please Enter A Valid Email.'
),
'email' => array(
'rule' => array('email', true),
'message' => 'Please supply a valid email address.'
)
),
'AccountPassword' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Please Enter A Valid Password.'
)
)
);
}
This is my relevant Controller Code
class AppController extends Controller {
/**
* Class Variables
*/
public $helpers = array('Js', 'Html', 'Session', 'Form');
public $components = array(
'Session',
'RequestHandler',
'Auth' => array(
'logoutRedirect' => array(
'controller' => 'Accounts',
'action' => 'login'
),
'authError' => 'You can\'t Access That Page',
'authorize' => array('Controller'),
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'AccountEmail',
'password' => 'AccountPassword'
),
'scope' => array('AccountActive' => '1')
)
)
)
);
}
class AccountsController extends AppController {
/**
* Class Variables
*/
public $name = 'Accounts';
public $layout = 'Accounts';
/**
* Class Functions
*/
public function login()
{
if ($this->request->is('ajax')) {
$this->Account->set($this->data);
if ($this->Account->validates()) {
if($this->Auth->login()) {
echo "logged In";
exit;
} else {
echo "Login Failed";
exit;
}
} else {
echo 'validation/' . json_encode($this->Account->invalidFields());
exit;
}
}
}
I don't think there is anything else. Again i'm sorry for the huge amount of code but i just don't know what you need.
The info is all passed via 'echo' to jquery which at the moment is just displaying the response via 'alert'.
I know the validation is working, but if i enter the info of someone who should be able to login it just shows "Login Failed". Thanks For Your Time.
Your passwords in the database need to be in their hashed form. Using Cake's default settings, 'pass' would be: 1c31af5bd9913ff511fe780f506e6fab68979b90