Completed
Push — master ( cae729...2dbb1e )
by Nazar
04:23
created

Mail::normalize_body()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 6
nop 2
dl 0
loc 24
ccs 13
cts 13
cp 1
crap 4
rs 8.6845
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
use
10
	h,
11
	PHPMailer,
12
	phpmailerException;
13
14
/**
15
 * @method static $this instance($check = false)
16
 */
17
class Mail {
18
	use Singleton;
19
	/**
20
	 * Sending of email
21
	 *
22
	 * @param array|string|string[]      $email       if emails without names - string (may be several emails separated by comma) or
23
	 *                                                1-dimensional array(<i>email</i>)<br>
24
	 *                                                2-dimensional of emails or array(<i>email</i>, <i>name</i>) must be given
25
	 * @param string                     $subject     Mail subject
26
	 * @param string                     $body        html body
27
	 * @param string|null                $body_text   plain text body
28
	 * @param array|null|string          $attachments 1- or 2-dimensional array of array(<i>path</i>, <i>name</i>) or simply string with path to the file in
29
	 *                                                file system
30
	 * @param array|null|string|string[] $reply_to    Similar to <b>$email</b>, but multiple emails are not supported
31
	 * @param bool|string                $signature   <b>true</b> - add system signature<br>
32
	 *                                                <b>false</b> - without signature<br>
33
	 *                                                <b>string</b> - custom signature
34
	 *
35
	 * @return bool
36
	 */
37 2
	public function send_to ($email, $subject, $body, $body_text = null, $attachments = null, $reply_to = null, $signature = true) {
38 2
		if (!$email || !$subject || !$body) {
39 2
			return false;
40
		}
41 2
		$Config    = Config::instance();
42 2
		$PHPMailer = $this->phpmailer_instance($Config);
43 2
		$this->add_email($email, [$PHPMailer, 'addAddress']);
44 2
		$this->add_email($reply_to, [$PHPMailer, 'addReplyTo']);
45
		try {
46 2
			foreach ($this->normalize_attachment($attachments) as $a) {
47 2
				$PHPMailer->addAttachment(...$a);
48
			}
49 2
			$PHPMailer->Subject = $subject;
50 2
			$signature          = $this->make_signature($Config, $signature);
51 2
			$PHPMailer->Body    = $this->normalize_body($body, $signature);
52 2
			if ($body_text) {
53 2
				$PHPMailer->AltBody = $body_text.strip_tags($signature);
54
			}
55 2
			return $PHPMailer->send();
56 2
		} catch (phpmailerException $e) {
57 2
			trigger_error($e->getMessage(), E_USER_WARNING);
58
		}
59 2
		return false;
60
	}
61
	/**
62
	 * Create PHPMailer instance with parameters according to system configuration
63
	 *
64
	 * @return PHPMailer
65
	 */
66 2
	protected function phpmailer_instance ($Config) {
67 2
		$PHPMailer = new PHPMailer(true);
68 2
		if ($Config->core['smtp']) {
69 2
			$PHPMailer->isSMTP();
70 2
			$PHPMailer->Host       = $Config->core['smtp_host'];
71 2
			$PHPMailer->Port       = $Config->core['smtp_port'];
72 2
			$PHPMailer->SMTPSecure = $Config->core['smtp_secure'];
73 2
			if ($Config->core['smtp_auth']) {
74 2
				$PHPMailer->SMTPAuth = true;
75 2
				$PHPMailer->Username = $Config->core['smtp_user'];
76 2
				$PHPMailer->Password = $Config->core['smtp_password'];
77
			}
78
		}
79 2
		$PHPMailer->From     = $Config->core['mail_from'];
80 2
		$PHPMailer->FromName = $Config->core['mail_from_name'];
81 2
		$PHPMailer->CharSet  = 'utf-8';
82 2
		$PHPMailer->isHTML();
83 2
		return $PHPMailer;
84
	}
85
	/**
86
	 * @param array|null|string|string[] $email
87
	 * @param callable                   $callback
88
	 */
89 2
	protected function add_email ($email, callable $callback) {
90 2
		foreach ($this->normalize_email($email) as $e) {
91 2
			$callback(...$e);
92
		}
93 2
	}
94
	/**
95
	 * @param array|null|string|string[] $email
96
	 *
97
	 * @return string[][]
98
	 */
99 2
	protected function normalize_email ($email) {
100 2
		if (!$email) {
101 2
			return [];
102
		}
103 2
		if (!is_array($email)) {
104 2
			$email = _trim(explode(',', $email));
105 2
		} elseif (!is_array($email[0])) {
106 2
			$email = [$email];
107
		}
108 2
		return _array($email);
109
	}
110
	/**
111
	 * @param Config      $Config
112
	 * @param bool|string $signature
113
	 *
114
	 * @return string
115
	 */
116 2
	protected function make_signature ($Config, $signature) {
117 2
		if ($signature === true) {
118 2
			$signature = $Config->core['mail_signature'];
119 2
			return $signature ? "<br>\n<br>\n-- \n<br>$signature" : '';
120
		}
121 2
		return $signature ? "<br>\n<br>\n-- \n<br>".xap($signature, true) : '';
122
	}
123
	/**
124
	 * @param string $body
125
	 * @param string $signature
126
	 *
127
	 * @return string
128
	 */
129 2
	protected function normalize_body ($body, $signature) {
130 2
		if (strpos($body, '<html') === false) {
131 2
			if (strpos($body, '<body') === false) {
132 2
				$body = h::body($body.$signature);
133
			} else {
134 2
				$body = str_replace('</body>', "$signature</body>", $body);
135
			}
136 2
			$body = h::html(
137 2
				h::{'head meta'}(
138
					[
139 2
						'content'    => 'text/html; charset=utf-8',
140
						'http-equiv' => 'Content-Type'
141
					]
142
				).
143 2
				$body
144
			);
145
		} else {
146 2
			$body = str_replace('</body>', "$signature</body>", $body);
147
		}
148 2
		if (strpos($body, '<!doctype') === false) {
149 2
			$body = "<!doctype html>\n$body";
150
		}
151 2
		return $body;
152
	}
153
	/**
154
	 * @param array|null|string $attachments
155
	 *
156
	 * @return array
157
	 */
158 2
	protected function normalize_attachment ($attachments) {
159 2
		if (!$attachments) {
160 2
			return [];
161
		}
162 2
		if (!is_array($attachments) || !is_array($attachments[0])) {
163 2
			$attachments = [$attachments];
164
		}
165 2
		return _array($attachments);
166
	}
167
}
168