利用CodeIgniter中的Email類發(fā)郵件
來源:程序員人生 發(fā)布時(shí)間:2014-09-03 08:23:17 閱讀次數(shù):5108次
CodeIgniter擁有功能強(qiáng)大的Email類。以下為利用其發(fā)送郵件的代碼。
關(guān)于CI的Email類的詳情請參考:http://codeigniter.org.cn/user_guide/libraries/email.html
文件路徑為/application/controllers/welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('email'); //加載CI的email類
//以下設(shè)置Email參數(shù)
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.163.com';
$config['smtp_user'] = 'fanteathy';
$config['smtp_pass'] = '******';
$config['smtp_port'] = '25';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->email->initialize($config);
//以下設(shè)置Email內(nèi)容
$this->email->from('fanteathy@163.com', 'fanteathy');
$this->email->to('517081935@qq.com');
$this->email->subject('Email Test');
$this->email->message('<font color=red>Testing the email class.</font>');
$this->email->attach('applicationcontrollers1.jpeg'); //相對于index.php的路徑
$this->email->send();
//echo $this->email->print_debugger(); //返回包含郵件內(nèi)容的字符串,包括EMAIL頭和EMAIL正文。用于調(diào)試。
}
}
在加載Email類之后需要配置Email參數(shù)。配置完成之后使用
$this->email->initialize($config)
函數(shù)來初始化參數(shù)。再設(shè)置郵件的內(nèi)容,最后調(diào)用
$this->email->send()發(fā)送郵件
。其中要注意如果添加附件時(shí),附件的地址是相對CI根目錄下的index.php的路徑。運(yùn)行結(jié)果如下:

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)