CakePHP 3 Unknown email configuration "gmail" in cPanel server - cakephp

Email are sent successfully in local. But the problem occurs in production server that shows an error: unknown gmail configuration.
My email configuration looks like:
'gmail' => [
'className' => 'Smtp',
// The following keys are used in SMTP transports
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 60,
'username' => 'noreplayxxxxx#gmail.com',
'password' => 'xxxxxxxxxxxxxxxxxxxxx',
'client' => null,
'tls' => true,
]
What is the problem and how can I fix this problem?

Make sure app.php file on your production server contains required configuration. app.php is ignored by git by default, so it might not get deployed in your case as you are expecting.

You can use these in your controller
///////////////////// Configuration /////////////////////////
$host = 'ssl://smtp.gmail.com';
$username = 'abc#gmail.com';
$password = 'xxxxxxx';
$port = '465';
$email_to = 'xyz#gmail.com';
$senderName = 'abc';
$email_id_reply ='abc#gmail.com';
$new_attachments = '<some image>';
$msgsend ='Hello!!';
Email::configTransport('WebMail'.$recipient,
[
'className' => 'Smtp',
'host' => $host,
'port' => $port,
'timeout' => 30,
'username' => $username,
'password' => $password,
'client' => null,
'tls' => null
]
);
////////// SEND MAIL
$transport = ['transport' => 'WebMail'.$recipient];
$email = new Email($transport);
$email ->template('default','default')
->emailFormat('both')
->from([$username => $senderName])
->to($email_to)
->replyTo($email_id_reply)
->subject('Client Message');
$email->attachments($new_attachments);
$response = $email->send($msgsend);

Related

Working with Events and Listeners in multiple databases in Laravel

I have main database with some tables and for every client we have different databases.While using events and listeners where do I specify the connection string to use.For model and controller I have used Config:set to change my connection string dynamically for one request.I added the same in the listeners handle method,but that isn't working and the events shows as failed.
Thanks in advance.
The Listener method.
public function handle(ApplicationAssociated $event)
{
echo "Hi";
$client_id=$event->client_id;
echo 'hiii'.$client_id;
$dbconnections=DBConnection::where('client_id',$client_id)->get();
if($client_id>0)
{
foreach($dbconnections as $dbconnection)
{
echo "in if the db name is".$dbconnection->mysql_database;
echo "break";
if($dbconnection->mysql_password==NULL)
{
$password="";
}
else
{
$password=$dbconnection->password;
}
\Config::set([
'database.connections.mysql1.host' => $dbconnection->mysql_host,
'database.connections.mysql1.database' => $dbconnection->mysql_database,
'database.connections.mysql1.username' => $dbconnection->mysql_username,
'database.connections.mysql1.password' => $password,
]);
}
}
echo \Config::get('database.connections.mysql1.database');
Application::where('id',21)
->update(['name' =>'Testing eventing']);
echo "LAST";
}
try this out.
config/database.php
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE2', 'forge'),
'username' => env('DB_USERNAME2', 'forge'),
'password' => env('DB_PASSWORD2', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
.env file
DB_DATABASE2=ichuztowork
DB_USERNAME2=root
DB_PASSWORD2=
Anywhere in your application you can set the connection string in this way
$user = (new \App\User(['name'=>'My Name', 'email'=>'myEmail#email.com']))-
>setConnection('foreignDB');
$user->save();
// or if you don't need to use the model, then just
(new \App\User(['name'=>'My Name', 'email'=>'myEmail#email.com']))-
>setConnection('foreignDB')->save();

change default db in cakephp3

In a controller I want to change the default database so i can access the new db (db2) from anywhere in the website. The db2 database has the same models but just different data. My code just accesses the other database but doesnt set the new default database to db2 which can be accessed anywhere in the website. I didnt get the answer from the below posts.
This is my controller :
$connection = ConnectionManager::get('db2'); // 'db2' where my second database is configured
$results = $connection->execute('SELECT * FROM tutors')->fetchAll('assoc');
//this works but doesnt set the default database to db2 everywhere
This is my app.php :
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
//'port' => 'non_standard_port_number',
'username' => 'root',
'password' => '',
'database' => 'aptutori_test',
'encoding' => 'utf8',
'timezone' => '+11:00',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
],
'db2' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
//'port' => 'non_standard_port_number',
'username' => 'root',
'password' => '',
'database' => 'aptutori_testbak',
'encoding' => 'utf8',
'timezone' => '+11:00',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
],
Dynamically change database connection in cakephp 3
http://mark-story.com/posts/view/using-cakephp-and-a-horizontally-sharded-database
Use ConnectionManager::alias():
http://api.cakephp.org/3.0/class-Cake.Datasource.ConnectionManager.html#_alias
Fore example this will make all tables that require the default connection to use db2:
ConnectionManager::alias('db2', 'default');
You could do this application wide in a Middleware in > cake 3.3, opposed to using a DispatcherFilter, like described in http://mark-story.com/posts/view/using-cakephp-and-a-horizontally-sharded-database.
<?php
namespace App\Middleware;
use Cake\Datasource\ConnectionManager;
class TenantShardMiddleware
{
public function __invoke($request, $response, $next)
{
$tenant = $request->getHeader('MY-tenant');
ConnectionManager::alias($tenant[0], 'default');
$response = $next($request, $response);
return $response;
}
}
In my example above I'm using a special Request Header to switch databases.

Google App Engine Cloud SQL connection issue

When trying to connect to Google Cloud SQL I get following error:
Connection error
Unable to find the socket transport "unix" - did you forget to enable it when you configured PHP?
I have been trying to solve this issue now for multiple hours, still nothing.
My config is as follows (in ZF2 config local.php):
return array(
'db' => array(
'driver' => 'Mysqli',
'host' => '',
'database' => 'ar_captab',
'username' => 'root',
'password' => '',
'driver_options' => [
'unix_socket' => ':/cloudsql/some-app-v1-test:some-db'
]
),
);
I also added my application ID of my Google App Engine application to Access Control list (Authorized App Engine Applications) in Cloud SQL.
When I try to run this:
$config = $this->getServiceLocator()->get('config');
$adapter = new \Zend\Db\Adapter\Adapter($config['db']);
$sql = new \Zend\Db\Sql\Sql($adapter);
$select = $sql->select();
$select->from('example');
$select->where('1');
$statement = $sql->prepareStatementForSqlObject($select);
$results = $statement->execute();
I get the unix socket error. What I am missing?
Finally figured it out. I was because of the colon. You do not need it with Mysqli connection, only with PDO connection types.
So the correct config is:
return array(
'db' => array(
'driver' => 'Mysqli',
'host' => '',
'database' => 'some-database',
'username' => 'root',
'password' => '',
'driver_options' => [
'unix_socket' => '/cloudsql/some-app-v1-test:some-db'
]
),
);

CakeDC user didn't send verification email

I'm able to get this plugin works after hours.
But my problem now is that it didn't send the verification email.
this is my email.php config.
I don't know how to set this up.
So I just follow what others are doing.
class EmailConfig {
public $default = array(
'transport' => 'Smtp',
'from' => 'you#email.com',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
public $smtp = array(
'transport' => 'Smtp',
'from' => array('site#test.com' => 'My Site'),
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
public $fast = array(
'from' => 'you#email.com',
'sender' => null,
'to' => null,
'cc' => null,
'bcc' => null,
'replyTo' => null,
'readReceipt' => null,
'returnPath' => null,
'messageId' => true,
'subject' => null,
'message' => null,
'headers' => null,
'viewRender' => null,
'template' => false,
'layout' => false,
'viewVars' => null,
'attachments' => null,
'emailFormat' => null,
'transport' => 'Smtp',
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'log' => true,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
}
Can anybody tell me how to make this thing right meaning that it send verification email?
Looks like you don't have an email server configured in your windows environment.
If you want to debug emails being sent you could use the Debug Transport this way
public $default = array(
'transport' => 'Debug',
'from' => 'you#email.com',
'log' => 'email',
);
Then check email output written to file app/tmp/logs/email.log
It looks like your email.php config file is badly misconfigured.
Most likely CakeEmail is using $default, which you have setup as follows:
public $default = array(
'transport' => 'Smtp',
'from' => 'you#email.com',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
Basically you are setting transport to SMTP and you are missing all the necessary configuration to have it working.
So, you should set your transport to Mail as this:
public $default = array(
'transport' => 'Mail',
'from' => 'you#yourdomain.com',
);
CakeDC most likely is using default as the following:
$Email = new CakeEmail('default');
Then it should work....

Cakephp email error

My controller
App::uses('CakeEmail', 'Network/Email'); //before class begins
//function
public function contact(){
$email = new CakeEmail();
$email->config('smtp');
$email->from('me#gmail.com');
$email->to('you#gmail.com');
$email->subject('About');
$email->send('My message');
}
//Email.php in config folder
class EmailConfig {
public $smtp = array(
'transport' => 'Smtp',
'from' => 'me#gmail.com',
'host' => 'smtp.gmail.com',
'port' => 465,
//'timeout' => 30,
'username' => 'me#gmail.com',
'password' => '*****',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
}
The error i get is
Fatal error: Maximum execution time of 30 seconds exceeded in
C:\wamp\www\myproject\lib\Cake\Network\CakeSocket.php on line 222
what do i need to change?
I even created the view file in Views/Users/contact.
Do i need to change the view file in View/Email folder?
You need to increase max_execution_time variable in your php.ini file.
You shouldn't be timing out sending an email through gmail though. Have you configured the smtp options correctly?
from the cake book
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
'You can configure SSL SMTP servers, like GMail. To do so, put the 'ssl://' at prefix in the host and configure the port value accordingly. Example:'
<?php
class EmailConfig {
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'my#gmail.com',
'password' => 'secret',
'transport' => 'Smtp'
);
}
?>
Remove $email->from('me#gmail.com'); from your action and try again. Specify From address only in the email config. Then see whether it works.
App::uses('CakeEmail', 'Network/Email'); //before class begins
//function
public function contact(){
$email = new CakeEmail();
$email->config('smtp');
$email->to('you#gmail.com');
$email->subject('About');
$email->send('My message');
}
//Email.php in config folder
class EmailConfig {
public $smtp = array(
'transport' => 'Smtp',
'from' => 'me#gmail.com',
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 60,
'username' => 'me#gmail.com',
'password' => '*****',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
}

Resources