Вы можете скачать симпатичный маленький пакет искр от "Shawn McCool", который использует "Swift-Mailer".
Посетите его сайт для инструкций по установке
Вот моя конфигурация для отправки электронной почты с использованием СУХОГО подхода (требуется PHP 5.3.0!).
Config
| --Mailer [точка] PHP
$config['swift_email_server'] = 'ssl://smtp.googlemail.com';
$config['swift_email_port'] = 465;
$config['swift_email_username'] = '######';
$config['swift_email_password'] = '######';
Libarary
| --MY_Email
class MY_Email extends CI_Email {
public function __construct(){
parent::__construct();
}
/**
* =========================================================================
* Parse's html email templates - initalised via swift-mailer
*
* @param mixed $data
* @param string $template
* @return html
*
* =========================================================================
*/
public function send_mail($data, $template){
//Anonymous functions are available since PHP 5.3.0
$callback = function ($matches) use ($data){
return ( isset($data[$matches[1]]) )
? $data[$matches[1]]
: $matches[0];
};
//Finds any matches in brackets [] and replaces them with data passed via callback
return preg_replace_callback(
'/\[(.*?)\]/',
$callback,
read_file(EMAIL_TEMPLATES . $template));
}
}
Email templates location defined in index.php
define('EMAIL_TEMPLATES', 'emailtemplates');
Контроллер
| --MY_Controller
class MY_Controller extends MX_Controller{
protected $user;
protected $swiftMailer = NULL;
public function __construct(){
parent::__construct();
$this->user = ($this->session->userdata('uid'))
? Member::find($this->session->userdata('uid')) : array();
$this->setup_profile();
$this->form_validation->set_error_delimiters('<p class="form-error">', '</p>');
}
public function setup_profile(){
$sections = array(
'config' => FALSE,
'http_headers' => FALSE,
'queries' => FALSE,
'memory_usage' => FALSE
);
$this->output->set_profiler_sections($sections);
if(ENVIRONMENT === 'development'){
$this->output->enable_profiler(TRUE);
}
}
protected function prep_email($data, $template){
$message =
Swift_Message::newInstance('Type your heading here...')
->setFrom($data['email_address'])
->setTo(array($this->config->item('primary_email'), $data['email_address']))
->setBody( $this->email->send_mail($data, $template))
->setContentType('text/html');
return ( $this->swiftMailer->send($message) );
}
}
Модуль
| --services // простой тестовый модуль
публичная функция упорядочить () {
$ config = array (
массив ('field' => 'service', 'label' => 'Service', 'rules' => 'trim | required | max_lenght [128]'),
массив ('field' => 'num', 'label' => 'Numer of Sessions', 'rules' => 'trim | required | numeric'),
массив ('field' => 'request_date', 'label' => 'Request_date', 'rules' => 'trim | required'),
массив ('field' => 'note', 'label' => 'Note', 'rules' => 'trim | max_lenght [500]')
);
$this->form_validation->CI =& $this;
$this->form_validation->set_rules($config);
if($this->form_validation->run($this)){
$service = Service::find($this->input->post('service'));
if((int)$this->input->post('num') > 1){
$potential = ( $this->input->post('num') * $service->price );
}else{
$potential = $this->input->post('num');
}
$insert = array(
'service_id' => $this->input->post('service'),
'num' => $this->input->post('num'),
'request_date' => $this->input->post('request_date'),
'note' => serialize( $this->input->post('note') ),
'potential_income' => $potential,
'member_id' => $this->user->id,
'status' => 'pending'
);
$emaildata = array(
'service' => $service->slug,
'num' => $insert['num'],
'request_date' => $insert['request_date'],
'note' => $insert['note'],
'potential_income' => $potential,
'email_address' => $this->user->email_address,
'status' => 'pending',
'fullname' => $this->user->firstname . ' ' . $this->user->surname,
'message_sent' => date('M d h:iA'),
'site_link' => ''
);
if(Meeting::create($insert)){
if(parent::prep_email($emaildata, 'request_meeting[dot]html')){
$this->session->set_flashdata('success' ,'Request has been sent! You will recieve an email shortly from Paul');
//redirect('/');
}else{
$this->session->set_flashdata('error', 'Email service seems to be down at the moment');
//redirect('/');
}
}else{
$this->session->set_flashdata('error', 'Unable to request meeting at this time');
//redirect('/');
}
}else{
$this->meetings_form();
}
Надеюсь, это поможет!