原因:
使用Codeigniter的时候,我们经常需要进行发送邮件进行提醒自己或者通知别人,这时候,我们就可以使用Codeigniter自带的类Email来完成工作。
介绍:
通过查看Email.php文件,我们看到Codeigniter可以使用三种protocol来发送邮件:
- var $protocol = "mail"; // 分别是mail/sendmail/smtp
那我们这里使用的是smtp。
使用:
首先要选择email,我这里选择的是google的gmail,gmail一般不会被认为是垃圾邮件,但是gmail的smtp是ssl方式的,所以主机必须要安装ssl才能使用。
- $config['protocol'] = 'smtp';
- $config['smtp_host'] = 'ssl://smtp.gmail.com';
- $config['smtp_user'] = 'youremail@gmail.com';
- $config['smtp_pass'] = 'yourpassword';
- $config['smtp_port'] = '465';
- $config['charset'] = 'utf-8';
- $config['mailtype'] = 'text';
- $config['smtp_timeout'] = '5';
- $config['newline'] = " ";
- $this->load->library ('email', $config);
- $this->email->from ('from@email.com', 'From email name');
- $this->email->to ('to@email.com', 'To email name');
- $this->email->bcc ('bcc@email.com');
- $this->email->subject ('Test subject');
- $this->email->message ('The content');
- $this->email->send ();
为了查看是否工作,使用如下指令进行Debug:
- echo $this->email->print_debugger();
- exit;