|
本帖最后由 Closer 于 2014-12-29 14:24 编辑
代码已经应用在实际项目中,邮箱用的是QQ的企业邮箱,实测mailgun也可以使用,其他邮箱没有测试过。
相关代码如下:
首先重写CI_Email(./application/libraries/MY_Email.php):
PHP复制代码
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* MY_Email.php Class
*
* Extends CodeIgniter Controller
*
*/
class MY_Email extends CI_Email {
var $send_from = Null;
var $send_user = Null;
public function __construct ($config = array())
{
parent ::__construct ($config);
$this->send_from = $this->smtp_user;
$this->send_user = 'noreply';//此项可从配置文件里读取
}
public function subject ($subject)
{
$subject = '=?'.$this->charset.'?B?'.base64_encode($subject).'?=';
$this->set_header('Subject', $subject);
return $this;
}
public function sendMail ($to,$subject,$message,$attach=NULL){
$this->from($this->smtp_user,$this->send_user);
$this->to($to);
$this->subject($subject);
$this->message($message);
if(!empty($attach))
{
$this->attach($attach);
}
return $this->send();
}
}
/* End of file MY_Email.php */
/* Location: ./application/libraries/MY_Email.php */ 复制代码
创建配置文件 ./application/config/email.php
PHP复制代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//---------------------------------------------------
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.exmail.qq.com';
$config['smtp_user'] = 'noreply@*****.com';
$config['smtp_pass'] = '******';
$config['mailtype'] = 'html';
$config['validate'] = TRUE;
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
/* End of file email.php */
/* Location: ./application/config/email.php */
复制代码
然后就可以在controller里调用了 /application/controllers/Cron.php
PHP复制代码
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//---------------------------------------------------
class Cron extends CI_Controller {
public function __construct ()
{
parent ::__construct ();
$this->load->library('email');
}
public function index ()
{
$to="****@qq.com";
$subject="Email test";
$message="Testing the email class";
$this->email->sendMail($to,$subject,$message);
}
}
/* End of file Cron.php */
/* Location: ./application/controllers/Cron.php */
复制代码
|
评分
-
查看全部评分
|