|  | 
 
| 本帖最后由 baiyuxiong 于 2010-4-30 18:58 编辑 
 类库代码
 PHP复制代码 复制代码 
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class CI_Sendmail {
 
 
var $_config = Array(
 
'protocol' => 'smtp',
 
'smtp_host' => 'smtp.163.com',
 
'smtp_port' => 25,
 
'smtp_user' => 'xxxxxx@163.com',
 
'smtp_pass' => 'xxxxx',
 
'charset' => 'GBK',
 
'mailtype' => 'html',
 
);
 
var $CI;
 
/** 
 * Constructor
 
 *
 
 * @access
 public
 
 */
 
function
  CI_Sendmail()
 
{
 
$this->CI =&  get_instance();
 
$this->CI->load->library('email',$this-> _config);
 
}
 
 
function  SendM($maillist)
 
{
                foreach($maillist as $mailinfo)                {                       $this->CI->email->set_newline("rn");
                        $from_name = "xxx网站";//发件人名称
                        $email_subject ="欢迎加入XXX";
                        $email_msg="<br>".$this-> _Get_Reg_Template($mailinfo['username'],$mailinfo['active_url']);
                        //解决乱码问题
                        //$from_name = iconv('UTF-8','GBK',$from_name);
                        //$email_subject = iconv('UTF-8','GBK',$email_subject);
                        //$email_msg = iconv('UTF-8','GBK',$email_msg);
                        //封装发送信息
                        $this->CI->email->from('baiyuxiong@163.com',$from_name);
                        $this->CI->email->to($mailinfo['email']);
                        $this->CI->email->subject($email_subject); 
                       $this->CI->email->message($email_msg);
                        //$this->email->attach("attachments/2009/01/1.xls");//附件
                        //发送
                   if (!$this->CI->email->send()) 
                       { 
                                show_error($this->email->print_debugger());
                                //return false;
                        }
                        else
                        {
                                echo"OK";
                                //return true;
                        }
                }
 
}
 
function  _Get_Reg_Template($username,$active_url)
 
{
 
return $username.':<br />欢迎注册XXX,请点击以下链接激您的账号:<br /><a href = "'.$active_url.'">'.$active_url.'</a>';
 
}
 
function  _Get_Other_Template()
 
{
 
    //需要其它邮件模板自己定义
 
}
 
}
 控制器中使用:
 
 PHP复制代码 复制代码 
 
function index()
 
{
 
$this->load->library('sendmail');
 
//$this->load->view('welcome_message');
//把所有邮件组合到这样的二维数组里,需要其它信息,可以类比扩展。
$maillist = array
 
(
 
'a'=>array(
 
'username'=>"Jesse",
 
'email'=>"ok@baiyuxiong.com",
 
'active_url'=>"active_url"
 
)
 
);
 
$this->sendmail->SendM($maillist);
 
}
 不需要发送结果显示的话,把类库里的输出注释掉。我自己测试没问题。
 | 
 评分
查看全部评分
 |