Passed
Push — master ( c65021...b58a23 )
by Murilo
02:50
created

Email   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 41
dl 0
loc 105
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 4
A clearAddresses() 0 3 1
A setConfig() 0 21 1
A getError() 0 3 1
A SendMail() 0 20 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Source\Domain\Email;
6
7
use PHPMailer\PHPMailer\PHPMailer;
0 ignored issues
show
Bug introduced by
The type PHPMailer\PHPMailer\PHPMailer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use PHPMailer\PHPMailer\Exception;
0 ignored issues
show
Bug introduced by
The type PHPMailer\PHPMailer\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
use Source\Infra\Logger\Log;
11
12
final class Email
13
{
14
    /** @var PHPMailer $mail */
15
    private $mail;
16
17
    /** @var string $error */
18
    private $error;
19
20
    /**
21
     * @param array $MailAddress
22
     * @param bool $html
23
     */
24
    public function __construct(array $MailAddress, bool $html = true)
25
    {
26
        $this->mail = new PHPMailer();
27
28
        // SMTP Server settings
29
        if (CONF_MAIL_OPTION_SMTP === true) {
30
            $this->mail->isSMTP();
31
        }
32
33
        if ($html === true) {
34
            $this->mail->isHTML();
35
        }
36
37
        $this->mail->setLanguage(CONF_MAIL_OPTION_LANG);
38
        $this->mail->CharSet = CONF_MAIL_OPTION_CHARSET;
39
        $this->setConfig();
40
41
        //Recipients
42
        foreach ($MailAddress as $key => $mail) {
43
            $this->mail->addAddress($mail);
44
        }
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getError(): string
51
    {
52
        return $this->error;
53
    }
54
55
    /**
56
     * @return void
57
     */
58
    private function setConfig(): void
59
    {
60
        $this->mail->Host = gethostbyname(CONF_MAIL_HOST);
61
        $this->mail->Port = CONF_MAIL_PORT;
62
        $this->mail->Username = CONF_MAIL_USER;
63
        $this->mail->Password = getenv('MAIL_PASS') ?? '';
64
        $this->mail->SMTPAuth = CONF_MAIL_OPTION_AUTH;
65
        $this->mail->SMTPSecure = CONF_MAIL_OPTION_SECURE;
66
67
        // Debug off
68
        $this->mail->SMTPDebug = 0;
69
70
        // Debug on
71
        // $this->mail->SMTPDebug = 4;
72
73
        $this->mail->setFrom(CONF_MAIL_SENDER['address'], CONF_MAIL_SENDER['name']);
74
        $this->mail->SMTPOptions = [
75
            'ssl' => [
76
                'verify_peer' => false,
77
                'verify_peer_name' => false,
78
                'allow_self_signed' => true
79
            ]
80
        ];
81
    }
82
    
83
    /**
84
     * @param string $MailBody
85
     * @param string $customer
86
     * @return bool
87
     */
88
    public function SendMail(string $MailBody, string $Subject): bool
89
    {
90
        try {
91
            // Content
92
            $this->mail->Subject = $Subject;
93
            $this->mail->Body = $MailBody;
94
            $this->mail->AltBody = $Subject;
95
        
96
            if (!$this->mail->send()) {
97
                $this->error = $this->mail->ErrorInfo;
98
                return false;
99
            }
100
101
            return true;
102
        } catch (\Exception $exception) {
103
            Log::exception($exception);
104
            Log::debug($this->mail->ErrorInfo);
105
106
            $this->error = $this->mail->ErrorInfo;
107
            return false;
108
        }
109
    }
110
111
    /**
112
     * @return void
113
     */
114
    public function clearAddresses(): void
115
    {
116
        $this->mail->ClearAddresses();
117
    }
118
}
119
120