Passed
Pull Request — master (#53)
by Matthew
02:43
created

EmailService::SendMail()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 32
rs 8.4444
c 0
b 0
f 0
cc 8
nc 32
nop 1
1
<?php
2
3
namespace PhpDraft\Domain\Services;
4
5
use \Silex\Application;
6
use PhpDraft\Domain\Models\PhpDraftResponse;
7
use PhpDraft\Domain\Models\MailMessage;
8
use PHPMailer\PHPMailer\PHPMailer;
9
10
class EmailService {
11
  private $app;
12
  private $mailer;
13
14
  public function __construct(Application $app) {
15
    $this->app = $app;
16
17
    $this->mailer = new PHPMailer();
18
19
    //Uncomment this line to help debug issues with your SMTP server
20
    //Watch the response from the API when you register/start lost pwd to see the output.
21
    //$this->mailer->SMTPDebug = 2;                               // Enable verbose debug output
22
23
    $this->mailer->isSMTP();
24
    $this->mailer->Host = MAIL_SERVER;
0 ignored issues
show
Bug introduced by
The constant PhpDraft\Domain\Services\MAIL_SERVER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
25
    $this->mailer->Port = MAIL_PORT;
0 ignored issues
show
Bug introduced by
The constant PhpDraft\Domain\Services\MAIL_PORT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
26
27
    if (MAIL_DEVELOPMENT != true) {
0 ignored issues
show
Bug introduced by
The constant PhpDraft\Domain\Services\MAIL_DEVELOPMENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
28
      $this->mailer->SMTPAuth = true;
29
      $this->mailer->Username = MAIL_USER;
0 ignored issues
show
Bug introduced by
The constant PhpDraft\Domain\Services\MAIL_USER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
30
      $this->mailer->Password = MAIL_PASS;
0 ignored issues
show
Bug introduced by
The constant PhpDraft\Domain\Services\MAIL_PASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
31
32
      if (MAIL_USE_ENCRYPTION == true) {
0 ignored issues
show
Bug introduced by
The constant PhpDraft\Domain\Services\MAIL_USE_ENCRYPTION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
33
        $this->mailer->SMTPSecure = MAIL_ENCRYPTION;
0 ignored issues
show
Bug introduced by
The constant PhpDraft\Domain\Services\MAIL_ENCRYPTION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
34
      }
35
    }
36
37
    $this->mailer->From = MAIL_USER;
38
    $this->mailer->FromName = 'Hoot Draft';
39
  }
40
41
  public function SendMail(MailMessage $message) {
42
    foreach ($message->to_addresses as $address => $name) {
43
      $this->mailer->addAddress($address, $name);
44
    }
45
46
    if (count($message->cc_addresses) > 0) {
47
      foreach($message->cc_addresses as $address => $name) {
48
        $this->mailer->addCC($address, $name);
49
      }
50
    }
51
52
    if (count($message->bcc_addresses) > 0) {
53
      foreach($message->bcc_addresses as $address => $name) {
54
        $this->mailer->addBCC($address, $name);
55
      }
56
    }
57
58
    $this->mailer->isHTML($message->is_html);
59
60
    $this->mailer->Subject = $message->subject;
61
62
    $this->mailer->Body = $message->body;
63
64
    if (strlen($message->altBody) > 0) {
65
      $this->mailer->AltBody = $message->altBody;
66
    }
67
68
    if (!$this->mailer->send()) {
69
      throw new \Exception("Unable to send mail: " . $this->mailer->ErrorInfo);
70
    }
71
72
    return;
73
  }
74
}
75