CakePHP - can't email via SMTP - cakephp

pulling my hair out with this one.
When I developed this cakephp (2.2) app on my local machine, I could email via gmail using smtp without a problem. Since then I've deployed it onto a 1and1 server and I can no longer send. What even more frustrating is that I cant seem to get any decent debugging info from cake.
Heres the code:
if($fu['name']) {
$url = Router::url('/admin/', true );
$ms = $url;
$ms = wordwrap($ms,1000);
//============Email================//
// SMTP Options
$this->Email->smtpOptions = array(
'port'=>'465',
'timeout'=>'30',
'host' => 'ssl://smtp.gmail.com',
'username'=>'myemail#gmail.com',
'password'=>'password'
);
$this->Email->template = 'newExpenseClaim';
$this->Email->from = 'Expense Tracker <myemail#email.co.uk>';
$this->Email->to = 'myemail#fakeemail.com';
$this->Email->subject = 'New Expense Claim Submitted - Please Review';
$this->Email->sendAs = 'both';
$this->Email->delivery = 'smtp';
// Set username & url in email
$this->set('user', $fu['name']);
$this->set('ms', $ms);
//$this->Email->send();
if(!$this->Email->send()) {
CakeLog::write('debug', $this->Email->smtpError);
}
$this->set('smtp_errors', $this->Email->smtpError);
//============EndEmail=============//
}
I get the following debug info each times it fails (printed on screen, nothing goes into the debug log which is massively anoying).
Connection refused
Error: An Internal Error Has Occurred.
Stack Trace
CORE/Cake/Network/Email/SmtpTransport.php line 95 → CakeSocket->connect()
CORE/Cake/Network/Email/SmtpTransport.php line 60 → SmtpTransport->_connect()
CORE/Cake/Network/Email/CakeEmail.php line 1059 → SmtpTransport->send(CakeEmail)
CORE/Cake/Controller/Component/EmailComponent.php line 345 → CakeEmail->send(null)
APP/Controller/ExpenseClaimsController.php line 236 → EmailComponent->send()
APP/Controller/ExpenseClaimsController.php line 278 → ExpenseClaimsController->notifyAdminsOfNewClaims()
[internal function] → ExpenseClaimsController->add()
CORE/Cake/Controller/Controller.php line 485 → ReflectionMethod->invokeArgs(ExpenseClaimsController, array)
CORE/Cake/Routing/Dispatcher.php line 186 → Controller->invokeAction(CakeRequest)
CORE/Cake/Routing/Dispatcher.php line 161 → Dispatcher->_invoke(ExpenseClaimsController, CakeRequest, CakeResponse)
APP/webroot/index.php line 92 → Dispatcher->dispatch(CakeRequest, CakeResponse)
Can anyone help? Thanks in advance
** Updated with wrapper and soctet info **
These are the wrappers and sockets I get returned from those functions:
Wrappers:
array(
(int) 0 => 'https',
(int) 1 => 'ftps',
(int) 2 => 'compress.zlib',
(int) 3 => 'compress.bzip2',
(int) 4 => 'php',
(int) 5 => 'file',
(int) 6 => 'data',
(int) 7 => 'http',
(int) 8 => 'ftp',
(int) 9 => 'zip'
)
Sockets
array(
(int) 0 => 'tcp',
(int) 1 => 'udp',
(int) 2 => 'unix',
(int) 3 => 'udg',
(int) 4 => 'ssl',
(int) 5 => 'sslv3',
(int) 6 => 'sslv2',
(int) 7 => 'tls'
)

In first you should check what your hosting support outgoing connection. Simple check is similar
echo implode("", file("http://google.com"));
In second you should check what PHP support SSL connection and it have stream wrapper for SSL.
I now not remember how you can check it. But search in google by keywords: PHP stream wrapper SSL

Related

Form data is hashed in CakeRequest after upgrade to cakephp2.x

I just recently performed an upgrade from cakephp 1.3 to 2.06. However when I attempt to access form data in my controller, the data is hashed like so:
2015-10-15 08:43:57 Error: CakeRequest Object
(
[params] => Array
(
[plugin] =>
[controller] => custom_users
[action] => ajax_login
[named] => Array
(
)
[pass] => Array
(
)
[isAjax] => 1
)
[data] => e1fe8b4c1724c30148e306708f27b30909f23d45
I checked that the form is correctly formed on the front end.
The documentation indicates that I should be able to access my data this way, but for some reason this behaviour is happening in stead. Any ideas?
I've added the trace I get when I try and submit the form:
Notice: Array to string conversion in /var/www/html/academy/lib/Cake/Utility/Security.php on line 92
Notice: Undefined offset: 1 in /var/www/html/academy/lib/Cake/Network/CakeResponse.php on line 456
Warning: Illegal string offset 'CustomUser' in /var/www/html/academy/app/Controller/CustomUsersController.php on line 569
Warning: Illegal string offset 'username' in /var/www/html/academy/app/Controller/CustomUsersController.php on line 569
Warning: Illegal string offset 'CustomUser' in /var/www/html/academy/app/Controller/CustomUsersController.php on line 570
Warning: Illegal string offset 'password' in /var/www/html/academy/app/Controller/CustomUsersController.php on line 570
Warning: implode(): Argument must be an array in /var/www/html/academy/lib/Cake/Utility/Security.php on line 92
Fatal error: Call to undefined method CustomAuthComponent::getModel() in /var/www/html/academy/app/Controller/Component/CustomAuthComponent.php on line 242

CakePHP file download corrupted

I have a site where users can upload images and then download them. I have moved my project on a live web host but the problem is that when I try to download the file I just uploaded, it downloads it but cant open it afterwards, it says it is corrupted... I had no problems in local though.
heres my download action:
public function getfile($accesskey) {
$this->autoRender = false;
if ($accesskey != null) {
$data = $this->getfileinfo($accesskey);
if (!empty($data)) {
$this->response->file(WWW_ROOT.$data['Document']['dlpath'], array('download' => true, $data['Document']['name']));
return $this->response;
} else {
$this->redirect(array('controller' => 'documents', 'action' => 'upload'));
}
} else {
$this->redirect(array('controller' => 'documents', 'action' => 'upload'));
}
}
$data['Document']['dlpath'] contains the following:
/home/vol2_2/byethost17.com/b17_15357734/htdocs/app/webroot/uploads/2014/09/26/filekey/imagename.extension
this is the first time i'm doing this so i might have made a mistake.. the path is wrong or something i'm confused
EDIT: It seems that the error i was getting was much deeper than it seemed. I opened the corruted file in a text editor and saw some error concerning set_time_limit() and php safe mode...
here's the error:
Warning (2) : set_time_limit() has been disabled for security reasons [ CORE/Cake/Network/CakeResponse.php , line 1458 ] Code Context
set_time_limit - [internal], line ??
CakeResponse::_sendFile() - CORE/Cake/Network/CakeResponse.php, line 1458
CakeResponse::send() - CORE/Cake/Network/CakeResponse.php, line 429
MediaView::render() - CORE/Cake/View/MediaView.php, line 96
Controller::render() - CORE/Cake/Controller/Controller.php, line 954
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 198
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 165
[main] - APP/webroot/index.php, line 108

cakephp dropdown box and arrays

Hi all I have a dropdown box on a view. I have a find statement that returns a list of templates, when I debug the find, it prints out the correct list of template.id and template.name however when I submit the form it carries across the number it is in the list for example if I select the 5th template in a list it saves the template_id = 5 NOT the actual template id number.
//Retrieve Account Id of current User
$accountid=$this->Auth->user('account_id');
//conditions for template arrays
$conditions=
array('AND' => array(
array('Template.account_id' => $accountid),
array('Template.active' => 1)
));
//An array of all Templates belonging to the current User's Account
$templates=$this->Template->find('list', array(
'conditions' => $conditions));
when I debug $templates I get this print out
array(
(int) 1 => 'Consulting- Bus',
(int) 2 => 'Consulting- Pers',
(int) 7 => 'ClientEdge',
(int) 8 => '11111',
(int) 9 => 'testSUn',
(int) 10 => 'Test Bruce Review',
(int) 11 => 'Test Bruce 3 = Final'
when I select for example 'Test Bruce Review' and hit submit and debug the value it prints out '6', the 6th item in the dropdown box when I want it printing out 10 when I debug it.
here is the snippet from the form relating to this dropdown box
<tr><td><?php echo "Template: "?></td>
<td><?php echo $this->Form->input('template_id', array('label'=>false,'type'=>'select','options'=>$templates));?></td></tr>
how can I remedy this issue?
I had an issue with merging my arrays that caused the death of my template.id

cake php element cache

I can't make element caching work:
echo $this->element('categorytree', array(
'cache' => array(
'key'=>'categorytree-cache','time' => '+1 hour'
)
))
I checked core.php:
// In development mode, caches should expire quickly.
$duration = '+999 days';
/*if (Configure::read('debug') >= 1) {
$duration = '+10 seconds';
}*/
And commented as you see - to prevent short caching in debug mode...
But still when I refresh page the SQL query that cached element does by requestAction() is shown... So no caching... why?
I may be wrong here, but I think the correct call would be:
echo $this->element('categorytree', array(), array(
'cache' => array(
'key'=>'categorytree-cache','time' => '+1 hour'
)
))
Notice the second argument is element params while the third is element options (what you need here).
You can see the function signature here.

Instance for File is not working in cakephp

i am getting the error as
Array to string conversion [CORE/cake/libs/file.php, line 96]
$path = array(
"name" => "23_50_11[1].gif",
"type" => "image/gif",
"tmp_name" => "/tmp/phpbBWxAT",
"error" => 0,
"size" => 25230
)
$create = false
$mode = 493
dirname - [internal], line ??
File::__construct() - CORE/cake/libs/file.php, line 96
FormsController::add() - APP/controllers/forms_controller.php, line 528
Object::dispatchMethod() - CORE/cake/libs/object.php, line 117
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 245
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 211
require - APP/webroot/index.php, line 88
[main] - CORE/index.php, line 61
Notice (8): Array to string conversion [CORE/cake/libs/file.php, line 97]
$path = array(
"name" => "23_50_11[1].gif",
"type" => "image/gif",
"tmp_name" => "/tmp/phpbBWxAT",
"error" => 0,
"size" => 25230
)
$create = false
$mode = 493
is_dir - [internal], line ??
File::__construct() - CORE/cake/libs/file.php, line 97
FormsController::add() - APP/controllers/forms_controller.php, line 528
Object::dispatchMethod() - CORE/cake/libs/object.php, line 117
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 245
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 211
require - APP/webroot/index.php, line 88
[main] - CORE/index.php, line 61
Warning (2): basename() expects parameter 1 to be string, array given [CORE/cake/libs/file.php, line 98]
The code that i have used is,
<h1>Form Fill</h1>
<?=$form->create('Form',array('type'=>'file'));?>
<?=$form->input('date',array('label'=>'Publication Date '));?>
<?=$form->input('headline');?>
<?=$form->input('content');?>
<?=$form->input('image',array('type'=>'file'));?>
<?=$form->end('Submit');?>
in the Formscontroller
function add($formid)
{
echo "Imaage ".$this->data['Form']['image'];// shows me the Imaage Array
if ($this->data['Form']['image']) {
$file = new File($this->data['Form']['image']);// this is not working
//$ext = $file->ext();
//echo "Extension ".$ext;
echo "Fiel ".$file;
$date = $this->data['Form']['date'];
$filename = $date['year'].'-'.$date['month'].'-'.$date['day'].'-form-image.'.'gif';
$data = $file->read();
echo "Data ".$data;
$file->close();
$file = new File(WWW_ROOT.'/img/'.$filename,true);
$file->write($data);
$file->close();
}
}
Instance for File is not working?? Please help me . actually i m trying to upload the image FIle and i m using Ubuntu linux machine ..
To recap:
$this->data['Form']['image']; // shows me the Imaage Array
$file = new File($this->data['Form']['image']); // this is not working
Error: Array to string conversion
The problem:
new File() expects a string as argument, but you're giving it an array.
Solution:
Supply a string as argument for new File(), preferably the path of the file:
$file = new File($this->data['Form']['image']['tmp_name']);
To be a little more detailed and help you fix these kinds of problems yourself next time (hopefully), it's not actually File that expects a string argument. If you look at the trace again:
dirname - [internal], line ??
File::__construct() - CORE/cake/libs/file.php, line 96
FormsController::add() - APP/controllers/forms_controller.php, line 528
...
This says the problem actually occurred in dirname, which is a PHP internal function. It was called on line 96 in File::__construct, which in turn was called from FormsController::add etc. pp.
Since PHP is a dynamic language it doesn't usually crap out completely when you supply arguments of the wrong type. Rather it'll try to make the best out of it and actually try to convert an array to a string, and just notify you that it did so with Notice (8): Array to string conversion. Since the result of an Array -> String conversion is literally the string "Array", your program will usually crap out later, since now you're trying to do things like is_dir("Array"), which is nonsense.
As you can see the program actually struggles on, with quite a few notices, and basename() on line 98 even complaining a little harder with a Warning:
94: function __construct($path, $create = false, $mode = 0755) {
95: parent::__construct();
96: $this->Folder =& new Folder(dirname($path), $create, $mode);
97: if (!is_dir($path)) {
98: $this->name = basename($path);
Warning (2): basename() expects parameter 1 to be string, array given [CORE/cake/libs/file.php, line 98]
Now, it would be good style for File to check if the parameter actually is a string or not and fail gracefully, but it doesn't bother. Because as a developer you should read the documentation to see what the function expects. If it's not on the website, look directly in the file /cake/libs/file.php:
/**
* Constructor
*
* #param string $path Path to file
* #param boolean $create Create file if it does not exist (if true)
* #param integer $mode Mode to apply to the folder holding the file
* #access private
*/
function __construct($path, $create = false, $mode = 0755) {
...

Resources