Select Page

So below is our basic E-mail service for Centeno. The e-mail code is code based on what I usually use in projects, so feedback is welcome. Notice the use of the Values class. That’s another article I should do next I guess. Think of it as a valid values class or an application.cfm (in some ways) for you ColdFusion users.

include_once("Mail.php");
include_once("Mail/mime.php");
class EmailService
{
	
  //base methods

  function send($p_recipient,
                     $p_htmlContent,
                     $p_textContent,
                     $p_subject,
                     $p_sender,
                     $p_customAuth=0)
  {
    $headers['To'] = $p_recipient;
    $headers['From'] = $p_sender;
    $headers['Subject'] = $p_subject;
    $headers['Content-Type'] = "text/html; CHARSET=UTF-8";
    $headers['Content-Transfer-Encoding'] = "7bit";

    if($p_customAuth)
    {
      $params = $p_customAuth;
    }
    else 
    {
      $params = EmailService::getParams();
    }
    $m = Mail::factory(Values::getEmailServerType(),$params);
	
    $mime = new Mail_mime();

    $mime->settxtbody($p_textContent);
    $mime->sethtmlbody($p_htmlContent);
	
    $body = $mime->get();
    $head = $mime->headers($headers);
	
    $m->send($recipient, $head, $body);
    }
	
    //extended methods
	
    //helper methods

    function getParams()
    {
        $params['host'] = Values::getEmailServer();
    	  $params['auth'] = Values::isAuthRequiredForEmailServer();
    	  $params['username'] = Values::getEmailServerUsername();
    	  $params['password'] = Values::getEmailServerPassword();
    	  return $params;
    }
}

You’ll notice I haven’t yet implemented a way to send attachments. That’s because I’ve never done it before, not having needed to, but I’ll be working on that in the next few days and will update it then.

I’m also using PEAR for now. I always do for e-mail now, since it works. If I ever became dissatisfied with it, I could simply change the send method and voila! no more PEAR in any e-mail I send. You see, we’re going to add a couple more methods but as you’ll see they’ll all go through the send method to actually send e-mail.

For clarity’s sake, I’ve divided the class (with some comments), into base methods, extended methods, and helper methods. Helper methods just, well, help out, but aren’t called directly. If you want to call one of them directly, you may want to consider breaking it into it’s own service for use. Think of base methods as the base of a funnel. The extended methods all use one or more of them to accomplish their task. On their own, base methods are usable but not very refined.

So what else would be good as far as some methods for this service. Well, how about a method to send to a list of people and a method to send using a custom template? Great!

include_once("Mail.php");
include_once("Mail/mime.php");
class EmailService
{
	
  //base methods

  function send($p_recipient,
                     $p_htmlContent,
                     $p_textContent,
                     $p_subject,
                     $p_sender,
                     $p_customAuth=0)
  {
    $headers['To'] = $p_recipient;
    $headers['From'] = $p_sender;
    $headers['Subject'] = $p_subject;
    $headers['Content-Type'] = "text/html; CHARSET=UTF-8";
    $headers['Content-Transfer-Encoding'] = "7bit";

    if($p_customAuth)
    {
      $params = $p_customAuth;
    }
    else 
    {
      $params = EmailService::getParams();
    }
    $m = Mail::factory(Values::getEmailServerType(),$params);
	
    $mime = new Mail_mime();

    $mime->settxtbody($p_textContent);
    $mime->sethtmlbody($p_htmlContent);
	
    $body = $mime->get();
    $head = $mime->headers($headers);
	
    $m->send($recipient, $head, $body);
  }

  //extended methods
	
  function sendBulk($p_recipients, 
                           $p_htmlContent,
                           $p_textContent,
                           $p_subject,
                           $p_sender,
                           $p_customAuth=0)
  {
    if(count($p_recipients)>0) {
      foreach($p_recipients as $recipient) {
        EmailService::send($recipient,
                                   $p_htmlContent,
                                   $p_textContent,
                                   $p_subject,
                                   $p_sender,
                                   $p_customAuth);
	}
    }
  }
	
  function sendTemplate($p_recipient,
                                 $p_htmlContent,
                                 $p_textContent,
                                 $p_subject,
                                 $p_sender,
                                 $p_customAuth=0)
  {
    $html = Values::getHtmlEmailPre().$p_htmlContent.Values::getHtmlEmailPost();
    $text = Values::getTextEmailPre().$p_textContent.Values::getTextEmailPost();
    EmailService::send($p_recipient,
                               $html,
                               $text,
                               $p_subject,
                               $p_sender,
                               $p_customAuth);
  }
	
  //helper methods

  function getParams()
  {
    $params['host'] = Values::getEmailServer();
    $params['auth'] = Values::isAuthRequiredForEmailServer();
    $params['username'] = Values::getEmailServerUsername();
    $params['password'] = Values::getEmailServerPassword();
    return $params;
  }
}

So in the expanded class, we now have two extended methods, sendBulk and sendTemplate both drawing on send. As you can see, if something were wrong with the base e-mail functionality, I can just fix the service rather than several parts of the application.

By simply building out extended methods, services can become very useful for any project. Remember that if you’re thinking about how this service will be used for your project, don’t. Write the service for the service’s sake. In this case, make an e-mail service that can do anything and everything you would want e-mailing functionality to do. Leave the business/project-specific logic to the Domain tier.