1: <?php
2: namespace Module\Mailer;
3:
4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 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: 54: 55: 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:
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:
83: $headers .= 'From: ' . $this->from . "\r\n";;
84:
85:
86: if (isset($this->cc)) {
87: $headers .= 'Cc: ' . implode(',', $this->cc) . "\r\n";
88: }
89:
90:
91: if (isset($this->bcc)) {
92: $headers .= 'Bcc: ' . implode(',', $this->bcc) . "\r\n";
93: }
94:
95:
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: 105: 106: 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: 125: 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: 144: 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: 163: 164: 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: 177: 178: 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: 193: 194: 195:
196: public function msgText($msg, $switch = false) {
197: $this->html = $switch;
198: $this->body = $msg;
199: }
200:
201: 202: 203: 204: 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: 220: 221: 222: 223: 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: }