1 <?php
2 /* --------------------------------------------------------------
3 EmailFactory.inc.php 2015-07-21 gm
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2015 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('EmailFactoryInterface');
13
14 /**
15 * Class EmailFactory
16 *
17 * @category System
18 * @package Email
19 */
20 class EmailFactory implements EmailFactoryInterface
21 {
22 /**
23 * Query builder.
24 * @var CI_DB_query_builder
25 */
26 protected $db;
27
28
29 /**
30 * Class Constructor
31 *
32 * @param CI_DB_query_builder $db Query builder.
33 */
34 public function __construct(CI_DB_query_builder $db)
35 {
36 $this->db = $db;
37 }
38
39
40 /**
41 * Creates an email object
42 *
43 * @param IdType $id (optional) E-Mail ID.
44 * @param EmailSubjectInterface $subject (optional) E-Mail subject.
45 * @param EmailContentInterface $contentPlain (optional) E-Mail plain content.
46 * @param EmailContentInterface $contentHtml (optional) E-Mail HTML content.
47 * @param bool $p_isPending (optional) E-Mail is pending?
48 * @param ContactCollectionInterface $contacts (optional) E-Mail contacts.
49 * @param AttachmentCollectionInterface $attachments (optional) E-Mail attachments.
50 *
51 * @throws InvalidArgumentException If '$p_isPending' is not a bool, or if any other argument is not valid.
52 * @throws UnexpectedValueException If the contact type was not found
53 *
54 * @return Email The created email.
55 */
56 public function createEmail(IdType $id = null,
57 EmailSubjectInterface $subject = null,
58 EmailContentInterface $contentHtml = null,
59 EmailContentInterface $contentPlain = null,
60 $p_isPending = true,
61 ContactCollectionInterface $contacts = null,
62 AttachmentCollectionInterface $attachments = null)
63
64 {
65 if(!is_bool($p_isPending))
66 {
67 throw new InvalidArgumentException('Invalid $p_isPending argument given (bool expected): '
68 . print_r($p_isPending, true));
69 }
70
71 $email = MainFactory::create('Email');
72
73 // Set email information.
74 if($id !== null)
75 {
76 $email->setId($id);
77 }
78
79 if($subject !== null)
80 {
81 $email->setSubject($subject);
82 }
83
84 if($contentPlain !== null)
85 {
86 $email->setContentPlain($contentPlain);
87 }
88
89 if($contentHtml !== null)
90 {
91 $email->setContentHtml($contentHtml);
92 }
93
94 $email->setPending($p_isPending);
95
96 // Set email contacts.
97 if($contacts !== null)
98 {
99 foreach($contacts->getArray() as $contact)
100 {
101 switch($contact->getContactType())
102 {
103 case ContactType::SENDER:
104 $email->setSender($contact);
105 break;
106 case ContactType::RECIPIENT:
107 $email->setRecipient($contact);
108 break;
109 case ContactType::REPLY_TO:
110 $email->setReplyTo($contact);
111 break;
112 case ContactType::BCC:
113 $email->getBcc()->add($contact);
114 break;
115 case ContactType::CC:
116 $email->getCc()->add($contact);
117 break;
118 default:
119 throw new UnexpectedValueException('Unexpected contact type: ' . $contact->getContactType());
120 }
121 }
122 }
123
124 // Set email attachments collection.
125 if($attachments !== null)
126 {
127 $email->setAttachments($attachments);
128 }
129
130 return $email;
131 }
132
133
134 /**
135 * Creates an email contact object
136 *
137 * @param EmailAddressInterface $emailAddress Email address of the contact.
138 * @param ContactTypeInterface $contactType Contact type (see ContactType class definition).
139 * @param ContactNameInterface $contactName (optional) Contact display name.
140 *
141 * @return EmailContact The created email contact.
142 */
143 public function createContact(EmailAddressInterface $emailAddress,
144 ContactTypeInterface $contactType,
145 ContactNameInterface $contactName = null)
146 {
147 return MainFactory::create('EmailContact', $emailAddress, $contactType, $contactName);
148 }
149
150
151 /**
152 * Creates an email attachment object
153 *
154 * @param AttachmentPathInterface $path Valid path of the attachment (on the server).
155 * @param AttachmentNameInterface $name (optional) Display name for the attachment.
156 *
157 * @return EmailAttachment The created email attachment.
158 */
159 public function createAttachment(AttachmentPathInterface $path, AttachmentNameInterface $name = null)
160 {
161 return MainFactory::create('EmailAttachment', $path, $name);
162 }
163
164
165 /**
166 * Creates a mailer adapter object
167 *
168 * @return MailerAdapter The created mailer adapter.
169 */
170 public function createMailerAdapter()
171 {
172 $mailer = $this->createMailer();
173
174 return MainFactory::create('MailerAdapter', $mailer);
175 }
176
177
178 /**
179 * Creates a PHP mailer object.
180 *
181 * @param string $protocol (Optional) Provide 'smtp', 'sendmail' or 'mail' if you want to override the
182 * EMAIL_TRANSPORT constant.
183 *
184 * @return PHPMailer The created PHP mailer.
185 */
186 public function createMailer($protocol = null)
187 {
188 $mailer = new PHPMailer(true);
189 $mailer->SMTPDebug = 0; // Disable debug output.
190
191 // Set PHPMailer CharSet
192 if(isset($_SESSION['language_charset']))
193 {
194 $mailer->CharSet = $_SESSION['language_charset'];
195 }
196 else
197 {
198 $row = $this->db->get_where(TABLE_LANGUAGES, array('code' => DEFAULT_LANGUAGE))->row_array();
199 $mailer->CharSet = $row['language_charset'];
200 }
201
202 // Set PHPMailer Language
203 if($_SESSION['language'] === 'german')
204 {
205 $mailer->setLanguage('de', DIR_WS_CLASSES);
206 }
207 else
208 {
209 $mailer->setLanguage('en', DIR_WS_CLASSES);
210 }
211
212 // Set PHPMailer Protocol
213 $protocol = ($protocol !== null) ? $protocol : EMAIL_TRANSPORT;
214
215 switch($protocol)
216 {
217 case 'smtp':
218 $mailer->IsSMTP();
219 $mailer->SMTPKeepAlive = true; // set mailer to use SMTP
220 $mailer->SMTPAuth = SMTP_AUTH; // turn on SMTP authentication true/false
221 $mailer->Username = SMTP_USERNAME; // SMTP username
222 $mailer->Password = SMTP_PASSWORD; // SMTP password
223 $mailer->Host = SMTP_MAIN_SERVER . ';'
224 . SMTP_Backup_Server; // specify main and backup server "smtp1.example.com;smtp2.example.com"
225 if(SMTP_ENCRYPTION == 'ssl' || SMTP_ENCRYPTION == 'tls')
226 {
227 $mailer->SMTPSecure = SMTP_ENCRYPTION;
228 }
229 break;
230
231 case 'sendmail':
232 $mailer->IsSendmail();
233 $mailer->Sendmail = SENDMAIL_PATH;
234 break;
235
236 case 'mail':
237 $mailer->IsMail();
238 break;
239 }
240
241 return $mailer;
242 }
243
244
245 /**
246 * Creates an email service object
247 *
248 * @return EmailService The created email service.
249 */
250 public function createService()
251 {
252 return MainFactory::create('EmailService', $this->createRepository(), $this, $this->createMailerAdapter(),
253 $this->createAttachmentsHandler());
254 }
255
256
257 /**
258 * Creates an email repository object
259 *
260 * @return EmailRepository The created email repository.
261 */
262 public function createRepository()
263 {
264 return MainFactory::create('EmailRepository', $this->createWriter(), $this->createReader(),
265 $this->createDeleter());
266 }
267
268
269 /**
270 * Creates an email writer object
271 *
272 * @return EmailWriter The created email writer.
273 */
274 public function createWriter()
275 {
276 return MainFactory::create('EmailWriter', $this->_getDbConnection());
277 }
278
279
280 /**
281 * Create EmailReader Object
282 *
283 * @return EmailReader The created email deleter.
284 */
285 public function createReader()
286 {
287 return MainFactory::create('EmailReader', $this->_getDbConnection(), $this);
288 }
289
290
291 /**
292 * Creates email deleter object
293 *
294 * @return EmailDeleter The created email deleter.
295 */
296 public function createDeleter()
297 {
298 return MainFactory::create('EmailDeleter', $this->_getDbConnection());
299 }
300
301
302 /**
303 * Creates an attachments handler object
304 *
305 * @param string $p_uploadsDirPath (optional) You can specify a custom uploads directory path if you do not want
306 * the default "uploads" directory. The path must contain a "tmp" and an
307 * "attachments" directory otherwise the AttachmentsHandler class will not work
308 * properly.
309 *
310 * @return AttachmentsHandler The created attachments handler.
311 */
312 public function createAttachmentsHandler($p_uploadsDirPath = null)
313 {
314 $uploadsDirPath = (!empty($p_uploadsDirPath)) ? $p_uploadsDirPath : DIR_FS_CATALOG . 'uploads';
315
316 return MainFactory::create('AttachmentsHandler', $uploadsDirPath);
317 }
318
319
320 /**
321 * Returns a database connection.
322 *
323 * @return CI_DB_query_builder Database connection.
324 */
325 protected function _getDbConnection()
326 {
327 return $this->db;
328 }
329 }
330