Email   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 145
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A sendMail() 0 14 2
A send() 0 10 2
A sendNotification() 0 17 2
1
<?php
2
3
namespace Asymptix\mail;
4
5
use Asymptix\core\Validator;
6
use Asymptix\core\Content;
7
8
/**
9
 * Mail send functionality wrapper.
10
 *
11
 * @category Asymptix PHP Framework
12
 * @author Dmytro Zarezenko <[email protected]>
13
 * @copyright (c) 2010 - 2016, Dmytro Zarezenko
14
 *
15
 * @git https://github.com/Asymptix/Framework
16
 * @license http://opensource.org/licenses/MIT
17
 */
18
class Email {
19
20
    /**
21
     * Mail format constants.
22
     */
23
    const FORMAT_TEXT = "text";
24
    const FORMAT_HTML = "html";
25
26
    /**
27
     * From e-mail address.
28
     *
29
     * @var string
30
     */
31
    private $fromEmail;
32
33
    /**
34
     * From name.
35
     *
36
     * @var string
37
     */
38
    private $fromName;
39
40
    /**
41
     * Absolute path to the e-mail templates folder with ending '/'.
42
     *
43
     * @var string
44
     */
45
    private $tplFolder;
46
47
    /**
48
     * Filename of the e-mail signature template (must be in e-mail templates folder).
49
     *
50
     * @var string
51
     */
52
    private $signatureTpl;
53
54
    /**
55
     * Email class constructor. Initialize basic e-mail send functionality
56
     * variables and validate from e-mail address.
57
     *
58
     * @param string $fromEmail From e-mail address.
59
     * @param string $fromName From name.
60
     * @param string $tplFolder Absolute path to the e-mail templates folder
61
     *           with ending '/'.
62
     * @param string $signatureTpl Filename of the e-mail signature template
63
     *           (must be in e-mail templates folder).
64
     *
65
     * @throws Exception If provided sender e-mail address is invalid.
66
     */
67
    public function __construct($fromEmail, $fromName, $tplFolder, $signatureTpl = "") {
68
        if (!Validator::isEmail($fromEmail)) {
69
            throw new EmailException("Invalid from e-mail address");
70
        }
71
72
        $this->fromEmail = $fromEmail;
73
        $this->fromName = $fromName;
74
        $this->tplFolder = $tplFolder;
75
        $this->signatureTpl = $signatureTpl;
76
    }
77
78
    /**
79
     * Sends mail.
80
     *
81
     * @param string $email E-mail of the reciver.
82
     * @param string $subject Subject of the email to be sent.
83
     * @param string $message Text of the mail.
84
     * @param string $format E-mail format ("text" or "html").
85
     * @param string $replyTo Reply to e-mail address.
86
     *
87
     * @return bool Returns TRUE if the mail was successfully accepted for
88
     *           delivery, FALSE otherwise.
89
     */
90
    protected function sendMail($email, $subject, $message, $format = self::FORMAT_TEXT, $replyTo = "") {
91
        $headers = "From: " . $this->fromName . " <" . $this->fromEmail . ">\r\n";
92
        $headers.= "Reply-To: " . $replyTo . "\r\n";
93
94
        if ($format == self::FORMAT_HTML) {
95
            $headers.= "MIME-Version: 1.0\r\n";
96
            $headers.= "Content-type: text/html; charset=utf-8\r\n";
97
        }
98
99
        /*
100
         * Send generated message
101
         */
102
        return mail($email, $subject, $message, $headers);
103
    }
104
105
    /**
106
     * Send mail with standard PHP mail() function.
107
     * See more details in the standard PHP mail() function documentation.
108
     *
109
     * @param string $to Receiver, or receivers of the mail.
110
     * @param string $subject Subject of the email to be sent.
111
     * @param mixed $message String message or two elements array:
112
     *           0 - path to the template file
113
     *           1 - template variables
114
     * @param array $headers String to be inserted at the end of the email header.
115
     * @param type $parameters Additional parameters.
116
     *
117
     * @return bool Returns TRUE if the mail was successfully accepted for delivery,
118
     *           FALSE otherwise.
119
     */
120
    public static function send($to, $subject, $message, $headers = [], $parameters = "") {
121
        if (is_array($message)) {
122
            $tplPath = $message[0];
123
            $tplVars = $message[1];
124
125
            $message = Content::render($tplPath, $tplVars);
126
        }
127
128
        return mail($to, $subject, $message, implode("\r\n", $headers), $parameters);
129
    }
130
131
    /**
132
     * Sends specific E-mail notification.
133
     *
134
     * @param string $email E-mail of the reciver.
135
     * @param string $subject Subject of the email to be sent.
136
     * @param string $template Name of the email message template file.
137
     * @param array $params List of the template parameters.
138
     * @param string $format E-mail format ("text" or "html").
139
     * @param string $replyTo Reply to e-mail address.
140
     *
141
     * @return bool Returns TRUE if the mail was successfully accepted for
142
     *           delivery, FALSE otherwise.
143
     */
144
    protected function sendNotification(
145
            $email, $subject, $languageCode, $template,
146
            array $params = null, $format = self::FORMAT_TEXT,
147
            $replyTo = ""
148
        ) {
149
        $_EMAIL = $params;
150
151
        ob_start();
152
        include($this->tplFolder . $languageCode . "/" . $template . ".tpl.php");
153
        if (!empty($this->signatureTpl)) {
154
            include($this->tplFolder . $languageCode . "/" . $this->signatureTpl);
155
        }
156
        $message = ob_get_contents();
157
        ob_end_clean();
158
159
        return self::sendMail($email, $subject, $message, $format, $replyTo);
160
    }
161
162
}
163
164
class EmailException extends \Exception {}
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
165