CakePHP 2.0: CakeEmail frustration - cakephp-2.0

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);

Related

Class 'cakeEmail' not found - cakephp2

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');

how to load a component in Lib or Vendor files

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 :)

Multiple Database connection issue CakePHP v2.x

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);

MSSQL Access From Zend2 via Linux

I'm upgrading an application that presently runs on ZendFramework1(ZF1) to ZendFramework2(ZF2). I'm having trouble getting DB results to return from the ZF2 connection.
In ZF1 this test works perfectly:
$db = Zend_Db::factory('Pdo_Mssql', array(
'host' => 'ServerNameFromFreeTdsConfig',
'charset' => 'UTF-8',
'username' => 'myUsername',
'password' => 'myPassword',
'dbname' => 'database_name',
'pdoType' => 'dblib'
));
$stmt = $db->prepare("select * from Products");
$stmt->execute();
$result = $stmt->fetchAll();
$stmt->closeCursor();
However, I've been trying this in ZF2 but I'm not really getting anywhere. In my config\autoload\global.php I have:
return array(
'db' => array(
'host' => 'ServerNameFromFreeTdsConfig',
'charset' => 'UTF-8',
'dbname' => 'database_name',
'username' => 'myUsername',
'password' => 'myPassword',
'driver' => 'pdo',
'pdodriver' => 'dblib',
),
);
And in the Module.php file:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$config = $e->getApplication()->getServiceManager()->get('Configuration');
$dbAdapter = new Adapter($config['db'], new SqlServer());
GlobalAdapterFeature::setStaticAdapter($dbAdapter);
}
Then in the Model\Products.php
class Products extends AbstractTableGateway
{
protected $table;
protected $featureSet;
public function __construct($table = 'Products') {
$this->table = $table;
$this->featureSet = new FeatureSet();
$this->featureSet->addFeature(new GlobalAdapterFeature());
$this->initialize();
}
//Test the connection.
public function getProducts() {
$result = $this->getAdapter()->query("select * from Products", Adapter::QUERY_MODE_EXECUTE);
die(var_dump($result));
}
}
It looks like it is connecting because the "var_dump" above returns a ["fieldCount":protected]=> int(7) which is correct (there are 7 columns in that table). However, it is not returning any results.
What might I need to do to get this to work in ZF2? Do I need to somehow extend Zend\Db\Adapter\Adapter using code from the ZF1 Zend_Db_Adapter_Pdo_Mssql.php file? Or is there some simple solution I'm missing?
Thanks for any insight.
I think you dont need to mention the user name and password
resources.db.adapter = "sqlsrv"
resources.db.host = "localhost\SQLEXPRESS"
resources.db.dbname = "DatabaseName"
resources.db.isDefaultTableAdapter = true
resources.db.driver_options.ReturnDatesAsStrings = true
I ended up writing my own Adapter adopted from the Zend Framework 1 adapter I had working there. If someone comes across this post looking for a solution to the same problem and would like a copy of the code I transposed, let me know. There is quite a lot of code or I'd post it here.

How do I send email from AppController in CakePHP 2.0?

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',
);

Resources