Passed
Branch master (d55c4a)
by refat
03:31
created

Email::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace System;
4
5
use PHPMailer\PHPMailer\PHPMailer;
6
use PHPMailer\PHPMailer\SMTP;
7
use PHPMailer\PHPMailer\Exception;
8
use System\Error;
9
10
class Email
11
{
12
  /**
13
   * Application Object
14
   *
15
   * @var \System\Application
16
   */
17
  private $app;
18
19
  /**
20
   * PHPMailer Object
21
   *
22
   * @var PHPMailer
23
   */
24
  private $mail;
25
26
  /**
27
   * Constructor
28
   *
29
   * @param \System\Application $app
30
   */
31
  public function __construct(Application $app)
32
  {
33
    $this->app = $app;
34
35
    $this->mail = new PHPMailer(true);
36
37
    $this->setUp();
38
  }
39
40
  /**
41
   * Set up the configrations
42
   *
43
   * @return void
44
   */
45
  private function setUp()
46
  {
47
    $this->mail->SMTPDebug = Error::allowDisplayingError() ? SMTP::DEBUG_SERVER : 0;
48
    $this->mail->isSMTP();
49
    $this->mail->Host = $_ENV['EMAIL_HOST'];
50
    $this->mail->SMTPAuth = $_ENV['EMAIL_SMTPAUTH'];
51
    $this->mail->Username = $_ENV['EMAIL_USERNAME'];
52
    $this->mail->Password = $_ENV['EMAIL_PASSWORD'];
53
    $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
54
    $this->mail->Port = $_ENV['EMAIL_PORT'];
55
  }
56
57
  /**
58
   * Add recipients to email
59
   *
60
   * @return $this
61
   */
62
  public function recipients($addresses, array $replayTo = [], $cc = null, $bcc = null)
63
  {
64
    $this->mail->setFrom($_ENV['EMAIL_ADMIN'], $_ENV['EMAIL_NAME']);
65
66
    $this->addAddresses($addresses);
67
68
    if (!empty($replayTo)) $this->mail->addReplyTo(array_values($replayTo)[0], array_keys($replayTo)[0]);
69
70
    if ($cc) $this->mail->addCC($cc);
71
72
    if ($bcc) $this->mail->addBCC($bcc);
73
74
    return $this;
75
  }
76
77
  /**
78
   * Add addresses
79
   *
80
   * @return void
81
   */
82
  private function addAddresses($addresses)
83
  {
84
    if (!is_array($addresses)) $addresses = [$addresses];
85
86
    foreach ($addresses as $key => $value) {
87
      if (is_numeric($key)) {
88
89
        $this->mail->addAddress($value);
90
      } else {
91
92
        $this->mail->addAddress($value, $key);
93
      }
94
    }
95
  }
96
97
  /**
98
   * Add attachments to email
99
   *
100
   * @return $this
101
   */
102
  public function attachments($attachments)
103
  {
104
    if (!is_array($attachments)) $attachments = [$attachments];
105
106
    foreach ($attachments as $key => $value) {
107
      if (is_numeric($key)) {
108
109
        $this->mail->addAttachment($value);
110
      } else {
111
112
        $this->mail->addAttachment($value, $key);
113
      }
114
    }
115
116
    return $this;
117
  }
118
119
  /**
120
   * Add content to email
121
   *
122
   * @return $this
123
   */
124
  public function content($html, $subject, $body, $altBody)
125
  {
126
    $this->mail->isHTML($html);
127
    $this->mail->Subject = $subject;
128
    $this->mail->Body = $body;
129
    $this->mail->AltBody = $altBody;
130
131
    return $this;
132
  }
133
134
  /**
135
   * Send email
136
   *
137
   * @return void
138
   */
139
  public function send()
140
  {
141
    try {
142
      $this->mail->send();
143
    } catch (Exception $e) {
144
      echo "Message could not be sent. Mailer Error: {$this->mail->ErrorInfo}";
145
    }
146
  }
147
}
148