Passed
Branch master (93b6ae)
by refat
12:48
created

Email::attachments()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 2
b 0
f 0
nc 6
nop 1
dl 0
loc 15
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;
0 ignored issues
show
Bug introduced by
Are you sure the usage of System\Error::allowDisplayingError() targeting System\Error::allowDisplayingError() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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
    if (!is_array($addresses)) $addresses = [$addresses];
67
68
    foreach ($addresses as $key => $value) {
69
      if (is_numeric($key)) {
70
71
        $this->mail->addAddress($value);
72
      } else {
73
74
        $this->mail->addAddress($value, $key);
75
      }
76
    }
77
78
    if (!empty($replayTo)) $this->mail->addReplyTo(array_values($replayTo)[0], array_keys($replayTo)[0]);
79
80
    if ($cc) $this->mail->addCC($cc);
81
82
    if ($bcc) $this->mail->addBCC($bcc);
83
84
    return $this;
85
  }
86
87
  /**
88
   * Add attachments to email
89
   *
90
   * @return $this
91
   */
92
  public function attachments($attachments)
93
  {
94
    if (!is_array($attachments)) $attachments = [$attachments];
95
96
    foreach ($attachments as $key => $value) {
97
      if (is_numeric($key)) {
98
99
        $this->mail->addAttachment($value);
100
      } else {
101
102
        $this->mail->addAttachment($value, $key);
103
      }
104
    }
105
106
    return $this;
107
  }
108
109
  /**
110
   * Add content to email
111
   *
112
   * @return $this
113
   */
114
  public function content($html, $subject, $body, $altBody)
115
  {
116
    $this->mail->isHTML($html);
117
    $this->mail->Subject = $subject;
118
    $this->mail->Body = $body;
119
    $this->mail->AltBody = $altBody;
120
121
    return $this;
122
  }
123
124
  /**
125
   * Send email
126
   *
127
   * @return void
128
   */
129
  public function send()
130
  {
131
    try {
132
      $this->mail->send();
133
    } catch (Exception $e) {
134
      echo "Message could not be sent. Mailer Error: {$this->mail->ErrorInfo}";
135
    }
136
  }
137
}
138