模型内的Codeigniter电子邮件类

6kkfgxo0  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(128)

是否可以在模型中使用Codeigniter Email类?
我一直在尝试,但它拒绝加载邮件对象。一般来说,库是否允许加载到模型中?

代码

<?php

class ClubInvitation extends DataMapper {
        
        var $table = "club_invitations";
        var $has_one = array("club");
        
        public function __construct($id){
            parent::__construct($id);
            $this->load->library('email');
        }
        
        public function inviteEmailToClub($email, $club, $message){ 
            $invite = new ClubInvitation();
            $invite->email = $email;
            $invite->code = random_string('unique', null);
            //$invite->save($club);
            
            $club->ClubAdmin->limit(1);
            $mainClubAdmin = $club->ClubAdmin->get()->User->get();
            $data['club'] = $club;
            $data['message'] = $message;
            $data['club_leader'] = $mainClubAdmin;
            $data['invite_code'] = $invite->code;
                        
            $email_body = $this->WBLayout->emailTemplate('invited_to_club', $data);
            $this->email->from($mainClubAdmin->User->getAccount()->email, $club->name);
            $this->email->to($email);
            $this->email->subject($club->name.' has invited you to join');
            $this->email->message($email_body);
            $this->email->send();
        }

}
798qvoo8

798qvoo81#

通常情况下,库被加载到控制器中,并从控制器中访问,而不是从模型中。你可以只将模型中的数据数组返回到控制器中,并从控制器本身使用电子邮件库。如果这对你来说不可行,那么试着这样做:

public function __construct($id){
   parent::__construct($id);
}

public function inviteEmailToClub($email, $club, $message){
   $CI =& get_instance();
   $CI->load->library('email');
   .....
   $CI->email->from($mainClubAdmin->User->getAccount()->email, $club->name);
   ....
}

相关问题