cakephp How to change the X-Mailer - cakephp

Using cakePHP version 2.3.8,
I am trying to change the X-Mailer: CakePHP Email into X-Mailer: PHP mail.
I have tried this in the controller and in the /app/Config/email.php in my 'default' settings.
'X-Mailer' => 'PHP mail',
But not able to get it changed, really frustrating.

You need to use addHeaders() to change it prior to sending the email.
As done here, for example:
https://github.com/dereuromark/tools/blob/master/Lib/EmailLib.php#L638
In this case - extending the core class - one can use Configure to automatically populate the X-Mailer via configs. But you can also do it inline for each email sending functionality.
Basically, on your CakeEmail object:
$CakeEmail = new CakeEmail();
$CakeEmail->addHeaders(array('X-Mailer' => 'My custom X-Mailer'));
If set manually Cake will not add his default value 'CakePHP Email'.

CakePHP 2,
Instead of setting it every time from a controller as suggested by #mark,
you can set in the EmailConfig class, located in Config/email.php:
public $default = array(
'transport' => '...',
'from' => '...',
'emailFormat' => 'both',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
'headers'=>array('X-Mailer'=>'Your App Name'),
);
Will send an email with these headers:
To: .....
X-Mailer: Your App Name
Date: ....

Related

How to have different dashboards based on roles with cakedc plugins / users & acl

I am using CakeDC Users & ACL plugins in my CakePhp app. I have different roles for my users in my app and I would like to have different dashboards based on roles after login.
I extend the plugin with my own table and controller based on the documentation here, so I have MyUsersController and MyUsersTable which override the initial files of the plugin, UsersController and UsersTable. Everything works fine. I create an event in my events.php file which contains:
use CakeDC\Users\Controller\Component\UsersAuthComponent;
use Cake\Event\Event;
use Cake\Event\EventManager;
EventManager::instance()->on(
UsersAuthComponent::EVENT_AFTER_LOGIN,
['priority' => 99],
function (Event $event) {
if ($event->data['user']['role_id'] === 'bbcb3031-ebed-445e-8507-f9effb2de026') //the id of my client role{
return ['plugin' => 'CakeDC/Users', 'controller' => 'MyUsers', 'action' => 'index', '_full' => true, 'prefix' => false];
}
}
);
But it seems like the override is not working because I have an error:
Error: CakeDC/Users.MyUsersController could not be found.
In my URL I have /users/my-users instead of /my-users and I don't know why. I have test with a template file which is include in the plugin and the Users controller like this:
function (Event $event) {
if ($event->data['user']['role_id'] === 'bbcb3031-ebed-445e-8507-
f9effb2de026') //the id of role{
return ['plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'profile';
}
And it works. My URL redirect after login as a client is /profile.
Could someone help me to understand? Please tell me if it's not clear enough and if it's missing parts of codes that might be important to understand my problem.
I specify that I am beginner with Cake.
Your custom controller doesn't live in the CakeDC/Users plugin, hence you must disable the plugin key accordingly, so that the correct URL is being generated (assuming your routes are set up correctly) that connects to your controller, like this:
[
'plugin' => null,
'controller' => 'MyUsers',
'action' => 'index',
'_full' => true,
'prefix' => false
]
That would for example match the default fallback routes, generating a URL like /my-users.
See also:
Cookbook > Routing > Creating Links to Plugin Routes

How to create a cron shell for sending email with cakephp 3.6

I want cake to retrive for exemple a pdf invoice from a user and send email every 10 days
So as far as I understand for the sender
I made changement Inside
app.php
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
'host' => 'ssl://smtp-mail.outlook.com',
'port' => 587,
'timeout' => 30,
'username' => exemple#live.fr,
'password' => exemple,
'client' => null,
'tls' => yes,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
Then I have to create the the Task Either Inside src\console or src\shell (not sure about thoses two)
Now do I have to create a Table email sql ? what changes to be applied for The UsersController
Exemple would Be appreciated
Note : documentation didn't help at all
I tried this way but its outdated
CakePHP send email
you should create inside src\shell and
format send mail
$email = new Email();
$email
->setEmailFormat('html')
->setTo($email)
->setFrom($your_email)
->setSubject($subject)
->send($content);
I used PHPMailer library
https://github.com/PHPMailer/PHPMailer
And then followed this tutoriel now
https://www.youtube.com/watch?v=RzqfpT8gv0w&list=PLmrTMUhqzS3iwbxyd61TM4qxej4zfvKeo
now I'm able to send email with Cron Shell

How to use Auth component of Cakephp 3 in Event

I'm using CakePHP 3.2 and proffer plugin for image uploading.
I want to rewrite the default path of proffer plugin to upload image and change image name before save.
As per the documentation of proffer from github. I have created an event in /src/Event
Now I want to rename the file like
$this->Auth->user('id').'-'.$row('id').date('dmyhis').ext
this is what I have done
$newFilename = $this->Auth->user('id').'-'.$event->subject()->get('id') . '_' . Inflector::slug($event->subject()->get('name')) . date('ymdhis') . $ext;
But this is giving error that Auth can not be used here. Is there any way to use Auth Component outside controller ?
You can access the logged in user id by loading the session.
use Cake\Network\Session;
$session = new Session();
$userData = $session->read('Auth.User.id');
Use this as a reference: Reading & Writing Session Data
With cake 3.x and after, you should using this:
$this->request->session()->read('Auth');
When debug it
debug( $this->request->session()->read('Auth') );
[
'id' => (int) 2,
'username' => 'admin',
'nice_name' => 'Tommy Do',
'first_name' => 'Huy',
'last_name' => 'Đỗ',
...//and more info in UserTable
]
And access to each element.
$this->request->session()->read('Auth.nice_name');
Print:
Tommy Do
Hope it will help you :D

Existing database integration with Laravel 5.0

I am new to laravel. I've got the whole database designed in phpmyadmin. Now I want to integrate the existing database in Laravel. Is there any way to do that? If so then do I have to create models?
Yes, you can use your database in laravel but at first you have to provide your database credentials to allow the framework/Laravel to access your database. So, you can use a .env file or simply you can use the config/database.php to provide credentials, for example, to use your mySql database all you need to setup the database configuration as follows:
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'your_database_name'),
'username' => env('DB_USERNAME', 'your_database_username'),
'password' => env('DB_PASSWORD', 'your_database_password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
]
Then regarding your second question, yes, you have to create models for each table or you can use DB facade to run queries directly from your controllers (not recommended). For example, to access your posts table you can create a Post model in app folder which may look something like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
// If your table name is other than posts
// then use this property to tell Laravel
// the table name.
protected $table = 'posts';
}
Then you can use something like this:
$posts = \App\Post::all(); // Get all posts
$post = \App\Post::find(1); // Get the post with id 1
If you don't want to use Eloquent Model like one I've created above then you may use something like this:
$posts = \DB::table('posts')->get(); // Get allp posts
These are some basic examples to get you start. To learn more about the use cases of models/database you should visit Laravel website and read the manual (find Database section and check Query Builder and Eloquent).

CakePHP Test Controller add edit

I am testing controllers add method (CakePHP2.0).
But the issue is that what ever data I send as post gets added to my default databases and not my test database.
Heres the code.
public function testAdd() {
$data = array(
'Post' => array(
'title' => 'Fourth Post Title',
'body' => 'Fourth Post Body',
'created' => '2012-01-22 21:31:52'
)
);
$result = $this->testAction('/posts/add', array('data' => $data, 'method', 'post'));
debug($result);
}
What I was actually expecting that the posted data togo into test DB that i configured in database.php and not in default DB.
There are a few requirements for tests to occur in CakePHP 2.0 that you may want to double check:
Ensure that your Database Config setting in config/database.php is setup as
public $test = array(
//Sample Test Settings Here
);
If you running tests from the web interface, debug must be set to at least 1 or else the tests will not run.
You can also validate that PHPUnit and your test database connection is working properly by running CakePHP core tests by navigating to: http://localhost/your_app/test.php and "AllConfigure" test.
All information from the CakePHP Book: http://book.cakephp.org/2.0/en/development/testing.html

Resources