升级 Email
文档
变更内容
只有一些小改动,例如方法名以及类的加载方式发生了变化。
使用 SMTP 协议时,其行为也有一些细微变化。如果沿用 CI3 的设置,可能无法与 SMTP 服务器正常通信。请参见 SMTP 协议的 SSL 与 TLS 和 邮件首选项。
升级指南
在类中,将
$this->load->library('email');改为$email = service('email');。之后,需要将所有以
$this->email开头的代码替换为$email。Email 类中的方法名略有不同。除
send()、attach()、printDebugger()和clear()外,所有方法都以set为前缀,后接原来的方法名。例如,bcc()现在是setBcc(),其他方法同理。app/Config/Email.php 中的配置属性已经变更。请参见 设置邮件首选项,查看新的属性列表。
代码示例
CodeIgniter 3.x 版本
<?php
$this->load->library('email');
$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
CodeIgniter 4.x 版本
<?php
$email = service('email');
$email->setFrom('your@example.com', 'Your Name');
$email->setTo('someone@example.com');
$email->setCC('another@another-example.com');
$email->setBCC('them@their-example.com');
$email->setSubject('Email Test');
$email->setMessage('Testing the email class.');
$email->send();