Completed
Push — master ( dc17eb...040d62 )
by Jacob
05:42
created
src/Archangel.php 1 patch
Indentation   +411 added lines, -412 removed lines patch added patch discarded remove patch
@@ -16,418 +16,417 @@
 block discarded – undo
16 16
 class Archangel implements LoggerAwareInterface
17 17
 {
18 18
 
19
-    /** @var string $subject */
20
-    protected $subject;
21
-
22
-    /** @var array $toAddresses */
23
-    protected $toAddresses = array();
24
-
25
-    /** @var array $ccAddresses */
26
-    protected $ccAddresses = array();
27
-
28
-    /** @var array $bccAddresses */
29
-    protected $bccAddresses = array();
30
-
31
-    /** @var array $headers */
32
-    protected $headers = array();
33
-
34
-    /** @var string $plainMessage */
35
-    protected $plainMessage;
36
-
37
-    /** @var string $htmlMessage */
38
-    protected $htmlMessage;
39
-
40
-    /** @var array $attachments */
41
-    protected $attachments = array();
42
-
43
-    /** @var string $boundary */
44
-    protected $boundary;
45
-
46
-    /** @var string $alternativeBoundary */
47
-    protected $alternativeBoundary;
48
-
49
-    /** @var LoggerInterface */
50
-    protected $logger;
51
-
52
-    /** @var string LINE_BREAK */
53
-    const LINE_BREAK = "\r\n";
54
-
55
-    /**
56
-     * @param string $mailer
57
-     */
58
-    public function __construct($mailer = null)
59
-    {
60
-        if (is_null($mailer)) {
61
-            $mailer = sprintf('PHP/%s', phpversion());
62
-        }
63
-        $this->headers['X-Mailer'] = $mailer;
64
-
65
-        $this->logger = new NullLogger();
66
-    }
67
-
68
-    /**
69
-     * @param LoggerInterface $logger
70
-     *
71
-     * @return $this;
72
-     */
73
-    public function setLogger(LoggerInterface $logger)
74
-    {
75
-        $this->logger = $logger;
76
-
77
-        return $this;
78
-    }
79
-
80
-    /**
81
-     * Setter method for adding recipients
82
-     *
83
-     * @param string $address email address for the recipient
84
-     * @param string $title   name of the recipient (optional)
85
-
86
-     * @return object instantiated $this
87
-     */
88
-    public function addTo($address, $title = '')
89
-    {
90
-        array_push(
91
-            $this->toAddresses,
92
-            $this->formatEmailAddress($address, $title)
93
-        );
94
-
95
-        return $this;
96
-    }
97
-
98
-    /**
99
-     * Setter method for adding cc recipients
100
-     *
101
-     * @param string $address email address for the cc recipient
102
-     * @param string $title   name of the cc recipient (optional)
103
-     *
104
-     * @return object instantiated $this
105
-     */
106
-    public function addCC($address, $title = '')
107
-    {
108
-        array_push(
109
-            $this->ccAddresses,
110
-            $this->formatEmailAddress($address, $title)
111
-        );
112
-
113
-        return $this;
114
-    }
115
-
116
-    /**
117
-     * Setter method for adding bcc recipients
118
-     *
119
-     * @param string $address email address for the bcc recipient
120
-     * @param string $title   name of the bcc recipient (optional)
121
-     *
122
-     * @return object instantiated $this
123
-     */
124
-    public function addBCC($address, $title = '')
125
-    {
126
-        array_push(
127
-            $this->bccAddresses,
128
-            $this->formatEmailAddress($address, $title)
129
-        );
130
-
131
-        return $this;
132
-    }
133
-
134
-    /**
135
-     * Setter method for setting the single 'from' field
136
-     *
137
-     * @param string $address email address for the sender
138
-     * @param string $title   name of the sender (optional)
139
-     *
140
-     * @return object instantiated $this
141
-     */
142
-    public function setFrom($address, $title = '')
143
-    {
144
-        $this->headers['From'] = $this->formatEmailAddress($address, $title);
145
-
146
-        return $this;
147
-    }
148
-
149
-    /**
150
-     * Setter method for setting the single 'reply-to' field
151
-     *
152
-     * @param string $address email address for the reply-to
153
-     * @param string $title   name of the reply-to (optional)
154
-     *
155
-     * @return object instantiated $this
156
-     */
157
-    public function setReplyTo($address, $title = '')
158
-    {
159
-        $this->headers['Reply-To'] = $this->formatEmailAddress($address, $title);
160
-
161
-        return $this;
162
-    }
163
-
164
-    /**
165
-     * @param string $address
166
-     * @param string $title
167
-     *
168
-     * @return string
169
-     */
170
-    protected function formatEmailAddress($address, $title)
171
-    {
172
-        if (!empty($title)) {
173
-            $address = sprintf('"%s" <%s>', $title, $address);
174
-        }
175
-        return $address;
176
-    }
177
-
178
-    /**
179
-     * Setter method for setting a subject
180
-     *
181
-     * @param string $subject subject for the email
182
-     *
183
-     * @return object instantiated $this
184
-     */
185
-    public function setSubject($subject)
186
-    {
187
-        $this->subject = $subject;
188
-
189
-        return $this;
190
-    }
191
-
192
-    /**
193
-     * Setter method for the plain text message
194
-     *
195
-     * @param string $message the plain-text message
196
-     *
197
-     * @return object instantiated $this
198
-     */
199
-    public function setPlainMessage($message)
200
-    {
201
-        $this->plainMessage = $message;
202
-
203
-        return $this;
204
-    }
205
-
206
-    /**
207
-     * Setter method for the html message
208
-     *
209
-     * @param string $message the html message
210
-     *
211
-     * @return object instantiated $this
212
-     */
213
-    public function setHTMLMessage($message)
214
-    {
215
-        $this->htmlMessage = $message;
216
-
217
-        return $this;
218
-    }
219
-
220
-    /**
221
-     * Setter method for adding attachments
222
-     *
223
-     * @param string $path  the full path of the attachment
224
-     * @param string $type  mime type of the file
225
-     * @param string $title the title of the attachment (optional)
226
-     *
227
-     * @return object instantiated $this
228
-     */
229
-    public function addAttachment($path, $type, $title = '')
230
-    {
231
-        array_push($this->attachments, array(
232
-          'path' => $path,
233
-          'type' => $type,
234
-          'title' => $title,
235
-        ));
236
-
237
-        return $this;
238
-    }
239
-
240
-    /**
241
-     * The executing step, the actual sending of the email
242
-     * First checks to make sure the minimum fields are set (returns false if they are not)
243
-     * Second it attempts to send the mail with php's mail() (returns false if it fails)
244
-     *
245
-     * return boolean whether or not the email was valid & sent
246
-     */
247
-    public function send()
248
-    {
249
-        if (!$this->checkRequiredFields()) {
250
-            return false;
251
-        }
252
-
253
-        $recipients = $this->buildTo();
254
-        $subject = $this->subject;
255
-        $message = $this->buildMessage();
256
-        $headers = $this->buildHeaders();
257
-
258
-        return mail($recipients, $subject, $message, $headers);
259
-    }
260
-
261
-    /**
262
-     * Call to check the minimum required fields
263
-     *
264
-     * @return boolean whether or not the email meets the minimum required fields
265
-     */
266
-    protected function checkRequiredFields()
267
-    {
268
-        if (empty($this->toAddresses)) {
269
-            return false;
270
-        }
271
-        if (empty($this->subject)) {
272
-            return false;
273
-        }
274
-
275
-        if (empty($this->plainMessage) && empty($this->htmlMessage) && empty($this->attachments)) {
276
-            return false;
277
-        }
278
-
279
-        return true;
280
-    }
281
-
282
-    /**
283
-     * Build the recipients from 'to'
284
-     *
285
-     * @return string comma-separated lit of recipients
286
-     */
287
-    protected function buildTo()
288
-    {
289
-        return implode(', ', $this->toAddresses);
290
-    }
291
-
292
-    /**
293
-     * Long, nasty creater of the actual message, with all the multipart logic you'd never want to see
294
-     *
295
-     * @return string email message
296
-     */
297
-    protected function buildMessage()
298
-    {
299
-        $messageString = '';
19
+	/** @var string $subject */
20
+	protected $subject;
21
+
22
+	/** @var array $toAddresses */
23
+	protected $toAddresses = array();
24
+
25
+	/** @var array $ccAddresses */
26
+	protected $ccAddresses = array();
27
+
28
+	/** @var array $bccAddresses */
29
+	protected $bccAddresses = array();
30
+
31
+	/** @var array $headers */
32
+	protected $headers = array();
33
+
34
+	/** @var string $plainMessage */
35
+	protected $plainMessage;
36
+
37
+	/** @var string $htmlMessage */
38
+	protected $htmlMessage;
39
+
40
+	/** @var array $attachments */
41
+	protected $attachments = array();
42
+
43
+	/** @var string $boundary */
44
+	protected $boundary;
45
+
46
+	/** @var string $alternativeBoundary */
47
+	protected $alternativeBoundary;
48
+
49
+	/** @var LoggerInterface */
50
+	protected $logger;
51
+
52
+	/** @var string LINE_BREAK */
53
+	const LINE_BREAK = "\r\n";
54
+
55
+	/**
56
+	 * @param string $mailer
57
+	 */
58
+	public function __construct($mailer = null)
59
+	{
60
+		if (is_null($mailer)) {
61
+			$mailer = sprintf('PHP/%s', phpversion());
62
+		}
63
+		$this->headers['X-Mailer'] = $mailer;
64
+
65
+		$this->logger = new NullLogger();
66
+	}
67
+
68
+	/**
69
+	 * @param LoggerInterface $logger
70
+	 *
71
+	 * @return $this;
72
+	 */
73
+	public function setLogger(LoggerInterface $logger)
74
+	{
75
+		$this->logger = $logger;
76
+
77
+		return $this;
78
+	}
79
+
80
+	/**
81
+	 * Setter method for adding recipients
82
+	 *
83
+	 * @param string $address email address for the recipient
84
+	 * @param string $title   name of the recipient (optional)
85
+	 * @return object instantiated $this
86
+	 */
87
+	public function addTo($address, $title = '')
88
+	{
89
+		array_push(
90
+			$this->toAddresses,
91
+			$this->formatEmailAddress($address, $title)
92
+		);
93
+
94
+		return $this;
95
+	}
96
+
97
+	/**
98
+	 * Setter method for adding cc recipients
99
+	 *
100
+	 * @param string $address email address for the cc recipient
101
+	 * @param string $title   name of the cc recipient (optional)
102
+	 *
103
+	 * @return object instantiated $this
104
+	 */
105
+	public function addCC($address, $title = '')
106
+	{
107
+		array_push(
108
+			$this->ccAddresses,
109
+			$this->formatEmailAddress($address, $title)
110
+		);
111
+
112
+		return $this;
113
+	}
114
+
115
+	/**
116
+	 * Setter method for adding bcc recipients
117
+	 *
118
+	 * @param string $address email address for the bcc recipient
119
+	 * @param string $title   name of the bcc recipient (optional)
120
+	 *
121
+	 * @return object instantiated $this
122
+	 */
123
+	public function addBCC($address, $title = '')
124
+	{
125
+		array_push(
126
+			$this->bccAddresses,
127
+			$this->formatEmailAddress($address, $title)
128
+		);
129
+
130
+		return $this;
131
+	}
132
+
133
+	/**
134
+	 * Setter method for setting the single 'from' field
135
+	 *
136
+	 * @param string $address email address for the sender
137
+	 * @param string $title   name of the sender (optional)
138
+	 *
139
+	 * @return object instantiated $this
140
+	 */
141
+	public function setFrom($address, $title = '')
142
+	{
143
+		$this->headers['From'] = $this->formatEmailAddress($address, $title);
144
+
145
+		return $this;
146
+	}
147
+
148
+	/**
149
+	 * Setter method for setting the single 'reply-to' field
150
+	 *
151
+	 * @param string $address email address for the reply-to
152
+	 * @param string $title   name of the reply-to (optional)
153
+	 *
154
+	 * @return object instantiated $this
155
+	 */
156
+	public function setReplyTo($address, $title = '')
157
+	{
158
+		$this->headers['Reply-To'] = $this->formatEmailAddress($address, $title);
159
+
160
+		return $this;
161
+	}
162
+
163
+	/**
164
+	 * @param string $address
165
+	 * @param string $title
166
+	 *
167
+	 * @return string
168
+	 */
169
+	protected function formatEmailAddress($address, $title)
170
+	{
171
+		if (!empty($title)) {
172
+			$address = sprintf('"%s" <%s>', $title, $address);
173
+		}
174
+		return $address;
175
+	}
176
+
177
+	/**
178
+	 * Setter method for setting a subject
179
+	 *
180
+	 * @param string $subject subject for the email
181
+	 *
182
+	 * @return object instantiated $this
183
+	 */
184
+	public function setSubject($subject)
185
+	{
186
+		$this->subject = $subject;
187
+
188
+		return $this;
189
+	}
190
+
191
+	/**
192
+	 * Setter method for the plain text message
193
+	 *
194
+	 * @param string $message the plain-text message
195
+	 *
196
+	 * @return object instantiated $this
197
+	 */
198
+	public function setPlainMessage($message)
199
+	{
200
+		$this->plainMessage = $message;
201
+
202
+		return $this;
203
+	}
204
+
205
+	/**
206
+	 * Setter method for the html message
207
+	 *
208
+	 * @param string $message the html message
209
+	 *
210
+	 * @return object instantiated $this
211
+	 */
212
+	public function setHTMLMessage($message)
213
+	{
214
+		$this->htmlMessage = $message;
215
+
216
+		return $this;
217
+	}
218
+
219
+	/**
220
+	 * Setter method for adding attachments
221
+	 *
222
+	 * @param string $path  the full path of the attachment
223
+	 * @param string $type  mime type of the file
224
+	 * @param string $title the title of the attachment (optional)
225
+	 *
226
+	 * @return object instantiated $this
227
+	 */
228
+	public function addAttachment($path, $type, $title = '')
229
+	{
230
+		array_push($this->attachments, array(
231
+		  'path' => $path,
232
+		  'type' => $type,
233
+		  'title' => $title,
234
+		));
235
+
236
+		return $this;
237
+	}
238
+
239
+	/**
240
+	 * The executing step, the actual sending of the email
241
+	 * First checks to make sure the minimum fields are set (returns false if they are not)
242
+	 * Second it attempts to send the mail with php's mail() (returns false if it fails)
243
+	 *
244
+	 * return boolean whether or not the email was valid & sent
245
+	 */
246
+	public function send()
247
+	{
248
+		if (!$this->checkRequiredFields()) {
249
+			return false;
250
+		}
251
+
252
+		$recipients = $this->buildTo();
253
+		$subject = $this->subject;
254
+		$message = $this->buildMessage();
255
+		$headers = $this->buildHeaders();
256
+
257
+		return mail($recipients, $subject, $message, $headers);
258
+	}
259
+
260
+	/**
261
+	 * Call to check the minimum required fields
262
+	 *
263
+	 * @return boolean whether or not the email meets the minimum required fields
264
+	 */
265
+	protected function checkRequiredFields()
266
+	{
267
+		if (empty($this->toAddresses)) {
268
+			return false;
269
+		}
270
+		if (empty($this->subject)) {
271
+			return false;
272
+		}
273
+
274
+		if (empty($this->plainMessage) && empty($this->htmlMessage) && empty($this->attachments)) {
275
+			return false;
276
+		}
277
+
278
+		return true;
279
+	}
280
+
281
+	/**
282
+	 * Build the recipients from 'to'
283
+	 *
284
+	 * @return string comma-separated lit of recipients
285
+	 */
286
+	protected function buildTo()
287
+	{
288
+		return implode(', ', $this->toAddresses);
289
+	}
290
+
291
+	/**
292
+	 * Long, nasty creater of the actual message, with all the multipart logic you'd never want to see
293
+	 *
294
+	 * @return string email message
295
+	 */
296
+	protected function buildMessage()
297
+	{
298
+		$messageString = '';
300 299
         
301
-        if (!empty($this->attachments)) {
302
-            $messageString .= "--{$this->getBoundary()}" . self::LINE_BREAK;
303
-        }
304
-        if (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
305
-            if (!empty($this->attachments)) {
306
-                $messageString .= "Content-Type: multipart/alternative; boundary={$this->getAlternativeBoundary()}" . self::LINE_BREAK;
307
-                $messageString .= self::LINE_BREAK;
308
-            }
309
-            $messageString .= "--{$this->getAlternativeBoundary()}" . self::LINE_BREAK;
310
-            $messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK;
311
-            $messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
312
-            $messageString .= self::LINE_BREAK;
313
-            $messageString .= $this->plainMessage;
314
-            $messageString .= self::LINE_BREAK;
315
-            $messageString .= "--{$this->getAlternativeBoundary()}" . self::LINE_BREAK;
316
-            $messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK;
317
-            $messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
318
-            $messageString .= self::LINE_BREAK;
319
-            $messageString .= $this->htmlMessage;
320
-            $messageString .= self::LINE_BREAK;
321
-            $messageString .= "--{$this->getAlternativeBoundary()}--" . self::LINE_BREAK;
322
-            $messageString .= self::LINE_BREAK;
323
-        } elseif (!empty($this->plainMessage)) {
324
-            if (!empty($this->attachments)) {
325
-                $messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK;
326
-                $messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
327
-                $messageString .= self::LINE_BREAK;
328
-            }
329
-            $messageString .= $this->plainMessage;
330
-            $messageString .= self::LINE_BREAK;
331
-        } elseif (!empty($this->htmlMessage)) {
332
-            if (!empty($this->attachments)) {
333
-                $messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK;
334
-                $messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
335
-                $messageString .= self::LINE_BREAK;
336
-            }
337
-            $messageString .= $this->htmlMessage;
338
-            $messageString .= self::LINE_BREAK;
339
-        }
340
-        if (!empty($this->attachments)) {
341
-            foreach ($this->attachments as $attachment) {
342
-                $messageString .= "--{$this->getBoundary()}" . self::LINE_BREAK;
343
-                $messageString .= "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\"" . self::LINE_BREAK;
344
-                $messageString .= 'Content-Transfer-Encoding: base64' . self::LINE_BREAK;
345
-                $messageString .= 'Content-Disposition: attachment' . self::LINE_BREAK;
346
-                $messageString .= self::LINE_BREAK;
347
-                $messageString .= $this->buildAttachmentContent($attachment);
348
-                $messageString .= self::LINE_BREAK;
349
-            }
350
-            $messageString .= "--{$this->getBoundary()}--" . self::LINE_BREAK;
351
-        }
352
-        return $messageString;
353
-    }
354
-
355
-
356
-    /**
357
-     * Builder for the additional headers needed for multipart emails
358
-     *
359
-     * @return string headers needed for multipart
360
-     */
361
-    protected function buildHeaders()
362
-    {
363
-        $headerString = '';
364
-        foreach ($this->headers as $key => $value) {
365
-            $headerString .= sprintf('%s: %s', $key, $value) . self::LINE_BREAK;
366
-        }
367
-
368
-        if (!empty($this->ccAddresses)) {
369
-            $headerString .= 'CC: ' . implode(', ', $this->ccAddresses) . self::LINE_BREAK;
370
-        }
371
-        if (!empty($this->bccAddresses)) {
372
-            $headerString .= 'BCC: ' . implode(', ', $this->bccAddresses) . self::LINE_BREAK;
373
-        }
300
+		if (!empty($this->attachments)) {
301
+			$messageString .= "--{$this->getBoundary()}" . self::LINE_BREAK;
302
+		}
303
+		if (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
304
+			if (!empty($this->attachments)) {
305
+				$messageString .= "Content-Type: multipart/alternative; boundary={$this->getAlternativeBoundary()}" . self::LINE_BREAK;
306
+				$messageString .= self::LINE_BREAK;
307
+			}
308
+			$messageString .= "--{$this->getAlternativeBoundary()}" . self::LINE_BREAK;
309
+			$messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK;
310
+			$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
311
+			$messageString .= self::LINE_BREAK;
312
+			$messageString .= $this->plainMessage;
313
+			$messageString .= self::LINE_BREAK;
314
+			$messageString .= "--{$this->getAlternativeBoundary()}" . self::LINE_BREAK;
315
+			$messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK;
316
+			$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
317
+			$messageString .= self::LINE_BREAK;
318
+			$messageString .= $this->htmlMessage;
319
+			$messageString .= self::LINE_BREAK;
320
+			$messageString .= "--{$this->getAlternativeBoundary()}--" . self::LINE_BREAK;
321
+			$messageString .= self::LINE_BREAK;
322
+		} elseif (!empty($this->plainMessage)) {
323
+			if (!empty($this->attachments)) {
324
+				$messageString .= 'Content-Type: text/plain; charset="iso-8859"' . self::LINE_BREAK;
325
+				$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
326
+				$messageString .= self::LINE_BREAK;
327
+			}
328
+			$messageString .= $this->plainMessage;
329
+			$messageString .= self::LINE_BREAK;
330
+		} elseif (!empty($this->htmlMessage)) {
331
+			if (!empty($this->attachments)) {
332
+				$messageString .= 'Content-Type: text/html; charset="iso-8859-1"' . self::LINE_BREAK;
333
+				$messageString .= 'Content-Transfer-Encoding: 7bit' . self::LINE_BREAK;
334
+				$messageString .= self::LINE_BREAK;
335
+			}
336
+			$messageString .= $this->htmlMessage;
337
+			$messageString .= self::LINE_BREAK;
338
+		}
339
+		if (!empty($this->attachments)) {
340
+			foreach ($this->attachments as $attachment) {
341
+				$messageString .= "--{$this->getBoundary()}" . self::LINE_BREAK;
342
+				$messageString .= "Content-Type: {$attachment['type']}; name=\"{$attachment['title']}\"" . self::LINE_BREAK;
343
+				$messageString .= 'Content-Transfer-Encoding: base64' . self::LINE_BREAK;
344
+				$messageString .= 'Content-Disposition: attachment' . self::LINE_BREAK;
345
+				$messageString .= self::LINE_BREAK;
346
+				$messageString .= $this->buildAttachmentContent($attachment);
347
+				$messageString .= self::LINE_BREAK;
348
+			}
349
+			$messageString .= "--{$this->getBoundary()}--" . self::LINE_BREAK;
350
+		}
351
+		return $messageString;
352
+	}
353
+
354
+
355
+	/**
356
+	 * Builder for the additional headers needed for multipart emails
357
+	 *
358
+	 * @return string headers needed for multipart
359
+	 */
360
+	protected function buildHeaders()
361
+	{
362
+		$headerString = '';
363
+		foreach ($this->headers as $key => $value) {
364
+			$headerString .= sprintf('%s: %s', $key, $value) . self::LINE_BREAK;
365
+		}
366
+
367
+		if (!empty($this->ccAddresses)) {
368
+			$headerString .= 'CC: ' . implode(', ', $this->ccAddresses) . self::LINE_BREAK;
369
+		}
370
+		if (!empty($this->bccAddresses)) {
371
+			$headerString .= 'BCC: ' . implode(', ', $this->bccAddresses) . self::LINE_BREAK;
372
+		}
374 373
         
375
-        if (!empty($this->attachments)) {
376
-            $headerString .= "Content-Type: multipart/mixed; boundary=\"{$this->getBoundary()}\"";
377
-        } elseif (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
378
-            $headerString .= "Content-Type: multipart/alternative; boundary=\"{$this->getAlternativeBoundary()}\"";
379
-        } elseif (!empty($this->htmlMessage)) {
380
-            $headerString .= 'Content-type: text/html; charset="iso-8859-1"';
381
-        }
382
-
383
-        return $headerString;
384
-    }
385
-
386
-    /**
387
-     * File reader for attachments
388
-     *
389
-     * @return string binary representation of file, base64'd
390
-     */
391
-    protected function buildAttachmentContent($attachment)
392
-    {
393
-        if (!file_exists($attachment['path'])) {
394
-            return ''; // todo log error
395
-        }
396
-
397
-        $handle = fopen($attachment['path'], 'r');
398
-        $contents = fread($handle, filesize($attachment['path']));
399
-        fclose($handle);
400
-
401
-        $contents = base64_encode($contents);
402
-        $contents = chunk_split($contents);
403
-        return $contents;
404
-    }
405
-
406
-    /**
407
-     * Holder for the boundry logic
408
-     * Not called/created unless it's needed
409
-     *
410
-     * @return  string  boundary
411
-     */
412
-    protected function getBoundary()
413
-    {
414
-        if (!isset($this->boundary)) {
415
-            $this->boundary = sprintf('PHP-mixed-%s', uniqid());
416
-        }
417
-        return $this->boundary;
418
-    }
419
-
420
-    /**
421
-     * Holder to create the alternative boundry logic
422
-     * Not called/created unless it's needed
423
-     *
424
-     * @return string alternative boundary
425
-     */
426
-    protected function getAlternativeBoundary()
427
-    {
428
-        if (!isset($this->alternativeBoundary)) {
429
-            $this->alternativeBoundary = sprintf('PHP-alternative-%s', uniqid());
430
-        }
431
-        return $this->alternativeBoundary;
432
-    }
374
+		if (!empty($this->attachments)) {
375
+			$headerString .= "Content-Type: multipart/mixed; boundary=\"{$this->getBoundary()}\"";
376
+		} elseif (!empty($this->plainMessage) && !empty($this->htmlMessage)) {
377
+			$headerString .= "Content-Type: multipart/alternative; boundary=\"{$this->getAlternativeBoundary()}\"";
378
+		} elseif (!empty($this->htmlMessage)) {
379
+			$headerString .= 'Content-type: text/html; charset="iso-8859-1"';
380
+		}
381
+
382
+		return $headerString;
383
+	}
384
+
385
+	/**
386
+	 * File reader for attachments
387
+	 *
388
+	 * @return string binary representation of file, base64'd
389
+	 */
390
+	protected function buildAttachmentContent($attachment)
391
+	{
392
+		if (!file_exists($attachment['path'])) {
393
+			return ''; // todo log error
394
+		}
395
+
396
+		$handle = fopen($attachment['path'], 'r');
397
+		$contents = fread($handle, filesize($attachment['path']));
398
+		fclose($handle);
399
+
400
+		$contents = base64_encode($contents);
401
+		$contents = chunk_split($contents);
402
+		return $contents;
403
+	}
404
+
405
+	/**
406
+	 * Holder for the boundry logic
407
+	 * Not called/created unless it's needed
408
+	 *
409
+	 * @return  string  boundary
410
+	 */
411
+	protected function getBoundary()
412
+	{
413
+		if (!isset($this->boundary)) {
414
+			$this->boundary = sprintf('PHP-mixed-%s', uniqid());
415
+		}
416
+		return $this->boundary;
417
+	}
418
+
419
+	/**
420
+	 * Holder to create the alternative boundry logic
421
+	 * Not called/created unless it's needed
422
+	 *
423
+	 * @return string alternative boundary
424
+	 */
425
+	protected function getAlternativeBoundary()
426
+	{
427
+		if (!isset($this->alternativeBoundary)) {
428
+			$this->alternativeBoundary = sprintf('PHP-alternative-%s', uniqid());
429
+		}
430
+		return $this->alternativeBoundary;
431
+	}
433 432
 }
Please login to merge, or discard this patch.
tests/ArchangelTest.php 1 patch
Indentation   +244 added lines, -244 removed lines patch added patch discarded remove patch
@@ -8,249 +8,249 @@
 block discarded – undo
8 8
 class ArchangelTest extends PHPUnit_Framework_TestCase
9 9
 {
10 10
 
11
-    public function testIsInstanceOfArchangel()
12
-    {
13
-        $archangel = new Archangel();
14
-
15
-        $this->assertInstanceOf('Jacobemerick\Archangel\Archangel', $archangel);
16
-    }
11
+	public function testIsInstanceOfArchangel()
12
+	{
13
+		$archangel = new Archangel();
14
+
15
+		$this->assertInstanceOf('Jacobemerick\Archangel\Archangel', $archangel);
16
+	}
17 17
 
18
-    public function testIsLoggerAwareInterface()
19
-    {
20
-        $archangel = new Archangel();
21
-
22
-        $this->assertInstanceOf('Psr\Log\LoggerAwareInterface', $archangel);
23
-    }
24
-
25
-    public function testConstructSetsDefaultMailer()
26
-    {
27
-        $archangel = new Archangel();
28
-        $mailer = sprintf('PHP/%s', phpversion());
29
-        $headers = array('X-Mailer' => $mailer);
30
-
31
-        $this->assertAttributeEquals($headers, 'headers', $archangel);
32
-    }
33
-
34
-    public function testConstructOverridesMailer()
35
-    {
36
-        $archangel = new Archangel('AwesomeMailer');
37
-        $headers = array('X-Mailer' => 'AwesomeMailer');
38
-
39
-        $this->assertAttributeEquals($headers, 'headers', $archangel);
40
-    }
41
-
42
-    public function testConstructSetsNullLogger()
43
-    {
44
-        $archangel = new Archangel();
45
-
46
-        $this->assertAttributeInstanceOf('Psr\Log\NullLogger', 'logger', $archangel);
47
-    }
48
-
49
-    public function testSetLogger()
50
-    {
51
-        $logger = $this->getMock('Psr\Log\LoggerInterface');
52
-        $archangel = new Archangel();
53
-        $archangel->setLogger($logger);
54
-
55
-        $this->assertAttributeSame($logger, 'logger', $archangel);
56
-    }
57
-
58
-    public function testAddTo()
59
-    {
60
-        $archangel = new Archangel();
61
-        $archangel->addTo('[email protected]');
62
-
63
-        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
64
-    }
65
-
66
-    public function testAddToMultiple()
67
-    {
68
-        $archangel = new Archangel();
69
-        $archangel->addTo('[email protected]');
70
-        $archangel->addTo('[email protected]');
71
-
72
-        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
73
-        $this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
74
-    }
75
-
76
-    public function testAddToWithTitle()
77
-    {
78
-        $archangel = new Archangel();
79
-        $archangel->addTo('[email protected]', 'Mr. Test Alot');
80
-
81
-        $this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'toAddresses', $archangel);
82
-    }
83
-
84
-    public function testAddCc()
85
-    {
86
-        $archangel = new Archangel();
87
-        $archangel->addCc('[email protected]');
88
-
89
-        $this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
90
-    }
91
-
92
-    public function testAddCcMultiple()
93
-    {
94
-        $archangel = new Archangel();
95
-        $archangel->addCc('[email protected]');
96
-        $archangel->addCc('[email protected]');
97
-
98
-        $this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
99
-        $this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
100
-    }
101
-
102
-    public function testAddCcWithTitle()
103
-    {
104
-        $archangel = new Archangel();
105
-        $archangel->addCc('[email protected]', 'Mr. Test Alot');
106
-
107
-        $this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'ccAddresses', $archangel);
108
-    }
109
-
110
-    public function testAddBcc()
111
-    {
112
-        $archangel = new Archangel();
113
-        $archangel->addBcc('[email protected]');
114
-
115
-        $this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
116
-    }
117
-
118
-    public function testAddBccMultiple()
119
-    {
120
-        $archangel = new Archangel();
121
-        $archangel->addBcc('[email protected]');
122
-        $archangel->addBcc('[email protected]');
123
-
124
-        $this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
125
-        $this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
126
-    }
127
-
128
-    public function testAddBccWithTitle()
129
-    {
130
-        $archangel = new Archangel();
131
-        $archangel->addBcc('[email protected]', 'Mr. Test Alot');
132
-
133
-        $this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'bccAddresses', $archangel);
134
-    }
135
-
136
-    public function testSetFrom()
137
-    {
138
-        $archangel = new Archangel();
139
-        $archangel->setFrom('[email protected]');
140
-        $setHeaders = $this->getProtectedValue($archangel, 'headers');
141
-
142
-        $this->assertArraySubset(array('From' => '[email protected]'), $setHeaders);
143
-    }
144
-
145
-    public function testSetFromMultiple()
146
-    {
147
-        $archangel = new Archangel();
148
-        $archangel->setFrom('[email protected]');
149
-        $archangel->setFrom('[email protected]');
150
-        $setHeaders = $this->getProtectedValue($archangel, 'headers');
151
-
152
-        $this->assertArraySubset(array('From' => '[email protected]'), $setHeaders);
153
-        $this->assertNotContains('[email protected]', $setHeaders);
154
-    }
155
-
156
-    public function testSetFromWithTitle()
157
-    {
158
-        $archangel = new Archangel();
159
-        $archangel->setFrom('[email protected]', 'Mr. Test Alot');
160
-        $setHeaders = $this->getProtectedValue($archangel, 'headers');
161
-
162
-        $this->assertArraySubset(array('From' => '"Mr. Test Alot" <[email protected]>'), $setHeaders);
163
-    }
164
-
165
-    public function testSetReplyTo()
166
-    {
167
-        $archangel = new Archangel();
168
-        $archangel->setReplyTo('[email protected]');
169
-        $setHeaders = $this->getProtectedValue($archangel, 'headers');
170
-
171
-        $this->assertArraySubset(array('Reply-To' => '[email protected]'), $setHeaders);
172
-    }
173
-
174
-    public function testSetReplyToMultiple()
175
-    {
176
-        $archangel = new Archangel();
177
-        $archangel->setReplyTo('[email protected]');
178
-        $archangel->setReplyTo('[email protected]');
179
-        $setHeaders = $this->getProtectedValue($archangel, 'headers');
180
-
181
-        $this->assertArraySubset(array('Reply-To' => '[email protected]'), $setHeaders);
182
-        $this->assertNotContains('[email protected]', $setHeaders);
183
-    }
184
-
185
-    public function testSetReplyToWithTitle()
186
-    {
187
-        $archangel = new Archangel();
188
-        $archangel->setReplyTo('[email protected]', 'Mr. Test Alot');
189
-        $setHeaders = $this->getProtectedValue($archangel, 'headers');
190
-
191
-        $this->assertArraySubset(array('Reply-To' => '"Mr. Test Alot" <[email protected]>'), $setHeaders);
192
-    }
193
-
194
-    public function testSetSubject()
195
-    {
196
-        $archangel = new Archangel();
197
-        $archangel->setSubject('Test Subject');
198
-        $setSubject = $this->getProtectedValue($archangel, 'subject');
199
-
200
-        $this->assertEquals('Test Subject', $setSubject);
201
-    }
202
-
203
-    public function testSetPlainMessage()
204
-    {
205
-        $archangel = new Archangel();
206
-        $archangel->setPlainMessage('Plain text message');
207
-        $setPlainMessage = $this->getProtectedValue($archangel, 'plainMessage');
208
-
209
-        $this->assertEquals('Plain text message', $setPlainMessage);
210
-    }
211
-
212
-    public function testSetHTMLMessage()
213
-    {
214
-        $archangel = new Archangel();
215
-        $archangel->setHTMLMessage('<p>An HTML message.</p>');
216
-        $setHTMLMessage = $this->getProtectedValue($archangel, 'htmlMessage');
217
-
218
-        $this->assertEquals('<p>An HTML message.</p>', $setHTMLMessage);
219
-    }
220
-
221
-    public function testFormatEmailAddress()
222
-    {
223
-        $archangel = new Archangel();
224
-        $formatMethod = $this->getProtectedMethod('formatEmailAddress');
225
-        $formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', ''));
226
-
227
-        $this->assertEquals('[email protected]', $formattedEmail);
228
-    }
229
-
230
-    public function testFormatEmailAddressWithTitle()
231
-    {
232
-        $archangel = new Archangel();
233
-        $formatMethod = $this->getProtectedMethod('formatEmailAddress');
234
-        $formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', 'Mr. Test Alot'));
235
-
236
-        $this->assertEquals('"Mr. Test Alot" <[email protected]>', $formattedEmail);
237
-    }
238
-
239
-    protected function getProtectedValue($archangel, $property)
240
-    {
241
-        $reflectedArchangel = new ReflectionClass($archangel);
242
-        $reflectedProperty = $reflectedArchangel->getProperty($property);
243
-        $reflectedProperty->setAccessible(true);
244
-
245
-        return $reflectedProperty->getValue($archangel);
246
-    }
247
-
248
-    protected function getProtectedMethod($method)
249
-    {
250
-        $reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
251
-        $reflectedMethod = $reflectedArchangel->getMethod($method);
252
-        $reflectedMethod->setAccessible(true);
253
-
254
-        return $reflectedMethod;
255
-    }
18
+	public function testIsLoggerAwareInterface()
19
+	{
20
+		$archangel = new Archangel();
21
+
22
+		$this->assertInstanceOf('Psr\Log\LoggerAwareInterface', $archangel);
23
+	}
24
+
25
+	public function testConstructSetsDefaultMailer()
26
+	{
27
+		$archangel = new Archangel();
28
+		$mailer = sprintf('PHP/%s', phpversion());
29
+		$headers = array('X-Mailer' => $mailer);
30
+
31
+		$this->assertAttributeEquals($headers, 'headers', $archangel);
32
+	}
33
+
34
+	public function testConstructOverridesMailer()
35
+	{
36
+		$archangel = new Archangel('AwesomeMailer');
37
+		$headers = array('X-Mailer' => 'AwesomeMailer');
38
+
39
+		$this->assertAttributeEquals($headers, 'headers', $archangel);
40
+	}
41
+
42
+	public function testConstructSetsNullLogger()
43
+	{
44
+		$archangel = new Archangel();
45
+
46
+		$this->assertAttributeInstanceOf('Psr\Log\NullLogger', 'logger', $archangel);
47
+	}
48
+
49
+	public function testSetLogger()
50
+	{
51
+		$logger = $this->getMock('Psr\Log\LoggerInterface');
52
+		$archangel = new Archangel();
53
+		$archangel->setLogger($logger);
54
+
55
+		$this->assertAttributeSame($logger, 'logger', $archangel);
56
+	}
57
+
58
+	public function testAddTo()
59
+	{
60
+		$archangel = new Archangel();
61
+		$archangel->addTo('[email protected]');
62
+
63
+		$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
64
+	}
65
+
66
+	public function testAddToMultiple()
67
+	{
68
+		$archangel = new Archangel();
69
+		$archangel->addTo('[email protected]');
70
+		$archangel->addTo('[email protected]');
71
+
72
+		$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
73
+		$this->assertAttributeContains('[email protected]', 'toAddresses', $archangel);
74
+	}
75
+
76
+	public function testAddToWithTitle()
77
+	{
78
+		$archangel = new Archangel();
79
+		$archangel->addTo('[email protected]', 'Mr. Test Alot');
80
+
81
+		$this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'toAddresses', $archangel);
82
+	}
83
+
84
+	public function testAddCc()
85
+	{
86
+		$archangel = new Archangel();
87
+		$archangel->addCc('[email protected]');
88
+
89
+		$this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
90
+	}
91
+
92
+	public function testAddCcMultiple()
93
+	{
94
+		$archangel = new Archangel();
95
+		$archangel->addCc('[email protected]');
96
+		$archangel->addCc('[email protected]');
97
+
98
+		$this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
99
+		$this->assertAttributeContains('[email protected]', 'ccAddresses', $archangel);
100
+	}
101
+
102
+	public function testAddCcWithTitle()
103
+	{
104
+		$archangel = new Archangel();
105
+		$archangel->addCc('[email protected]', 'Mr. Test Alot');
106
+
107
+		$this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'ccAddresses', $archangel);
108
+	}
109
+
110
+	public function testAddBcc()
111
+	{
112
+		$archangel = new Archangel();
113
+		$archangel->addBcc('[email protected]');
114
+
115
+		$this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
116
+	}
117
+
118
+	public function testAddBccMultiple()
119
+	{
120
+		$archangel = new Archangel();
121
+		$archangel->addBcc('[email protected]');
122
+		$archangel->addBcc('[email protected]');
123
+
124
+		$this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
125
+		$this->assertAttributeContains('[email protected]', 'bccAddresses', $archangel);
126
+	}
127
+
128
+	public function testAddBccWithTitle()
129
+	{
130
+		$archangel = new Archangel();
131
+		$archangel->addBcc('[email protected]', 'Mr. Test Alot');
132
+
133
+		$this->assertAttributeContains('"Mr. Test Alot" <[email protected]>', 'bccAddresses', $archangel);
134
+	}
135
+
136
+	public function testSetFrom()
137
+	{
138
+		$archangel = new Archangel();
139
+		$archangel->setFrom('[email protected]');
140
+		$setHeaders = $this->getProtectedValue($archangel, 'headers');
141
+
142
+		$this->assertArraySubset(array('From' => '[email protected]'), $setHeaders);
143
+	}
144
+
145
+	public function testSetFromMultiple()
146
+	{
147
+		$archangel = new Archangel();
148
+		$archangel->setFrom('[email protected]');
149
+		$archangel->setFrom('[email protected]');
150
+		$setHeaders = $this->getProtectedValue($archangel, 'headers');
151
+
152
+		$this->assertArraySubset(array('From' => '[email protected]'), $setHeaders);
153
+		$this->assertNotContains('[email protected]', $setHeaders);
154
+	}
155
+
156
+	public function testSetFromWithTitle()
157
+	{
158
+		$archangel = new Archangel();
159
+		$archangel->setFrom('[email protected]', 'Mr. Test Alot');
160
+		$setHeaders = $this->getProtectedValue($archangel, 'headers');
161
+
162
+		$this->assertArraySubset(array('From' => '"Mr. Test Alot" <[email protected]>'), $setHeaders);
163
+	}
164
+
165
+	public function testSetReplyTo()
166
+	{
167
+		$archangel = new Archangel();
168
+		$archangel->setReplyTo('[email protected]');
169
+		$setHeaders = $this->getProtectedValue($archangel, 'headers');
170
+
171
+		$this->assertArraySubset(array('Reply-To' => '[email protected]'), $setHeaders);
172
+	}
173
+
174
+	public function testSetReplyToMultiple()
175
+	{
176
+		$archangel = new Archangel();
177
+		$archangel->setReplyTo('[email protected]');
178
+		$archangel->setReplyTo('[email protected]');
179
+		$setHeaders = $this->getProtectedValue($archangel, 'headers');
180
+
181
+		$this->assertArraySubset(array('Reply-To' => '[email protected]'), $setHeaders);
182
+		$this->assertNotContains('[email protected]', $setHeaders);
183
+	}
184
+
185
+	public function testSetReplyToWithTitle()
186
+	{
187
+		$archangel = new Archangel();
188
+		$archangel->setReplyTo('[email protected]', 'Mr. Test Alot');
189
+		$setHeaders = $this->getProtectedValue($archangel, 'headers');
190
+
191
+		$this->assertArraySubset(array('Reply-To' => '"Mr. Test Alot" <[email protected]>'), $setHeaders);
192
+	}
193
+
194
+	public function testSetSubject()
195
+	{
196
+		$archangel = new Archangel();
197
+		$archangel->setSubject('Test Subject');
198
+		$setSubject = $this->getProtectedValue($archangel, 'subject');
199
+
200
+		$this->assertEquals('Test Subject', $setSubject);
201
+	}
202
+
203
+	public function testSetPlainMessage()
204
+	{
205
+		$archangel = new Archangel();
206
+		$archangel->setPlainMessage('Plain text message');
207
+		$setPlainMessage = $this->getProtectedValue($archangel, 'plainMessage');
208
+
209
+		$this->assertEquals('Plain text message', $setPlainMessage);
210
+	}
211
+
212
+	public function testSetHTMLMessage()
213
+	{
214
+		$archangel = new Archangel();
215
+		$archangel->setHTMLMessage('<p>An HTML message.</p>');
216
+		$setHTMLMessage = $this->getProtectedValue($archangel, 'htmlMessage');
217
+
218
+		$this->assertEquals('<p>An HTML message.</p>', $setHTMLMessage);
219
+	}
220
+
221
+	public function testFormatEmailAddress()
222
+	{
223
+		$archangel = new Archangel();
224
+		$formatMethod = $this->getProtectedMethod('formatEmailAddress');
225
+		$formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', ''));
226
+
227
+		$this->assertEquals('[email protected]', $formattedEmail);
228
+	}
229
+
230
+	public function testFormatEmailAddressWithTitle()
231
+	{
232
+		$archangel = new Archangel();
233
+		$formatMethod = $this->getProtectedMethod('formatEmailAddress');
234
+		$formattedEmail = $formatMethod->invokeArgs($archangel, array('[email protected]', 'Mr. Test Alot'));
235
+
236
+		$this->assertEquals('"Mr. Test Alot" <[email protected]>', $formattedEmail);
237
+	}
238
+
239
+	protected function getProtectedValue($archangel, $property)
240
+	{
241
+		$reflectedArchangel = new ReflectionClass($archangel);
242
+		$reflectedProperty = $reflectedArchangel->getProperty($property);
243
+		$reflectedProperty->setAccessible(true);
244
+
245
+		return $reflectedProperty->getValue($archangel);
246
+	}
247
+
248
+	protected function getProtectedMethod($method)
249
+	{
250
+		$reflectedArchangel = new ReflectionClass('Jacobemerick\Archangel\Archangel');
251
+		$reflectedMethod = $reflectedArchangel->getMethod($method);
252
+		$reflectedMethod->setAccessible(true);
253
+
254
+		return $reflectedMethod;
255
+	}
256 256
 }
Please login to merge, or discard this patch.