如何在cakephp 2.x中向用户发送电子邮件

时间:2015-06-10 22:12:00

标签: cakephp-2.0

如果专家能帮助我如何向用户发送电子邮件,我将非常感激。 我正在建立一个登记系统。用户成功申请注册后,管理员必须批准,点击批准按钮,会向用户发送一封电子邮件,用户详细信息将保存在已批准的表格中。 以下是applicationsController中的批准操作。

    public function approve($student_id = null) {

   if ($this->request->is('post')) 
      $application = $this->Application->findById($student_id);
       $approved['Approved'] = $application['Application'];
       $approved['Approved']['student_id'] = $approved['Approved']['student_id'];

       $status = array('Application.status' => 'approved');
       unset($application['Application']['id']);
       unset($application['Application']['receipts']);
       $this->loadModel('Approved');
       $this->Approved->create();
       if ($this->Approved->save($approved)) {
          if ($this->Approved->saveField('status', 'approved')){
          $this->Session->setFlash(__('The student has been approved'));


          $email=$this->request->data['Application']['email'];
          $this->Email->to = $email;
          $this->Email->subject = 'Registration request approval';
          $this->Email->from = 'ernestmwesha@gmail.com';
          $this->Email->template = 'template';


          $this->Email->smtpOptions = array(
           'port' => '465',
           'timeout' => '30',
           'host' => 'ssl://smtp.gmail.com',
           'username' => 'ernestmwesha@gmail.com',
           'password' => 'mweshaernest',
            );

          $this->Email->delivery = 'smtp';
          if($this->Email->send()){

            return true;
          }
          else{

            echo $this->Email->smtpError;
          }

       $this->Application->delete($student_id);
      $this->redirect(array('action' => 'index')); }
    } else {
       $this->Session->setFlash(__('The student could not be approved.'));
   }

   $this->set('title_for_layout', 'Approved Requests');
}

点击已批准的按钮后,我收到以下错误:

注意(8):未定义索引:应用程序[APP \ Controller \ ApplicationsController.php,第120行]

您需要为to,cc或bcc指定至少一个目的地。 错误:发生内部错误。

.....博士学生获得批准并被放入批准的表格中

2 个答案:

答案 0 :(得分:0)

// in your controller 

App::uses('CakeEmail', 'Network/Email');


function somrthing () {
    $Email = new CakeEmail();
    $Email->from(array('me@example.com' => 'My Site'));
    $Email->to('you@example.com');
    $Email->subject('About');
    $Email->send('My message');
}

看看CakeEmail in CakePHP 2.x

答案 1 :(得分:0)

查看u2460470's回答,为您指明使用CakePHP生成电子邮件的正确方向。

确保您有邮件服务器设置来处理电子邮件的处理。您可能已经在本地设置了一个设置,例如SquirrelMail,或者您可能更喜欢使用托管的托管提供商(例如Gmail)。您可以在CakeEmail文档中找到configuring CakePHP to send mail through Gmail的示例。

我使用Postmark处理交易电子邮件的经验很丰富。有一个很好的插件maurymmarques/postmark-plugin,您可以使用它为CakePHP应用程序轻松设置Postmark。