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; |
|
|
|
|
25
|
|
|
$this->mailer->Port = MAIL_PORT; |
|
|
|
|
26
|
|
|
|
27
|
|
|
if (MAIL_DEVELOPMENT != true) { |
|
|
|
|
28
|
|
|
$this->mailer->SMTPAuth = true; |
29
|
|
|
$this->mailer->Username = MAIL_USER; |
|
|
|
|
30
|
|
|
$this->mailer->Password = MAIL_PASS; |
|
|
|
|
31
|
|
|
|
32
|
|
|
if (MAIL_USE_ENCRYPTION == true) { |
|
|
|
|
33
|
|
|
$this->mailer->SMTPSecure = MAIL_ENCRYPTION; |
|
|
|
|
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 (!$this->mailer->send()) { |
65
|
|
|
throw new \Exception("Unable to send mail: " . $this->mailer->ErrorInfo); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|