Overview

Namespaces

  • Sleepy
  • Module
    • Authentication
    • CSV
    • DB
    • FormBuilder
    • FSDB
    • IP2Country
    • Mailer
    • MobiDetect
    • Navigation
    • StaticCache
  • PHP

Classes

  • Sleepy\Debug
  • Sleepy\Hook
  • Sleepy\Router
  • Sleepy\SM
  • Sleepy\Template

Exceptions

  • Sleepy\RouteNotFound
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: namespace Module\Mailer;
  3: 
  4: /**
  5:  * Simplifies sending emails.
  6:  *
  7:  * Simplifies sending emails by automatically verifying email addresses and
  8:  * fetching HTML.
  9:  *
 10:  * ### Usage
 11:  *
 12:  * <code>
 13:  *   $m = new \Module\Mailer\Message();
 14:  *   $m->addTo("test@test.com");
 15:  *   $m->addFrom("from.me@test.com");
 16:  *   $m->addSubject("This is a test, don't panic.");
 17:  *   $m->fetchHTML("http://test.com/template.php");
 18:  *   $m->send();
 19:  * </code>
 20:  *
 21:  * ### Changelog
 22:  *
 23:  * ## Version 1.8
 24:  * * Added namespacing
 25:  *
 26:  * ## Version 1.6
 27:  * * Fixed bug with BCC and CC
 28:  *
 29:  * @date    August 13, 2014
 30:  * @author  Jaime Rodriguez, hi.i.am.jaime@gmail.com
 31:  * @version 1.8
 32:  * @license  http://opensource.org/licenses/MIT
 33:  */
 34: class Message {
 35:     private $to;
 36:     private $cc;
 37:     private $bcc;
 38:     private $from;
 39:     private $subject;
 40:     private $body;
 41:     private $html;
 42: 
 43:     public function __construct() {
 44:         $this->to;
 45:         $this->cc;
 46:         $this->bcc;
 47: 
 48:         $this->addSubject();
 49:         $this->html = false;
 50:     }
 51: 
 52:     /**
 53:      * Sends the email.
 54:      *
 55:      * @return bool Was the email successfully sent?
 56:      */
 57:     public function send() {
 58:         $headers = "";
 59: 
 60:         if (count($this->from) < 1) {
 61:             throw new \Exception('You forgot to addFrom();');
 62:         }
 63: 
 64:         if (count($this->to) < 1) {
 65:             throw new \Exception("You forgot to addTo();");
 66:         }
 67: 
 68:         if (strlen($this->body) < 1) {
 69:             throw new \Exception("You forgot to add content.");
 70:         }
 71: 
 72:         if (!isset($this->subject)) {
 73:             $this->addSubject();
 74:         }
 75: 
 76:         // To send HTML mail, the Content-type header must be set
 77:         if ($this->html) {
 78:             $headers  .= 'MIME-Version: 1.0' . "\r\n";
 79:             $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 80:         }
 81: 
 82:         // Add From
 83:         $headers .= 'From: ' . $this->from . "\r\n";;
 84: 
 85:         // Add CC's if there are any
 86:         if (isset($this->cc)) {
 87:             $headers .= 'Cc: ' . implode(',', $this->cc) . "\r\n";
 88:         }
 89: 
 90:         // Add BCC's if there are any
 91:         if (isset($this->bcc)) {
 92:             $headers .= 'Bcc: ' . implode(',', $this->bcc) . "\r\n";
 93:         }
 94: 
 95:         // Mail it
 96:         if (!mail(implode(",", $this->to), $this->subject, $this->body, $headers)) {
 97:             throw new \Exception("Mail was not sent.");
 98:         } else {
 99:             return true;
100:         }
101:     }
102: 
103:     /**
104:      * Adds a primary recipient.
105:      *
106:      * @param string $email A valid email address.
107:      */
108:     public function addTo($email) {
109:         $emails = explode(',', $email);
110: 
111:         foreach ($emails as $e) {
112:             $e = trim($e);
113:             if ($this->rfcCheck($e)) {
114:                 $this->to[] = $e;
115:             } else {
116:                 throw new \Exception("The \$email parameter has a non RFC 2822 compliant addresses: {$e}");
117:             }
118:         }
119: 
120:         return true;
121:     }
122: 
123:     /**
124:      * Adds a Carbon Copy recipient.
125:      * @param string $email A valid email address.
126:      */
127:     public function addCc($email) {
128:         $emails = explode(',', $email);
129: 
130:         foreach ($emails as $e) {
131:             $e = trim($e);
132:             if ($this->rfcCheck($e)) {
133:                 $this->cc[] = $e;
134:             } else {
135:                 throw new \Exception("The \$email parameter has a non RFC 2822 compliant addresses: {$e}");
136:             }
137:         }
138: 
139:         return true;
140:     }
141: 
142:     /**
143:      * Adds a Blind Carbon Copy recipient.
144:      * @param string $email A valid email address
145:      */
146:     public function addBcc($email) {
147:         $emails = explode(',', $email);
148: 
149:         foreach ($emails as $e) {
150:             $e = trim($e);
151:             if ($this->rfcCheck($e)) {
152:                 $this->bcc[] = $e;
153:             } else {
154:                 throw new \Exception("The \$email parameter has a non RFC 2822 compliant addresses: {$e}");
155:             }
156:         }
157: 
158:         return true;
159:     }
160: 
161:     /**
162:      * Sets a senders email address.
163:      * @param string $email
164:      *   A valid email address.
165:      */
166:     public function addFrom($email) {
167:         if ($this->rfcCheck($email)) {
168:             $this->from = $email;
169:             return true;
170:         } else {
171:             throw new \Exception("The \$email parameter has no RFC 2822 compliant addresses.");
172:         }
173:     }
174: 
175:     /**
176:      * Gets HTML for a $url and repalces anything in the body of the email with
177:      * the HTML that it fetched.
178:      * @param string $url the url of an html file for the email body.
179:      */
180:     public function fetchHtml($url) {
181:         $tempHtml = file_get_contents($url);
182: 
183:         if (isset($tempHtml)) {
184:             $this->html = true;
185:             $this->body = $tempHtml;
186:         } else {
187:             throw new \Exception("Failed to capture data from " . $url);
188:         }
189:     }
190: 
191:     /**
192:      * Sets the body of the email to text.
193:      * @param string $msg a string for the body
194:      * @param boolean $switch an optional parameter for overloading $this->html
195:      */
196:     public function msgText($msg, $switch = false) {
197:         $this->html = $switch;
198:         $this->body = $msg;
199:     }
200: 
201:     /**
202:      * Adds a subject to the email. If there is no subject the time will be used.
203:      * @param string $subject
204:      *   The subject of the email
205:      */
206:     public function addSubject($subject='') {
207:         if (strlen($subject) > 78) {
208:             throw new \Exception('The subject cannot be longer than 78 characters.');
209:         }
210: 
211:         if ($subject == '') {
212:             $this->subject = time();
213:         } else {
214:             $this->subject = $subject;
215:         }
216:     }
217: 
218:     /**
219:      * Extracts the email address from a RFC 2822 compliant string
220:      * @param  string $string
221:      *   RFC 2822 compliant string
222:      * @return string[]
223:      *   an array of email addresses
224:      */
225:     private function rfcCheck($string) {
226:         if (filter_var($string, FILTER_VALIDATE_EMAIL)) {
227:             return true;
228:         } else {
229:             return false;
230:         }
231:     }
232: }
sleepyMUSTACHE v.0.8 API documentation generated by ApiGen