如何使用控制器在新事件上发送邮件-Cakephp
我正在windows上执行Cron作业,以便使用 图书馆 cd/d c:\wamp64\www\project\bin 蛋糕用户邮件 编辑: 用户控制器如何使用控制器在新事件上发送邮件-Cakephp,php,shell,email,cakephp,cron,Php,Shell,Email,Cakephp,Cron,我正在windows上执行Cron作业,以便使用 图书馆 cd/d c:\wamp64\www\project\bin 蛋糕用户邮件 编辑: 用户控制器 private function sendResetEmail($url, $user) private function sendResetEmail($url, $user) { $email = new Email(); $email->template('resetpw'); $email-&g
private function sendResetEmail($url, $user)
private function sendResetEmail($url, $user) {
$email = new Email();
$email->template('resetpw');
$email->emailFormat('both');
$email->from('no-reply@naidim.org');
$email->to($user->email, $user->full_name);
$email->subject('Reset your password');
$email->viewVars(['url' => $url, 'username' => $user->username]);
if ($email->send()) {
$this->Flash->success(__('Check your email for your reset password link'));
} else {
$this->Flash->error(__('Error sending email: ') . $email->smtpError);
}
}
public function reset($passkey = null) {
if ($passkey) {
$query = $this->Users->find('all', ['conditions' => ['passkey' => $passkey, 'timeout >' => time()]]);
$user = $query->first();
if ($user) {
if (!empty($this->request->data)) {
// Clear passkey and timeout
$this->request->data['passkey'] = null;
$this->request->data['timeout'] = null;
//$user = $this->Users->patchEntity($user, $this->request->data);
$user->password = $this->request->data['password'];
if ($this->Users->save($user)) {
$this->Flash->success('Password has been Updated.');
return $this->redirect(array('action' => 'login'));
} else {
$this->Flash->success('Password Could not be updated');
}
}
} else {
$this->Flash->error('Invalid or expired passkey. Please check your email or try again');
$this->redirect(['action' => 'password']);
}
unset($user->password);
$this->set(compact('user'));
} else {
$this->redirect('/');
}
}
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
use App\Event\UserListener; // Load Event Class
use Cake\Event\EventManager;
class PagesController extends AppController
{
public function index()
{
$UserListener = new UserListener();
EventManager::instance()->on($UserListener);
$event = new Event('Model.Users.afterRemove', $this, ['user' => 1]);
$this->getEventManager()->dispatch($event);
exit;
}
}
在EventsController添加函数中调用它
我想为每个连接的用户检索与连接的电子邮件
例如:
$this->set('user_session', $this->request->session()->read('Auth.User'));
然后,如果该用户创建了新事件,请向该用户发送电子邮件。
然后用午餐吃克朗的工作
换句话说,我需要从EventController中的add函数发送邮件
private function sendResetEmail($url, $user)
private function sendResetEmail($url, $user) {
$email = new Email();
$email->template('resetpw');
$email->emailFormat('both');
$email->from('no-reply@naidim.org');
$email->to($user->email, $user->full_name);
$email->subject('Reset your password');
$email->viewVars(['url' => $url, 'username' => $user->username]);
if ($email->send()) {
$this->Flash->success(__('Check your email for your reset password link'));
} else {
$this->Flash->error(__('Error sending email: ') . $email->smtpError);
}
}
public function reset($passkey = null) {
if ($passkey) {
$query = $this->Users->find('all', ['conditions' => ['passkey' => $passkey, 'timeout >' => time()]]);
$user = $query->first();
if ($user) {
if (!empty($this->request->data)) {
// Clear passkey and timeout
$this->request->data['passkey'] = null;
$this->request->data['timeout'] = null;
//$user = $this->Users->patchEntity($user, $this->request->data);
$user->password = $this->request->data['password'];
if ($this->Users->save($user)) {
$this->Flash->success('Password has been Updated.');
return $this->redirect(array('action' => 'login'));
} else {
$this->Flash->success('Password Could not be updated');
}
}
} else {
$this->Flash->error('Invalid or expired passkey. Please check your email or try again');
$this->redirect(['action' => 'password']);
}
unset($user->password);
$this->set(compact('user'));
} else {
$this->redirect('/');
}
}
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
use App\Event\UserListener; // Load Event Class
use Cake\Event\EventManager;
class PagesController extends AppController
{
public function index()
{
$UserListener = new UserListener();
EventManager::instance()->on($UserListener);
$event = new Event('Model.Users.afterRemove', $this, ['user' => 1]);
$this->getEventManager()->dispatch($event);
exit;
}
}
但是首先我必须从我的索引函数调用它
通过改变这个
EventsController.php
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Console\ShellDispatcher;
use Cake\Routing\Router;
use Cake\Event\Event;
class EventsController extends AppController
{
public $components = array("Email");
public function index()
{
//$to = $user_session['email'];
$to = 'exemple@gmail.com';
$subject = 'Hi buddy, i got a message for you.';
$message = 'Nothing much. Just test out my Email Component using PHPMailer.';
try {
$mail = $this->Email->send_mail($to, $subject, $message);
print_r($mail);
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
exit;
}
public function run($username = null){
$shell = new ShellDispatcher();
$output = $shell->run(['cake', 'users', $username]);
debug($output);
if ($output === 0){
echo "Shell Command execute";
}else{
echo "Fail Command execute";
}
exit;
}
public function add()
{
$event = $this->Events->newEntity();
if ($this->request->is('post')) {
$event = $this->Events->patchEntity($event, $this->request->data);
if ($this->Events->save($event)) {
//function send_mail to user who crated Event
$this->Flash->success(__('The event has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The event could not be saved. Please, try again.'));
}
}
$this->set('eventTypes', $this->Events->EventTypes->find('list'));
$this->set(compact('event'));
$this->set('_serialize', ['event']);
$this->set('user_session', $this->request->session()->read('Auth.User'));
$this->viewBuilder()->setLayout('user');
}
}
那么我想问的是如何在add函数中发送邮件
编辑:如何在我的视图中调用它
index.ctp
<li role="presentation" class="dropdown">
<a href="javascript:;" class="dropdown-toggle info-number"
data-toggle="dropdown" aria-expanded="false">
<i class="fa fa-envelope-o"></i>
<span class="badge bg-green">6</span>
</a>
<ul id="menu1" class="dropdown-menu list-unstyled msg_list" role="menu">
<li>
<a>r
<span class="image"><?php echo $this->Html->image($user_session['photo']); ?></span>
<span>
<span>Zak</span>
<span class="time">3 mins ago</span>
</span>
<span class="message">
Film festivals used to be do-or-die moments for movie makers. They were where...
</span>
</a>
</li>
首先在src\Event\UserListener.php
路径中创建UserListener.php
文件。像这样
<?php
namespace App\Event;
use Cake\Event\EventListenerInterface;
use Cake\Log\Log;
class UserListener implements EventListenerInterface
{
public function implementedEvents()
{
return [
'Model.Users.afterRemove' => 'afterRemove',
];
}
public function afterRemove($event, $user)
{
Log::write('debug', $user. ' has deleted his/her account.');
}
}
logs\debug.log