Mail::setFormatText()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * Manage Form
5
 *
6
 * @category  	lib
7
 * @author    	Judicaël Paquet <[email protected]>
8
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
9
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
10
 * @version   	Release: 1.0.0
11
 * @filesource	https://github.com/las93/venus2
12
 * @link      	https://github.com/las93
13
 * @since     	1.0
14
 */
15
namespace Venus\lib;
16
17
/**
18
 * This class manage the Form
19
 *
20
 * @category  	lib
21
 * @author    	Judicaël Paquet <[email protected]>
22
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
23
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
24
 * @version   	Release: 1.0.0
25
 * @filesource	https://github.com/las93/venus2
26
 * @link      	https://github.com/las93
27
 * @since     	1.0
28
 */
29
class Mail
30
{
31
	/**
32
	 * the recipient of mail
33
	 * @access private
34
	 * @var    array
35
	 */
36
	private $_aRecipient = array();
37
38
	/**
39
	 * the from of mail
40
	 * @access private
41
	 * @var    string
42
	*/
43
	private $_sFrom = "[email protected]";
44
45
	/**
46
	 * the subject of mail
47
	 * @access private
48
	 * @var    string
49
	 */
50
	private $_sSubject = "nosubject";
51
52
	/**
53
	 * the content of mail
54
	 * @access private
55
	 * @var    string
56
	 */
57
	private $_sMessage = "";
58
59
	/**
60
	 * the format of mail
61
	 * @access private
62
	 * @var    string
63
	 */
64
	private $_sFormat = "TXT"; // valeur : TXT ou HTML;
65
66
	/**
67
	 * file to send with mail
68
	 * @access private
69
	 * @var    array
70
	 */
71
	private $_aAttachments = array();
72
73
	/**
74
	 * add a recipient of mail
75
	 *
76
	 * @access public private
77
	 * @param  string $sRecipient
78
	 * @return Mail
79
	 */
80
	public function addRecipient(string $sRecipient) : Mail
81
	{
82
		$this->_aRecipient[] = $sRecipient;
83
		return $this;
84
	}
85
86
	/**
87
	 * add a file to the mail
88
	 *
89
	 * @access public private
90
	 * @param  string $sFileName
91
	 * @param  string $sContent
92
	 * @param  string $sType
93
	 * @return bool
94
	 */
95
	public function attachFile(string $sFileName, string $sContent, string $sType) : bool
96
	{
97
		$this->_aAttachments[] = array(
98
			"name" => $sFileName,
99
			"content" => $sContent,
100
			"type" => $sType
101
		);
102
		
103
		return true;
104
	}
105
106
	/**
107
	 * set the from of mail
108
	 *
109
	 * @access public private
110
	 * @param  string $sFrom
111
	 * @return Mail
112
	 */
113
	public function setFrom(string $sFrom) : Mail
114
	{
115
		$this->_sFrom = $sFrom;
116
		return $this;
117
	}
118
119
	/**
120
	 * set the subjet of mail
121
	 *
122
	 * @access public private
123
	 * @param  string $sSubject
124
	 * @return Mail
125
	 */
126
	public function setSubject(string $sSubject) : Mail
127
	{
128
		$this->_sSubject = $sSubject;
129
		return $this;
130
	}
131
132
	/**
133
	 * set the message of mail
134
	 *
135
	 * @access public private
136
	 * @param  string $sMessage
137
	 * @return Mail
138
	 */
139
	public function setMessage(string $sMessage) : Mail
140
	{
141
		$this->_sMessage = $sMessage;
142
		return $this;
143
	}
144
145
	/**
146
	 * set the format HTML of mail
147
	 *
148
	 * @access public private
149
	 * @return Mail
150
	 */
151
	public function setFormatHtml() : Mail
152
	{
153
		$this->_sFormat = "HTML";
154
		return $this;
155
	}
156
157
	/**
158
	 * set the format HTML of mail
159
	 *
160
	 * @access public private
161
	 * @return Mail
162
	 */
163
	public function setFormatText() : Mail
164
	{
165
		$this->_sFormat = "TXT";
166
		return $this;
167
	}
168
169
	/**
170
	 * send the mail
171
	 *
172
	 * @access public private
173
	 * @return bool
174
	 */
175
	public function send() : bool
176
	{
177
		$sHeaders = 'From: ' . $this->_sFrom . "\r\n";
178
179
		if (empty($this->_aAttachments)) {
180
			
181
			if ($this->_sFormat == "HTML") {
182
				
183
				$sHeaders .= 'MIME-Version: 1.0' . "\r\n";
184
				$sHeaders .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
185
			}
186
			
187
			return mail(implode(',', $this->_aRecipient), $this->_sSubject, $this->_sMessage, $sHeaders);
188
		}
189
		else {
190
191
			$sBoundary = "_" . md5(uniqid(rand()));
192
193
			$sAttached = "";
194
195
			foreach ($this->_aAttachments as $aAttachment) {
196
				
197
				$sAttached_file = chunk_split(base64_encode($aAttachment["content"]));
198
				$sAttached = "\n\n" . "--" . $sBoundary . "\nContent-Type: application; name=\"" . $aAttachment["name"] . "\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"" . $aAttachment["name"] . "\"\r\n\n" . $sAttached_file . "--" . $sBoundary . "--";
199
			}
200
201
			$sHeaders = 'From: ' . $this->_sFrom . "\r\n";
202
			$sHeaders .= "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"$sBoundary\"\r\n";
203
204
			$sBody = "--" . $sBoundary . "\nContent-Type: " . ($this->_sFormat == "HTML" ? "text/html" : "text/plain") . "; charset=UTF-8\r\n\n" . $this->_sMessage . $sAttached;
205
206
			return mail(implode(',', $this->_aRecipient), $this->_sSubject, $sBody, $sHeaders);
207
		}
208
	}
209
}
210