1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace JuniorCorpse\NotifyToMail; |
5
|
|
|
|
6
|
|
|
use PHPMailer\PHPMailer\PHPMailer; |
7
|
|
|
use PHPMailer\PHPMailer\Exception; |
8
|
|
|
|
9
|
|
|
class Email |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** @var \stdClass */ |
13
|
|
|
private $data; |
14
|
|
|
|
15
|
|
|
/** @var PHPMailer */ |
16
|
|
|
private $mail; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Email constructor. |
20
|
|
|
* @param int $smtpDegug |
21
|
|
|
* @param string $host |
22
|
|
|
* @param string $user |
23
|
|
|
* @param string $passwd |
24
|
|
|
* @param string $smtpSecure |
25
|
|
|
* @param int $port |
26
|
|
|
* @param string $charSEt |
27
|
|
|
* @param string $language |
28
|
|
|
*/ |
29
|
|
|
public function __construct( |
30
|
|
|
int $smtpDegug, |
31
|
|
|
string $host, |
32
|
|
|
string $user, |
33
|
|
|
string $passwd, |
34
|
|
|
string $smtpSecure, |
35
|
|
|
int $port = 587, |
36
|
|
|
string $charSEt = 'utf-8', |
37
|
|
|
string $language = 'br' |
38
|
|
|
|
39
|
|
|
) { |
40
|
|
|
$this->mail = new PHPMailer(true); |
41
|
|
|
$this->data = new \stdClass(); |
42
|
|
|
//Server settings |
43
|
|
|
$this->mail->SMTPDebug = $smtpDegug; // Enable verbose debug output |
44
|
|
|
$this->mail->isSMTP(); // Send using SMTP |
45
|
|
|
$this->mail->Host = $host; // Set the SMTP server to send through |
46
|
|
|
$this->mail->SMTPAuth = true; // Enable SMTP authentication |
47
|
|
|
$this->mail->Username = $user; // SMTP username |
48
|
|
|
$this->mail->Password = $passwd; // SMTP password |
49
|
|
|
$this->mail->SMTPSecure = $smtpSecure; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS |PHPMailer::ENCRYPTION_STARTTLS` also accepted |
50
|
|
|
$this->mail->Port = $port; // TCP port to connect to |
51
|
|
|
$this->mail->CharSet = $charSEt; |
52
|
|
|
$this->mail->setLanguage($language); |
53
|
|
|
$this->mail->isHTML(true); |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $subject |
59
|
|
|
* @param string $body |
60
|
|
|
* @param string $recipient |
61
|
|
|
* @param string $recipientName |
62
|
|
|
* @return Email |
63
|
|
|
*/ |
64
|
|
|
public function boot(string $subject, string $body, string $recipient, string $recipientName): Email |
65
|
|
|
{ |
66
|
|
|
$this->data->subject = $subject; |
67
|
|
|
$this->data->body = $body; |
68
|
|
|
$this->data->recipient_email = $recipient; |
69
|
|
|
$this->data->recipient_name = $recipientName; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $filePath |
75
|
|
|
* @param string $fileName |
76
|
|
|
* @return Email |
77
|
|
|
*/ |
78
|
|
|
public function attach(string $filePath, string $fileName): Email { |
79
|
|
|
$this->data->attach[$filePath] = $fileName; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $email |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function is_email(string $email): bool |
88
|
|
|
{ |
89
|
|
|
return filter_var($email, FILTER_VALIDATE_EMAIL); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $fromAddressMail |
94
|
|
|
* @param string $fromNameAddressMail |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function sendEmail(string $from, string $fromName): bool |
98
|
|
|
{ |
99
|
|
|
|
100
|
|
|
try { |
101
|
|
|
$this->mail->Subject = $this->data->subject; |
102
|
|
|
$this->mail->msgHTML($this->data->body); |
103
|
|
|
$this->mail->addAddress($this->data->recipient_email, $this->data->recipient_name); |
104
|
|
|
$this->mail->setFrom($from, $fromName); |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
if (!empty($this->data->attach)) { |
108
|
|
|
foreach ($this->data->attach as $path => $name) { |
109
|
|
|
$this->mail->addAttachment($path, $name); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->mail->send(); |
114
|
|
|
return true; |
115
|
|
|
} catch (Exception $e) { |
116
|
|
|
echo "Erro ao enviar o e-mail: {$this->mail->ErrorInfo} {$e->getMessage()}"; |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return PHPMailer |
123
|
|
|
*/ |
124
|
|
|
public function mail(): PHPMailer |
125
|
|
|
{ |
126
|
|
|
return $this->mail; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
} |