-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailer.php
More file actions
55 lines (47 loc) · 1.73 KB
/
mailer.php
File metadata and controls
55 lines (47 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
namespace O365Integration;
require 'vendor/autoload.php';
// require 'config.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use TheNetworg\OAuth2\Client\Provider\Azure;
class O365Mailer {
private $mail;
private $accessToken;
public function __construct($accessToken) {
$this->accessToken = $accessToken;
$this->mail = new PHPMailer(true);
$this->mail->isSMTP();
$this->mail->Host = 'smtp.office365.com';
$this->mail->SMTPAuth = true;
$this->mail->AuthType = 'XOAUTH2';
$this->mail->Port = 587;
$this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
}
public function sendMail($to, $subject, $body, $from, $isHtml = false) {
try {
$this->mail->setOAuth(
new OAuth(
[
'provider' => new Azure([
'clientId' => Config::CLIENT_ID,
'clientSecret' => Config::CLIENT_SECRET
]),
'clientId' => Config::CLIENT_ID,
'clientSecret' => Config::CLIENT_SECRET,
'refreshToken' => $this->refreshToken,
'userName' => $from
]
)
);
$this->mail->setFrom($from);
$this->mail->addAddress($to);
$this->mail->Subject = $subject;
$this->mail->Body = $body;
$this->mail->isHTML($isHtml);
return $this->mail->send();
} catch (Exception $e) {
throw new Exception('Mail gönderme hatası: ' . $this->mail->ErrorInfo);
}
}
}