Professional CodeIgniter, Thomas Myer
Chapter 8: Last-Minute Upgrades
261
function sendemail(){
  if ($this- 
> 
input- 
> 
post(`subject')){
    $test = $this- 
> 
input- 
> 
post(`test');
    $subject = $this- 
> 
input- 
> 
post(`subject');
    $msg = $this- 
> 
input- 
> 
post(`message');
    if ($test){
      $this- 
> 
email- 
> 
clear();
      $this- 
> 
email- 
> 
from(`claudia@example.com', `ClaudiasKids.net');
      $this- 
> 
email- 
> 
to(`claudia@example.com');
      $this- 
> 
email- 
> 
subject($subject);
      $this- 
> 
email- 
> 
message($msg);
      $this- 
> 
email- 
> 
send();
      $this- 
> 
session- 
> 
set_flashdata(`message', "Test email sent");
    }else{
      $subs = $this- 
> 
MSubscribers- 
> 
getAllSubscribers();
      foreach ($subs as $key = 
> 
 $list){
        $unsub = " 
< 
p 
> 
< 
a href='". base_url()."welcome/unsubscribe/"
            .$list[`id']. "' 
> 
Unsubscribe 
< 
/a 
> 
< 
/p 
> 
";
        $this- 
> 
email- 
> 
clear();
        $this- 
> 
email- 
> 
from(`claudia@example.com', `ClaudiasKids.net');
        $this- 
> 
email- 
> 
to($list[`email']);
        $this- 
> 
email- 
> 
bcc(`claudia@example.com'); 
        $this- 
> 
email- 
> 
subject($subject);
        $this- 
> 
email- 
> 
message($msg . $unsub);
        $this- 
> 
email- 
> 
send();
      }
      $this- 
> 
session- 
> 
set_flashdata(`message', count($subs) . " emails sent");
    }
    redirect(`admin/subscribers/index','refresh'); 
  }else{
    $data[`title'] = "Send Email";
    $data[`main'] = `admin_subs_mail';
    $this- 
> 
load- 
> 
vars($data);
    $this- 
> 
load- 
> 
view(`dashboard');  
  }
} 
The E - mail class is very simple: it allows you to quickly and easily send e - mails without too much fuss.
Since you ' re autoloading the class, there ' s no need to invoke it specifically.
First you run
$this- 
> 
email- 
> 
clear()
to clear out any settings or variables. You set the " from "
address with  
$this- 
> 
email- 
> 
from()
 ,  the   " to "   address  with   
$this- 
> 
email- 
> 
to()
, and so on. Once
you ' ve set the subject and message parameters, you can send with  
$this- 
> 
email- 
> 
send()
 . 
The only thing you ' re doing differently is checking for a test message. If the user has indicated a test
message, then send to a single test address. Otherwise, loop through all the subscribers pulled from the
database table, adding an unsubscribe method to the end of the message.
Unsubscribing from within an E - mail
Notice that the last thing you ' re appending to the e - mail is a line of HTML:
$unsub = " 
< 
p 
> 
< 
a href='". base_url()."welcome/unsubscribe/"
            .$list[`id']. "' 
> 
Unsubscribe 
< 
/a 
> 
< 
/p 
> 
"; 
c08.indd   261
c08.indd   261
6/10/08   5:37:34 PM
6/10/08   5:37:34 PM