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',
);
}
Related
I configured second database in laravel with name Test. but when i used to insert data in second database's table. It shows me an error, database not configured please help me to find where is my mistake?
I already make configuration to add new database in project. i changed .env file and database.php file in config folder. but nothing can help me.
database.php
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => 'Test',
'username' => 'root',
'password' => '',
'unix_socket' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
.env
DB_CONNECTION_SECOND=mysql2
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=Test
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=
SellerSelectProduct.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class SellerSelectProduct extends Model
{
protected $connection = 'Test';
protected $fillable = ['product_id'];
public function product(){
return $this->belongsTo(Product::class);
}
}
SellerBaseController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
use App\SellerSelectProduct;
class SellerBaseController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function someMethod()
{
$someModel = new SellerSelectProduct;
$someModel->setConnection('Test');
$something = $someModel->find(1);
return $something;
}
}
SellerProductController.php
<?php
namespace App\Http\Controllers;
use App\City;
use App\State;
use Illuminate\Http\Request;
use App\SellerSelectProduct;
class SellerProductController extends SellerBaseController
{
public function someMethod()
{
$someModel = new SellerSelectProduct;
$someModel->setConnection('Test');
$something = $someModel->find(1);
return $something;
}
public function index(State $state)
{
return $state->Cities;
}
public function addproducts(Request $request){
SellerSelectProduct::insert($request->data);
return response()->json([
'message' => 'Selected product list updated successfully'
]);
}
}
a sample of a second conection.
.env
DB_CONNECTION=mysql2
DB_HOST2=127.0.0.1
DB_PORT2=3306
DB_DATABASE2=
DB_USERNAME2=
DB_PASSWORD2=
database.php
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST2', ''),
'port' => env('DB_PORT2', '3306'),
'database' => env('DB_DATABASE2', 'forge'),
'username' => env('DB_USERNAME2', 'forge'),
'password' => env('DB_PASSWORD2', ''),
'unix_socket' => env('DB_SOCKET2', ''),
'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'),
]) : [],
],
To have an effect on clean or cache changes with or optimize handmade php command.
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'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....
I have 2 controllers, ContentController for general user and ManageController for administrator. I need to change the connection from default to admin and I have this code in my database.php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => '',
'database' => 'ComputerScience',
'prefix' => '',
'encoding' => 'utf8',
);
public $admin = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'admin',
'password' => '',
'database' => 'ComputerScience',
'prefix' => '',
'encoding' => 'utf8',
);
}
Thank you
So, inside your Model, you would use the useDbConfig Attribute:
class Example extends AppModel {
public $useDbConfig = 'admin';
}
Inside your Controller, simply use:
$this->ModelName->useDbConfig = 'admin';
Thats all.
I would use Model::setDataSource() rather than just setting the database config var. This is because there are other possible changes that come with changing the datasource:
$this->Model->setDataSource('admin');
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;