使用ci框架mail邮件类发送邮件
2017-02-07 本文已影响0人
zxwen_zl
通过ci框架mail邮件类发送邮件和利用利用第三方平台发送邮件
1、controller
public function mail()
{
$param = array();
$param['to'] = 'qing.chen';
$param['subject'] = '邮件测试-标题';
//$param['tmpl'] = 'email/demo';
$param['content'] = "111";
$this->MailModel->sendMail($param);
echo 'sss';
}
2、model
class MailModel extends CI_Model
{
/**
* 生成邮件内容
*
* @param string $templ
* @param array $data
* @return string
* @author http://blog.iwshop.com/
*/
public function makeBody($tmpl, $data)
{
$content = $this->load->view($tmpl, $data, true);
return $content;
}
/**
* 发送邮件
*
* @param array $param
* @return boolean
*/
public function sendMail($param)
{
// 发件人配置
if ( empty($param['frommail'])) $param['frommail'] = 'woims';
if ( empty($param['fromname'])) $param['fromname'] = '运营平台';
$param['frommail'] = $this->_domain($param['frommail']);
// CI mail 配置
$this->load->library('email');
$config = array();
$config['protocol'] = 'smtp'; // 邮件协议
$config['smtp_host'] = 'ssl://smtp.qq.com';
$config['smtp_user'] = '你的@qq.com';
$config['smtp_pass'] = '授权码';//去QQ邮箱设置开启smtp
$config['smtp_port'] = 465; //邮箱端口
$config['smtp_timeout'] = 30; //超时时间
$config['mailtype'] = 'text'; // 格式 (text/html)
$config['charset'] = 'utf-8'; // 编码
$config['wordwrap'] = TRUE; // 自动换行
$this->email->initialize($config);
$this->email->set_newline("\r\n");
$config['crlf'] = "\r\n";
//$param['cc'] = 'barly.li,qingbin.wu';
$this->email->from($param['frommail'], $param['fromname']);
if ( $param['to']) $this->email->to($this->_domain($param['to']));
//if ( $param['cc']) $this->email->cc($this->_domain($param['cc']));//抄送
//if ( $param['bcc']) $this->email->bcc($this->_domain($param['bcc']));//密送
//如果存在邮件模板使用模板作为邮件内容 2015-01-08
$param['content'] = $param['tmpl'] ? $this->makeBody($param['tmpl'], $param['data']) : $param['content'];
$this->email->subject($param['subject']);
$this->email->message($param['content']);
$this->email->send();
return true;
}