testing
[namibia] / module / Utility / src / Utility / Comms / Email.php
1 <?php
2 namespace Utility\Comms;
3
4
5 /**
6  * Zend email wrapper class
7  * @author Tjaart Viljoen
8  * @copyright 2012 Nirph Online
9  */
10 class Email
11 {
12         /**
13          * Zend Mail object
14          * @var Zend_Mail
15          */
16         protected $_service;
17
18         /**
19          * Body tag to be compatible with ZF2 mail message
20          * @var array
21          */
22         protected $_body = array();
23
24
25         /**
26          * Summoning.
27          * @return \Utility\Comms\Email
28          */
29         public function __construct()
30         {
31                 $this->_service = new \Zend\Mail\Message();
32         }
33
34         /**
35          * Set message context.
36          * @param array $aContext
37          * @return \Utility\Comms\Email
38          */
39         public function setContext(array $aContext = array())
40         {
41                 foreach ($aContext as $sParam => $mValue)
42                 {
43                         switch ($sParam)
44                         {
45                                 case 'From':
46                                         if (!is_null($mValue)
47                                                 && '' != $mValue)
48                                         {
49                                                 $this->_service->setFrom($mValue);
50                                         }
51                                         break;
52                                 case 'To':
53                                 if (IS_DEV_ENV || IS_STAGE_ENV)
54                                 {
55                                         #-> Environmental override.
56                                         \Utility\Debug::errorLog(
57                                                         'Email.send Override: ' . $mValue,
58                                                         isset($aContext['Subject'])
59                                                                 ? $aContext['Subject']
60                                                                 : 'No subject'
61                                 . ' to ' . 'nirph@webmail.co.za'
62                                                 );
63                                         $this->_service->addTo('testnirph@bonsaihut.co.za');
64 //                        $this->_service->addBcc('gerard@nirph.com');
65                                 }
66                                         else
67                                         {
68                                                 $this->_service->addTo($mValue);
69                                         }
70                                         break;
71                                 case 'Cc':
72                                 if (!IS_DEV_ENV && !IS_STAGE_ENV)
73                                 {
74                                                 $this->_service->addCc($mValue);
75                                         }
76                                         break;
77                                 case 'Bcc':
78                                 if (!IS_DEV_ENV && !IS_STAGE_ENV)
79                                 {
80                                                 $this->_service->addBcc($mValue);
81                                         }
82                                         break;
83                                 case 'Subject':
84                                         $this->_service->setSubject($mValue);
85                                         break;
86                                 case 'Body':
87                                         $tmpBody = new \Zend\Mime\Part($mValue);
88                                         $tmpBody->type = 'text/plain';
89                                         $this->_body[] = $tmpBody;
90                                         //$this->_service->setBodyText($mValue);
91                                         break;
92                                 case 'Html':
93                                         $tmpBody = new \Zend\Mime\Part($mValue);
94                                         $tmpBody->type = 'text/html';
95                                         $this->_body[] = $tmpBody;
96                                         //$this->_service->setBodyHtml($mValue);
97                                         break;
98                                 case 'ComplexAttachment':
99                                         foreach ($mValue as $cid => $meta)
100                                         {
101                                                 if(isset($meta['data']) && !empty($meta['data']))
102                                                 {
103                                                         $file = new \Zend\Mime\Part($meta['data']);
104                                                         $file->disposition = \Zend\Mime\Mime::DISPOSITION_INLINE;
105                                                         $file->encoding    = \Zend\Mime\Mime::ENCODING_BASE64;
106                                                         $file->description = 'Attached file';
107                                                         isset($meta['type'])
108                                                                 && $file->type = $meta['type'];
109                                                         isset($meta['filename'])
110                                                                 && $file->filename = $meta['filename'];
111                                                         $file->id = $cid;
112                                                         $this->_body[] = $file;
113                                                         //$this->_service->addAttachment($file);
114                                                 }
115                                         }
116                                         break;
117                                 case 'Attachment':
118                                         foreach ($mValue as $fileName => $fileData)
119                                         {
120                                                 if(!empty($fileData))
121                                                 {
122                                                         $file = new \Zend\Mime\Part($fileData);
123                                                         $file->disposition = \Zend\Mime\Mime::DISPOSITION_INLINE;
124                                                         $file->encoding    = \Zend\Mime\Mime::ENCODING_BASE64;
125                                                         $file->description = 'Attached file';
126                                                         $file->filename    = $fileName;
127
128                                                         $this->_body[] = $file;
129                                                         //$this->_service->addAttachment($file);
130                                                 }
131                                         }
132                                         break;
133                         }
134                 }
135                 return $this;
136         }
137
138         /**
139          * Send message.
140          * @param array $aContext
141          * @return \Utility\Comms\Email
142          */
143         public function send(array $aContext = array())
144         {
145                 empty($aContext)
146                         || $this->setContext($aContext);
147                 $transport = new \Zend\Mail\Transport\Sendmail();
148
149                 /*
150                  * Alteration to include message body as a \Zend\Mime\Message() as per ZF2 specification
151                  */
152                 $body = new \Zend\Mime\Message();
153                 $body->setParts($this->_body);
154                 $this->_service->setBody($body);
155
156                 $transport->send($this->_service);
157                 return $this;
158         }
159
160
161 }
162