分享CI群发邮件类库,欢迎测试
本帖最后由 baiyuxiong 于 2010-4-30 18:58 编辑类库代码
<?phpif ( ! 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()
{
//需要其它邮件模板自己定义
}
}
控制器中使用:
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);
}
不需要发送结果显示的话,把类库里的输出注释掉。我自己测试没问题。 收藏~~收藏~~ smtp似乎需要企业邮箱支持啊 回复 3# snllll
不需要企业邮箱支持,你随便申请一个免费邮箱也能用 挺好玩的,申请个邮箱试一下 靠,不能用呀! 不能用 提示经过错误 :错误如下:
Message: mail() : Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
Filename: libraries/Email.php:L 没意思。 Severity: Notice
Message: fwrite(): send of 29 bytes failed with errno=10053 您的主机中的软件中止了一个已建立的连接。
Filename: libraries/Email.php
Line Number: 1816 楼主试过用QQ邮箱发送没?
页:
[1]