Overview

Namespaces

  • Core
    • BaseClasses
    • Cookie
    • Crontab
    • DB
    • Input
    • Mailer
    • Middleware
      • Auth
      • CSRF
    • Route
    • Session
    • System
  • None

Classes

  • Bridge
  • Core\BaseClasses\BaseAuth
  • Core\BaseClasses\BaseController
  • Core\BaseClasses\BaseCSRF
  • Core\Cookie\Cookie
  • Core\Crontab\Crontab
  • Core\DB\DB
  • Core\Input\Input
  • Core\Mailer\Mailer
  • Core\Middleware\Auth\Auth
  • Core\Middleware\CSRF\CSRF
  • Core\Route\Route
  • Core\Session\Session
  • Core\System\System

Functions

  • back
  • ControllerAutoload
  • dbd
  • ErrorHandler
  • ExceptionHandler
  • json
  • loadConfig
  • MiddlewareAutoload
  • redirect
  • SPAutoload
  • view
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * Created by PhpStorm.
  4:  * @Author: Shakti Phartiyal
  5:  * Date: 01/27/17
  6:  * Time: 06:43 PM
  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:      * Mailer constructor.
 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:      * Sets the mailing Engine php/swiftmailer etc avoid using this method except for testing purposes
 49:      * @deprecated use the config file instead
 50:      * @param $engine
 51:      * @param null $host
 52:      * @param null $port
 53:      * @param null $user
 54:      * @param null $password
 55:      * @param null $encryption
 56:      * @return Mailer class object
 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:      * Initialize the mailing system
 71:      * @return Mailer class Object
 72:      */
 73:     public static function init()
 74:     {
 75:         $mailer = new Mailer();
 76:         return $mailer;
 77:     }
 78: 
 79:     /**
 80:      * Sets the mail recipients
 81:      * @param $recipients can be a single email address or an array
 82:      * @return Mailer class Object
 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:      * Sets the sender of the mail
102:      * @param $from can be email address or an associative array of length 1 containing the name as key and email as value
103:      * @return Mailer class Object
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:      * Sets the CC recipient of the email
120:      * @param $cc can be a single email address or an array of addresses
121:      * @return Mailer class Object
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:      * Sets the Bcc recipient of the email
141:      * @param $bcc
142:      * @return Mailer class Object
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:      * Sets the content type of mail message defaults to text/plain
162:      * @param $type
163:      * @return Mailer class Object
164:      */
165:     public function contentType($type)
166:     {
167:         $this->mailContentType = $type;
168:         return $this;
169:     }
170: 
171:     /**
172:      * Sets the email subject
173:      * @param $subject
174:      * @return Mailer class Object
175:      */
176:     public function subject($subject)
177:     {
178:         $this->mailSubject = $subject;
179:         return $this;
180:     }
181: 
182:     /**
183:      * Set the body of the email message a custom view can also be passed as a string
184:      * @param $body
185:      * @return Mailer class Object
186:      */
187:     public function body($body)
188:     {
189:         $this->mailBody = $body;
190:         return $this;
191:     }
192: 
193:     /**
194:      * Adds an attachment to the mail
195:      * @param $attachmentPath
196:      * @param $fileName
197:      * @return $this
198:      */
199:     public function attach($attachmentPath, $fileName)
200:     {
201:         $this->mailAttachments[$attachmentPath] = $fileName;
202:         return $this;
203:     }
204: 
205:     /**
206:      * Send the compiled mail
207:      * @param null $options for swiftmailer/php mailer
208:      * @return bool mail send response
209:      */
210:     public function send($options=null) //$options['ssl']['verify_peer'] = false;
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:      * PHP mailer
224:      * @param $options
225:      * @return bool
226:      * @throws Exception
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:      * Swiftmailer
267:      * @param $options
268:      * @return int
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:      * @internal
297:      * Test mailer
298:      * @return bool
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: }
API documentation generated by ApiGen