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....
Related
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();
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.
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);
I am a new learner of cakephp 2.x.
I want to build a simple form for user to upload their resume files, and then send them as email attachments.
Somehow I can't find a way to attach uploaded file to email. Any help will welcome!
Here is my send.ctp:
<?php
echo $this -> Form -> create(null, array('enctype' =>'multipart/form-data'));
echo $this -> Form -> input('first_name', array('type'=>'text'));
echo $this -> Form ->input('last_name', array('type'=>'text'));
echo $this -> Form ->input('contact_number',array('type'=>'text'));
echo $this -> Form ->input('email',array('type'=>'text'));
echo $this -> Form ->input('resume', array('type' => 'file',));
echo $this -> Form ->end('Submit');
?>
Here is my HomesController.php
<?php
class HomesController extends AppController {
public $name = 'Homes';
public $uses = null;
public $helpers = array('Html', 'Form');
public function index() {
}
public function send(){
if (!empty($this->data)) {
echo $this->data['last_name'];
echo $this->data['contact_number'];
echo $this->data['email'];
echo $this->data['resume'];
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->from('xyz#gmail.com');
$email->to('abc#gmail.com');
$email->subject('About');
$email->attachments = array($this->data['resume']);
$email->send($this->data['last_name']);
$this->redirect(array('action' => 'index'));
}
}
}?>
The email can actually send and be able to receive, but without attachment. When I try "echo $this->data['resume']" it only display a string---'Array'. But other fields like "echo $this->data['last_name']" works properly.
http://cakebaker.42dh.com/2006/04/15/file-upload-with-cakephp/
Above one use database, I don't wanna use database to store files. And it use cakephp 1.x which cannot run in 2.x.
How to do form-based file uploads in CakePHP?
This one doesn't say how to attach files to email.
This is my Config/email.php, I use gmail smtp:
class EmailConfig {
public $default = array(
'transport' => 'Smtp',
'from' => 'XXX#gmail.com',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
public $smtp = array(
'transport' => 'Smtp',
'from' => array('XXX#gmail.com' => 'Sender'),
'host' => 'smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'XXX',
'password' => '#password',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
public $fast = array(
'from' => 'you#localhost',
'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',
);
}
$this->data['resume'] is an array that contains information about the file upload.
The array looks something like this:
array(
'name' => '(filename)',
'type' => '(filetype)',
'tmp_name' => '(file location)',
'error' => (int) 0,
'size' => (int) 1
)
Try setting the attachment using:
$email->attachments(array($this->data['resume']['name'] => $this->data['resume']['tmp_name']));
$Path = WWW_ROOT."img";
$fileName = 'image.png';
$this->Email->to = $to;
$this->Email->subject = $subject;
$this->Email->attachments = array($Path.$fileName);
$this->Email->from = $from;
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',
);
}