Passed
Push — main ( d9e048...73c905 )
by Rafael N.
02:21
created

Email::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 19
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace RafaNSilva\Support;
5
6
use PHPMailer\PHPMailer\PHPMailer;
7
use PHPMailer\PHPMailer\Exception;
8
9
/**
10
 * Class Email
11
 *
12
 * @author Rafael N. Silva
13
 * @package RafaNSilva\Support
14
 */
15
class Email
16
{
17
    /** @var \stdClass */
18
    private $data;
19
20
    /** @var PHPMailer */
21
    private $mail;
22
23
    /** @var Exception */
24
    private $error;
25
26
27
    /**
28
     * Email constructor.
29
     */
30
    public function __construct()
31
    {
32
        $this->data = new \stdClass();
33
        $this->mail = new PHPMailer(true);
34
35
        //setup
36
        $this->mail->SMTPDebug = CONF_MAIL_OPTION_DEBUG;
37
        $this->mail->isSMTP();
38
        $this->mail->setLanguage(CONF_MAIL_OPTION_LANG);
39
        $this->mail->isHTML(CONF_MAIL_OPTION_HTML);
40
        $this->mail->SMTPAuth = CONF_MAIL_OPTION_AUTH;
41
        $this->mail->SMTPSecure = CONF_MAIL_OPTION_SECURE;
42
        $this->mail->CharSet = CONF_MAIL_OPTION_CHARSET;
43
44
        //auth
45
        $this->mail->Host = CONF_MAIL_HOST;
46
        $this->mail->Port = CONF_MAIL_PORT;
47
        $this->mail->Username = CONF_MAIL_USER;
48
        $this->mail->Password = CONF_MAIL_PASS;
49
    }
50
51
    /**
52
     * @param string $subject
53
     * @param string $body
54
     * @param string $recipientMail
55
     * @param string $recipientName
56
     * @return $this
57
     */
58
    public function bootstrap(string $subject, string $body, string $recipientMail, string $recipientName): Email
59
    {
60
        $this->data->subject = $subject;
61
        $this->data->body = $body;
62
        $this->data->recipientMail = $recipientMail;
63
        $this->data->recipientName = $recipientName;
64
        return $this;
65
    }
66
67
68
    /**
69
     * @param string $filePath
70
     * @param string $fileName
71
     * @return $this
72
     */
73
    public function attach(string $filePath, string $fileName): Email
74
    {
75
        $this->data->attach[$filePath] = $fileName;
76
        return $this;
77
    }
78
79
80
    /**
81
     * @param string $from
82
     * @param string $fromName
83
     * @return bool
84
     * @throws \Exception
85
     */
86
    public function send(string $from = CONF_MAIL_SENDER["address"], string $fromName = CONF_MAIL_SENDER["name"]): bool
87
    {
88
        try {
89
            if (empty($this->data)) {
90
                throw new Exception("Error sending, please check the data");
91
            }
92
93
            if (!filter_var($this->data->recipientMail, FILTER_VALIDATE_EMAIL)) {
94
                throw new Exception("Recipient email is not valid");
95
            }
96
97
            if (!filter_var($from, FILTER_VALIDATE_EMAIL)) {
98
                throw new Exception("The sender email is not valid");
99
            }
100
101
            $this->mail->Subject = $this->data->subject;
102
            $this->mail->msgHTML($this->data->body);
103
            $this->mail->addAddress($this->data->recipientMail, $this->data->recipientName);
104
            $this->mail->setFrom($from, $fromName);
105
106
            if (!empty($this->data->attach)) {
107
                foreach ($this->data->attach as $path => $name) {
108
                    $this->mail->addAttachment($path, $name);
109
                }
110
            }
111
112
            $this->mail->send();
113
            return true;
114
        } catch (Exception $exception) {
115
            $this->error = $exception;
116
            return false;
117
        }
118
    }
119
120
    /**
121
     * @return PHPMailer
122
     */
123
    public function mail(): PHPMailer
124
    {
125
        return $this->mail;
126
    }
127
128
    /**
129
     * @return Exception|null
130
     */
131
    public function error(): ?Exception
132
    {
133
        return $this->error;
134
    }
135
}