Completed
Push — master ( 758d43...d0ef0e )
by Nazar
04:37
created

Mail::make_signature()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.2
cc 4
eloc 5
nc 4
nop 1
1
<?php
2
/**
3
 * @package   CleverStyle CMS
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
13
/**
14
 * @method static $this instance($check = false)
15
 */
16
class Mail extends PHPMailer {
17
	use Singleton;
18
	/**
19
	 * Setting base mail sending parameters according to system configuration
20
	 */
21
	function construct () {
22
		$Config = Config::instance();
23
		if ($Config->core['smtp']) {
24
			$this->isSMTP();
25
			$this->Host = $Config->core['smtp_host'];
26
			/** @noinspection NestedTernaryOperatorInspection */
27
			$this->Port       = $Config->core['smtp_port'] ?: ($Config->core['smtp_secure'] ? 465 : 25);
28
			$this->SMTPSecure = $Config->core['smtp_secure'];
29
			if ($Config->core['smtp_auth']) {
30
				$this->SMTPAuth = true;
31
				$this->Username = $Config->core['smtp_user'];
32
				$this->Password = $Config->core['smtp_password'];
33
			}
34
		}
35
		$this->From     = $Config->core['mail_from'];
36
		$this->FromName = get_core_ml_text('mail_from_name');
37
		$this->CharSet  = 'utf-8';
38
		$this->isHTML();
39
	}
40
	/**
41
	 * Sending of email
42
	 *
43
	 * @param array|string|string[]      $email       if emails without names - string (may be several emails separated by comma) or
44
	 *                                                1-dimensional array(<i>email</i>)<br>
45
	 *                                                2-dimensional of emails or array(<i>email</i>, <i>name</i>) must be given
46
	 * @param string                     $subject     Mail subject
47
	 * @param string                     $body        html body
48
	 * @param string|null                $body_text   plain text body
49
	 * @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
50
	 *                                                file system
51
	 * @param array|null|string|string[] $reply_to    Similar to <b>$email</b>, but multiple emails are not supported
52
	 * @param bool|string                $signature   <b>true</b> - add system signature<br>
53
	 *                                                <b>false</b> - without signature<br>
54
	 *                                                <b>string</b> - custom signature
55
	 *
56
	 * @return bool
57
	 */
58
	function send_to ($email, $subject, $body, $body_text = null, $attachments = null, $reply_to = null, $signature = true) {
59
		if (!$email || !$subject || !$body) {
60
			return false;
61
		}
62
		foreach ($this->normalize_email($email) as $e) {
63
			$this->addAddress(...$e);
64
		}
65
		foreach ($this->normalize_email($reply_to) as $r) {
66
			$this->addReplyTo(...$r);
67
		}
68
		foreach ($this->normalize_attachment($attachments) as $a) {
69
			try {
70
				$this->addAttachment(...$a);
71
			} catch (\phpmailerException $e) {
0 ignored issues
show
Bug introduced by
The class phpmailerException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
72
				trigger_error($e->getMessage(), E_USER_WARNING);
73
			}
74
		}
75
		$this->Subject = $subject;
76
		$signature     = $this->make_signature($signature);
77
		$this->Body    = $this->normalize_body($body, $signature);
78
		if ($body_text) {
79
			$this->AltBody = $body_text.strip_tags($signature);
80
		}
81
		try {
82
			$result = $this->send();
83
		} catch (\phpmailerException $e) {
0 ignored issues
show
Bug introduced by
The class phpmailerException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
84
			trigger_error($e->getMessage(), E_USER_WARNING);
85
			$result = false;
86
		}
87
		$this->clearAddresses();
88
		$this->clearAttachments();
89
		$this->clearReplyTos();
90
		return $result;
91
	}
92
	/**
93
	 * @param array|string|string[] $email
94
	 *
95
	 * @return string[][]
96
	 */
97
	protected function normalize_email ($email) {
98
		if (!$email) {
99
			return [];
100
		}
101
		if (!is_array($email)) {
102
			$email = _trim(explode(',', $email));
103
		} elseif (!is_array($email[0])) {
104
			$email = [$email];
105
		}
106
		return _array($email);
107
	}
108
	protected function make_signature ($signature) {
109
		if ($signature === true) {
110
			$signature = get_core_ml_text('mail_signature');
111
			return $signature ? "<br>$this->LE<br>$this->LE-- $this->LE<br>$signature" : '';
112
		}
113
		return $signature ? "<br>$this->LE<br>$this->LE-- $this->LE<br>".xap($signature, true) : '';
114
	}
115
	/**
116
	 * @param string $body
117
	 * @param string $signature
118
	 *
119
	 * @return string
120
	 */
121
	protected function normalize_body ($body, $signature) {
122
		if (strpos($body, '<!doctype') === 0 && strpos($body, '<body') !== false) {
123
			$body = "<!doctype html>\n$body";
124
		}
125
		if (strpos($body, '<html') === false) {
126
			if (strpos($body, '<body') === false) {
127
				$body = h::body($body.$signature);
128
			} else {
129
				$body = str_replace('</body>', "$signature</body>", $body);
130
			}
131
			$body = h::html(
132
				h::{'head meta'}(
133
					[
134
						'content'    => 'text/html; charset=utf-8',
135
						'http-equiv' => 'Content-Type'
136
					]
137
				).
138
				$body
139
			);
140
		} else {
141
			$body = str_replace('</body>', "$signature</body>", $body);
142
		}
143
		return $body;
144
	}
145
	/**
146
	 * @param array|null|string $attachments
147
	 *
148
	 * @return array
149
	 */
150
	protected function normalize_attachment ($attachments) {
151
		if (!$attachments) {
152
			return [];
153
		}
154
		if (!is_array($attachments) || !is_array($attachments[0])) {
155
			$attachments = [$attachments];
156
		}
157
		return _array($attachments);
158
	}
159
}
160