Codeigniter模型中的Email类

时间:2013-11-07 04:56:53

标签: php codeigniter email codeigniter-classloader

是否可以在模型中使用Codeigniter Email类?

我一直在尝试,但它拒绝加载邮件对象。通常,是否允许将库加载到模型中?

代码

类ClubInvitation扩展了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();
    }

}

1 个答案:

答案 0 :(得分:2)

通常,库被加载到控制器中并从那里访问,而不是从模型中访问。您可以将模型中的数据数组返回到控制器,并使用控制器本身的电子邮件库。如果这对您不可行,那么请尝试:

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);
   ....
}