Git Repository Public Repository

namibia

URLs

Copy to Clipboard
1
2
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace Utility\Comms;


/**
 * Zend email wrapper class
 * @author Tjaart Viljoen
 * @copyright 2012 Nirph Online
 */
class Email
{
	/**
	 * Zend Mail object
	 * @var Zend_Mail
	 */
	protected $_service;

	/**
	 * Body tag to be compatible with ZF2 mail message
	 * @var array
	 */
	protected $_body = array();


	/**
	 * Summoning.
	 * @return \Utility\Comms\Email
	 */
	public function __construct()
	{
		$this->_service = new \Zend\Mail\Message();
	}

	/**
	 * Set message context.
	 * @param array $aContext
	 * @return \Utility\Comms\Email
	 */
	public function setContext(array $aContext = array())
	{
		foreach ($aContext as $sParam => $mValue)
		{
			switch ($sParam)
			{
				case 'From':
					if (!is_null($mValue)
						&& '' != $mValue)
					{
						$this->_service->setFrom($mValue);
					}
					break;
				case 'To':
			        if (IS_DEV_ENV || IS_STAGE_ENV)
			        {
			        	#-> Environmental override.
			        	\Utility\Debug::errorLog(
			        			'Email.send Override: ' . $mValue,
			        			isset($aContext['Subject'])
			        				? $aContext['Subject']
			        				: 'No subject'
                                . ' to ' . 'nirph@webmail.co.za'
						);
			        	$this->_service->addTo('testnirph@bonsaihut.co.za');
//                        $this->_service->addBcc('gerard@nirph.com');
			        }
					else
					{
						$this->_service->addTo($mValue);
					}
					break;
				case 'Cc':
			        if (!IS_DEV_ENV && !IS_STAGE_ENV)
			        {
						$this->_service->addCc($mValue);
					}
					break;
				case 'Bcc':
			        if (!IS_DEV_ENV && !IS_STAGE_ENV)
			        {
						$this->_service->addBcc($mValue);
					}
					break;
				case 'Subject':
					$this->_service->setSubject($mValue);
					break;
				case 'Body':
					$tmpBody = new \Zend\Mime\Part($mValue);
					$tmpBody->type = 'text/plain';
					$this->_body[] = $tmpBody;
					//$this->_service->setBodyText($mValue);
					break;
				case 'Html':
					$tmpBody = new \Zend\Mime\Part($mValue);
					$tmpBody->type = 'text/html';
					$this->_body[] = $tmpBody;
					//$this->_service->setBodyHtml($mValue);
					break;
				case 'ComplexAttachment':
					foreach ($mValue as $cid => $meta)
					{
						if(isset($meta['data']) && !empty($meta['data']))
						{
							$file = new \Zend\Mime\Part($meta['data']);
							$file->disposition = \Zend\Mime\Mime::DISPOSITION_INLINE;
							$file->encoding    = \Zend\Mime\Mime::ENCODING_BASE64;
							$file->description = 'Attached file';
							isset($meta['type'])
								&& $file->type = $meta['type'];
							isset($meta['filename'])
								&& $file->filename = $meta['filename'];
							$file->id = $cid;
							$this->_body[] = $file;
							//$this->_service->addAttachment($file);
						}
					}
					break;
				case 'Attachment':
					foreach ($mValue as $fileName => $fileData)
					{
						if(!empty($fileData))
						{
							$file = new \Zend\Mime\Part($fileData);
							$file->disposition = \Zend\Mime\Mime::DISPOSITION_INLINE;
							$file->encoding    = \Zend\Mime\Mime::ENCODING_BASE64;
							$file->description = 'Attached file';
							$file->filename    = $fileName;

							$this->_body[] = $file;
							//$this->_service->addAttachment($file);
						}
					}
					break;
			}
		}
		return $this;
	}

	/**
	 * Send message.
	 * @param array $aContext
	 * @return \Utility\Comms\Email
	 */
	public function send(array $aContext = array())
	{
		empty($aContext)
			|| $this->setContext($aContext);
		$transport = new \Zend\Mail\Transport\Sendmail();

		/*
		 * Alteration to include message body as a \Zend\Mime\Message() as per ZF2 specification
		 */
		$body = new \Zend\Mime\Message();
		$body->setParts($this->_body);
		$this->_service->setBody($body);

		$transport->send($this->_service);
		return $this;
	}


}

Commits for namibiamodule/Utility/src/Utility/Comms/Email.php

Diff revisions: vs.
Revision Author Commited Message
df0489 ... Mark Fri 14 Oct, 2016 10:01:00 +0000

initial commit