1: <?php
2: 3: 4: 5: 6: 7:
8: namespace Core\Mailer;
9:
10: use Exception;
11: use Swift_Attachment;
12: use Swift_Mailer;
13: use Swift_Message;
14: use Swift_SmtpTransport;
15:
16: class Mailer
17: {
18: private $mailEngine = null;
19: private $mailTo = array();
20: private $mailCc = array();
21: private $mailBcc = array();
22: private $mailFrom = null;
23: private $mailSubject = null;
24: private $mailBody = null;
25: private $mailContentType = "text/plain";
26: private $mailHeaders = array();
27: private $mailHost = null;
28: private $mailPort = null;
29: private $mailUser = null;
30: private $mailPassword = null;
31: private $mailEncryption = null;
32: private $mailAttachments = array();
33:
34: 35: 36:
37: private function __construct()
38: {
39: $this->mailEngine = $_ENV['mail']['engine'];
40: $this->mailHost = $_ENV['mail']['host'];
41: $this->mailPort = $_ENV['mail']['port'];
42: $this->mailUser = $_ENV['mail']['user'];
43: $this->mailPassword = $_ENV['mail']['password'];
44: $this->mailEncryption = $_ENV['mail']['encryption'];
45: }
46:
47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57:
58: public function setEngine($engine, $host=null, $port=null, $user=null, $password=null, $encryption=null)
59: {
60: $this->mailEngine = $engine;
61: $this->mailHost = $host;
62: $this->mailPort = $port;
63: $this->mailUser = $user;
64: $this->mailPassword = $password;
65: $this->mailEncryption = $encryption;
66: return $this;
67: }
68:
69: 70: 71: 72:
73: public static function init()
74: {
75: $mailer = new Mailer();
76: return $mailer;
77: }
78:
79: 80: 81: 82: 83:
84: public function to($recipients)
85: {
86: if(is_array($recipients))
87: {
88: foreach ($recipients as $recipient)
89: {
90: $this->mailTo[] = $recipient;
91: }
92: }
93: else
94: {
95: $this->mailTo[] = $recipients;
96: }
97: return $this;
98: }
99:
100: 101: 102: 103: 104:
105: public function from($from)
106: {
107: if(is_array($from))
108: {
109: $this->mailFrom = $from;
110: }
111: else
112: {
113: $this->mailFrom = [$from => $from];
114: }
115: return $this;
116: }
117:
118: 119: 120: 121: 122:
123: public function cc($cc)
124: {
125: if(is_array($cc))
126: {
127: foreach ($cc as $copy)
128: {
129: $this->mailCc[] = $copy;
130: }
131: }
132: else
133: {
134: $this->mailCc[] = $cc;
135: }
136: return $this;
137: }
138:
139: 140: 141: 142: 143:
144: public function bcc($bcc)
145: {
146: if(is_array($bcc))
147: {
148: foreach ($bcc as $bcopy)
149: {
150: $this->mailBcc[] = $bcopy;
151: }
152: }
153: else
154: {
155: $this->mailBcc[] = $bcc;
156: }
157: return $this;
158: }
159:
160: 161: 162: 163: 164:
165: public function contentType($type)
166: {
167: $this->mailContentType = $type;
168: return $this;
169: }
170:
171: 172: 173: 174: 175:
176: public function subject($subject)
177: {
178: $this->mailSubject = $subject;
179: return $this;
180: }
181:
182: 183: 184: 185: 186:
187: public function body($body)
188: {
189: $this->mailBody = $body;
190: return $this;
191: }
192:
193: 194: 195: 196: 197: 198:
199: public function attach($attachmentPath, $fileName)
200: {
201: $this->mailAttachments[$attachmentPath] = $fileName;
202: return $this;
203: }
204:
205: 206: 207: 208: 209:
210: public function send($options=null)
211: {
212: if(strtolower($this->mailEngine) == "php")
213: {
214: return $this->corePHPMailer($options);
215: }
216: else if(strtolower($this->mailEngine) == "swiftmailer")
217: {
218: return $this->swiftMailer($options);
219: }
220: }
221:
222: 223: 224: 225: 226: 227:
228: private function corePHPMailer($options)
229: {
230: $options = null;
231: $this->mailHeaders[] = "MIME-Version: 1.0";
232: $this->mailHeaders[] = "Content-Type: ".$this->mailContentType;
233: $this->mailHeaders[] = "Cc: ".implode(",",$this->mailCc);
234: $this->mailHeaders[] = "Bcc: ".implode(",",$this->mailBcc);
235: foreach ($this->mailFrom as $name=>$mail)
236: {
237: $this->mailHeaders[] = "From: ".$name." <".$mail.">";
238: }
239: if(count($this->mailAttachments)>0)
240: {
241: $this->mailBody .= "\r\n";
242: foreach($this->mailAttachments as $path => $name)
243: {
244: $fileData = null;
245: try
246: {
247: $fileHandle = fopen($path, 'r');
248: $fileData = stream_get_contents($fileHandle);
249: fclose($fileHandle);
250: }
251: catch(Exception $e)
252: {
253: Throw New Exception("Unable to open File for Reading",1);
254: }
255: $this->mailBody .= "Content-Type: application/octet-stream; name=\"" . $name . "\"\r\n";
256: $this->mailBody .= "Content-Transfer-Encoding: base64\r\n";
257: $this->mailBody .= "Content-Disposition: attachment\r\n";
258: $this->mailBody .= chunk_split(base64_encode($fileData)) ."\r\n";
259: }
260: }
261: $response = mail(implode(",",$this->mailTo), $this->mailSubject , wordwrap($this->mailBody,70), implode("\r\n", $this->mailHeaders),$options);
262: return $response;
263: }
264:
265: 266: 267: 268: 269:
270: private function swiftMailer($options)
271: {
272: $transport = Swift_SmtpTransport::newInstance($this->mailHost, $this->mailPort, $this->mailEncryption)
273: ->setUsername($this->mailUser)
274: ->setPassword($this->mailPassword)
275: ->setStreamOptions($options);
276: $mailer = Swift_Mailer::newInstance($transport);
277: $message = Swift_Message::newInstance($this->mailSubject)
278: ->setFrom($this->mailFrom)
279: ->setTo($this->mailTo)
280: ->setCc($this->mailCc)
281: ->setBcc($this->mailBcc)
282: ->setContentType($this->mailContentType)
283: ->setBody($this->mailBody);
284: if(count($this->mailAttachments)>0)
285: {
286: foreach($this->mailAttachments as $path => $name)
287: {
288: $message->attach(Swift_Attachment::fromPath($path)->setFilename($name));
289: }
290: }
291: $result = $mailer->send($message);
292: return $result;
293: }
294:
295: 296: 297: 298: 299:
300: protected function test()
301: {
302: $options['ssl']['verify_peer'] = false;
303: return Mailer::init()
304: ->setEngine("swiftmailer","mail.example.com",587,"jane.doe@email.com","janeSecurePassword","tls")
305: ->to("john@email.com")
306: ->from("jane.doe@email.com")
307: ->contentType("text/html")
308: ->subject("Sample Test Mail")
309: ->body("<h1>This is the mail body</h1>")
310: ->attach("/home/jane/somefile.zip", "theFile.zip")
311: ->send($options);
312: }
313:
314: }