如何用ci框架library自带的email library发送邮件

CI框架邮件发送简单演示
ci框架绝对是php初学中想要的东西,它能极大的缩短你的代码量!
下面看看我的发送邮件的简单演示:
function email()
$this-&load-&library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = '';
$config['smtp_user'] = '';//这里写上你的163邮箱账户
$config['smtp_pass'] = '';//这里写上你的163邮箱密码
$config['mailtype'] = 'html';
$config['validate'] =
$config['priority'] = 1;
$config['crlf'] = &\r\n&;
$config['smtp_port'] = 25;
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$this-&email-&initialize($config);
$this-&email-&from('', '合肥学院青年志愿者联合会');//发件人
$this-&email-&to('');
$this-&email-&message('哈哈,测试邮件发送');
$this-&email-&send();
echo $this-&email-&print_debugger();
如果您不想使用使用上述方法设定参数,您可以把它们放入一个配置文件。创建一个新文件称为email.php ,添加$config数组在该文件中。然后将该文件保存为config/email.php 它将自动的被使用。如果您保存了一个参数配置文件,就不需要使用$this-&email-&initialize()函数来初始化参数了
标签(Tag):
------分隔线----------------------------
------分隔线----------------------------CI框架Email类发送HTML邮件方法
最近写会员中心的时候,需要做一个给注册用户邮箱验证的功能。因为用的是CI框架,查了下手册,就一些简单的配置选项说明,但是并没有详细的实例,于是决定贴一份实例出来。
1.现实在CI框架公共函数里加了两个自定义公共函数:
&//发送html邮件模板处理函数&function emailmoban($body){&&if (is_array($body))&&{&&&$file = array_keys($body);&&&$file = $file[0];&&&$data = array_values($body);&&&$data = $data[0];&&&/* 检查邮件模板文件是否存在 */&&&if (!file_exists($file))&&&{&&&&echo '模板不存在!';&&&}&&&$body = file_get_contents($file);&&&if(is_array($data)){&&&&foreach ($data as $key=&$val)&&&&{&&&&&$body& = str_replace('{'.$key.'}',$val,$body);&&&&}&&&&return $&&&}&&&}&}
&/**& *生成一个随机码& *$length& 随机码的长度& */&function random($length)&{&& $strStartName=&&;&& $strChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';&& $max = strlen($strChars) - 1;&& mt_srand((double)microtime() * 1000000);&& for($i = 0; $i & $ $i++)&& {&&& $strStartName .= $strChars[mt_rand(0, $max)];&& }&& return $strStartN&}&
2.然后就是在控制器中配置Email类了,和对HTML邮件模板进行处理
&& //自定义邮件模板内容中的变量值&&$toemail=$_POST['newemail'];
&&$emaildata['username']=$_SESSION['txz'];&&$emaildata['time']=date('Y-m-d H:i:s',time());
&&$suijima=random(6);&&& //生成认证随机码&&$this-&Model_proveemail-&iproveemailcode($suijima,$_SESSION['txzid']);& //将随机码入库&&$emaildata['suijima']=$&&//$_SESSION['suijima']=$
&&$proveurl=$this-&config-&item('base_url').'?c=proveemail&m=prove'.'&code='.urlencode($suijima).'&id='.$_SESSION['txzid'].'&newemail='.$& //生成激活地址&&$emaildata['weburl']=$
&&$emailbody=emailmoban(array('template/email_test.html'=&$emaildata));& //处理邮件html内容&&&&&//配置邮件类&&$config['protocol']=&smtp&;&&$config['smtp_host']=&smtp.&;& //这里用的是腾讯企业邮箱服务器&&$config['smtp_user']=&email&;&&&&&&&&&&&&&&&&&&&&&&& //发送用的邮箱&&$config['smtp_pass']=&emailpassword&;&&&&&&&&//发送用的邮箱密码&&$config['crlf']=&\r\n&;&&$config['newline']=&\r\n&;&&$config['mailtype']=&html&;&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //发送邮件格式
&&//发送邮件&&$this-&load-&library('email'); &//加载邮件类&&$this-&email-&initialize($config);&&$this-&email-&from(
, '世纪鹤图');& //邮件发送邮箱和邮件主题&&$this-&email-&to($toemail);&&&&& &&//$this-&email-&cc(
);& //抄送&&//$this-&email-&bcc(
);& //暗送&&$this-&email-&subject('世纪鹤图会员中心-邮箱认证');&& //邮件主题&&$this-&email-&message(stripcslashes($emailbody));& //邮件内容&&if($this-&email-&send()==1){&&&$this-&Model_showmsg-&success(&邮件发送成功,请到邮箱验证&,3,&?c=userzx&);&&}else{&&&$this-&Model_showmsg-&error(&邮件发送失败,请重新发送&,3,&&);&&}&&& // debug($this-&email-&print_debugger());& //邮件内容编码&
无论我们走到哪里,有 5 样东西不能丢弃:1.追求,没有目标,你就失去了精神的信仰;2.尊严,它能支撑你的脊梁,让你高傲的或者;3.自信,千钧压顶何须叹,披荆斩棘向前看;4.坚韧,成败皆在毫厘之间,只要你选择的,再苦再难都要挺住;5.知识,千金易散尽,善学是财富,唯有知识能够创造一切。
日志数量:267
评论数量:72
引用数量:0
今日访问:1515
总访问量:5280908CI框架简单邮件发送类实例
作者:yanhui_wei
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了CI框架简单邮件发送类,结合完整实例形式分析了邮件发送类的功能定义与具体使用方法,需要的朋友可以参考下
本文实例讲述了CI框架简单邮件发送类。分享给大家供大家参考,具体如下:
ci框架绝对是php初学中想要的东西,它能极大的缩短你的代码量!
下面看看我的发送邮件的简单演示:
function email()
$this-&load-&library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = '';
$config['smtp_user'] = '';//这里写上你的163邮箱账户
$config['smtp_pass'] = 'jb51;';//这里写上你的163邮箱密码
$config['mailtype'] = 'html';
$config['validate'] =
$config['priority'] = 1;
$config['crlf'] = "\r\n";
$config['smtp_port'] = 25;
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$this-&email-&initialize($config);
$this-&email-&from('', '脚本之家');//发件人
$this-&email-&to('');
$this-&email-&message('哈哈,测试邮件发送');
$this-&email-&send();
echo $this-&email-&print_debugger();
如果您不想使用使用上述方法设定参数,您可以把它们放入一个配置文件。创建一个新文件称为email.php,添加$config数组在该文件中。然后将该文件保存为config/email.php 它将自动的被使用。如果您保存了一个参数配置文件,就不需要使用$this-&email-&initialize()函数来初始化参数了
更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》及《》
希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具Email 类 & CodeIgniter 3.0.0 中文手册|用户手册|用户指南|中文文档有助请顶,不好请评。0:33 CI框架使用PHPmail插件发送QQ邮件:发送成功,不过修改了主机参数,还包含了一个phpmail中的一个另外的文件,详见下方:参见:/forums/thread-.html博文摘写:
不知道大家在使用CI的email类的时候,是否有遇到麻烦,特别是使用smtp方式的时候,我遇到的是只能使用126邮箱,QQ和gmail都发送不成功,很无懒,最后在我们另外一个站上直接使用了phpmailer,但是直接使用phpmailer的话,有时候不是很方便,特别你的很多功能都是基于CI完成的时候,要相互依赖就不方便了,所以在想,那是否可以将phpmailer集成到CI中呢,像使用email类这样使用他,功夫不负有心人,在网上居然有人分享了很多内容,但是之前的CI是支持插件功能的,所以很多文章都是说的基于插件的方式,现在CI有了新的调整,基于类的方式。最后找到一篇文章,可以帮助我们解决这个问题,将phpmailer集成到CI中,成为类,大家可以去到这个url查看详细的介绍:http://blog.qoding.us/2011/09/codeigniter-using-phpmailer-to-send-email-via-gmail/
我将他的部分内容拷贝如下:
最近要處理一個電子報系統,再用 CI 那跛腳 Email Class 大概會被客訴到瘋掉。所以還是認命改用老牌的 PHPMailer Library。稍微試一下,發現在 CI 裡使用 PHPMailer 相當無痛,先到官網下載一份 PHPMailer (本文完成時的最新版本是 5.2.0),解壓縮後把整個資料夾丟到 CI\application\libraries\PHPMailer_5.2.0。接著在 libraries 下建立新檔案,就叫 mailer.php 好了。
PHP复制代码&?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mailer {
public function __construct()
require_once('PHPMailer_5.2.0/class.phpmailer.php');
// the true param means it will throw exceptions on errors, which we need to catch
$this-&mail = new PHPMailer(true);
$this-&mail-&IsSMTP(); // telling the class to use SMTP
$this-&mail-&CharSet = "utf-8";
// 一定要設定 CharSet 才能正確處理中文
$this-&mail-&SMTPDebug
// enables SMTP debug information
$this-&mail-&SMTPAuth
// enable SMTP authentication
$this-&mail-&SMTPSecure = "ssl";
// sets the prefix to the servier
$this-&mail-&Host
// sets GMAIL as the SMTP server
$this-&mail-&Port
// set the SMTP port for the GMAIL server
$this-&mail-&Username
= "YOUR_";// GMAIL username
$this-&mail-&Password
= "YOUR_PASSWORD";
// GMAIL password
$this-&mail-&AddReplyTo('YOUR_', 'YOUR_NAME');
$this-&mail-&SetFrom('YOUR_', 'YOUR_NAME');
public function sendmail($to, $to_name, $subject, $body){
$this-&mail-&AddAddress($to, $to_name);
$this-&mail-&Subject = $
$this-&mail-&Body
$this-&mail-&Send();
echo "Message Sent OK&/p&\n";
} catch (phpmailerException $e) {
echo $e-&errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e-&getMessage(); //Boring error messages from anything else!
}} /* End of file mailer.php */ 复制代码
接著在 Controller 裡呼叫這支 library 就可以了,範例如下。PHP复制代码&?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Epaper extends CI_Controller {
public function __construct(){
parent::__construct();
public function send(){
$mail_body = "落落長的內文";
$this-&load-&library('mailer');
$this-&mailer-&sendmail(
'這是測試信 '.date('Y-m-d H:i:s'),
$mail_body
}}/* End of file epaper.php */复制代码
至於多重收件人之類的設定就要另外再變化了,這邊只是最入門的版本。
注意:我用的时候需要额外包含一个文件,另外,我用的QQ邮箱,所以需要修改主机参数:
require_once('PHPMailer/class.smtp.php');
// $this-&mail-&Host
// sets GMAIL as the SMTP server
$this-&mail-&Host
// sets GMAIL as the SMTP server
修改后的一个函数:
public function __construct()
// require_once('PHPMailer_5.2.0/class.phpmailer.php');
// require_once('../../../PHPMailer/class.phpmailer.php');
require_once('PHPMailer/class.phpmailer.php');
require_once('PHPMailer/class.smtp.php');
// the true param means it will throw exceptions on errors, which we need to catch
$this-&mail = new PHPMailer(true);
$this-&mail-&IsSMTP(); // telling the class to use SMTP
$this-&mail-&CharSet = "utf-8";
// 一定要設定 CharSet 才能正確處理中文
$this-&mail-&SMTPDebug
// enables SMTP debug information
$this-&mail-&SMTPAuth
// enable SMTP authentication
$this-&mail-&SMTPSecure = "ssl";
// sets the prefix to the servier
// $this-&mail-&Host
// sets GMAIL as the SMTP server
$this-&mail-&Host
// sets GMAIL as the SMTP server
$this-&mail-&Port
// set the SMTP port for the GMAIL server
// $this-&mail-&Username
= "YOUR_";// GMAIL username
// $this-&mail-&Password
= "YOUR_PASSWORD";
// GMAIL password
// $this-&mail-&AddReplyTo('YOUR_', 'YOUR_NAME');
// $this-&mail-&SetFrom('YOUR_', 'YOUR_NAME');
$this-&mail-&Username
= "";// GMAIL username
$this-&mail-&Password
= "qnrxylzlojcobcej";
// GMAIL password
$this-&mail-&AddReplyTo('', '测试的邮件回复人名称');
$this-&mail-&SetFrom('', '测试的发件人名称');
参见上面这个博客成功发送。
另一个博客:在codeigniter的helper用phpmailer 发送邮件 参见:http://blog.csdn.net/jiaochangyun/article/details/7711656
阅读(...) 评论()

我要回帖

更多关于 php ci library 的文章

 

随机推荐