[eluser]NogDog[/eluser]
Figured I'd follow up here with what I actually implemented (and it worked!) in case it helps anyone who stumbles upon this thread in a web or forum search. I won't bother with the views, as I think they're pretty trivial.
application/config/email.php:
Code:
<?php
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'outgoing.example.net';
$config['smtp_user'] = 'username';
$config['smtp_pass'] = 'password';
application/controllers/email.php:
Code:
<?php
/**
* Email form
*/
class Email extends Controller
{
/**
* constructor
* @return void
*/
public function Email()
{
parent::Controller();
$this->load->helper(array('form', 'url', 'html', 'my_html'));
}
/**
* Display the form
* @return bool;
*/
public function index()
{
$this->load->view('email_form');
return true;
}
/**
* Process the form request
* @return bool
*/
public function send()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name',
'trim|required|max_length[30]|callback_nobreaks');
$this->form_validation->set_rules('email', 'Email',
'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject',
'trim|required|max_length[70]|callback_nobreaks');
$this->form_validation->set_rules('message', 'Message Text',
'trim|required|max_length[2000]');
if($this->form_validation->run() == false)
{
$this->load->view('email_form');
}
else
{
if($this->sendMail() == true)
{
$this->load->view('email_result');
}
else
{
$this->load->view('email_result',
array('error' => "An unknown error occurred while trying to send your email."));
}
}
return true;
}
/**
* Send the email
* @return bool
*/
private function sendMail()
{
$this->load->library('email');
$this->config->load('email', true);
$this->email->initialize($this->config->config['email']);
return $this->email->facade(
'[email protected]',
'[email protected]',
$_POST['name'].' <'.$_POST['email'].'>',
$_POST['subject'],
$_POST['name'].' ('.$_POST['email'].") writes:\r\n\r\n".$_POST['message']
);
}
/**
* Ensure no newlines or carriage returns in text
* @return bool
* @param string $value
*/
public function nobreaks($value)
{
if(preg_match('/[\r\n]/', $value))
{
$this->form_validation->set_message('nobreaks',
'The %s field cannot contain line breaks');
return false;
}
return true;
}
}
application/libraries/MY_Email.php:
Code:
/**
* Extend the built-in email class to provide a single point of entry
*/
class MY_Email extends CI_Email
{
/**
* constructor
* Loads custom config file and inits it
* @return void
*/
public function MY_Email()
{
parent::CI_Email();
}
/**
* Provides one point to set the message values and send it
* @return bool
* @param string $to To email
* @param string $from From email
* @param string $reply Reply-To email
* @param string $subj Subject
* @param string $msg Message text
*/
public function facade($to, $from, $reply, $subj, $msg)
{
$this->to($to);
$this->from($from);
$this->reply_to($reply);
$this->subject($subj);
$this->message($msg);
return($this->send());
}
}