1 | <?php |
||
2 | |||
3 | namespace Cafewebcode\Cafewebcodemail; |
||
4 | |||
5 | use PHPMailer\PHPMailer\PHPMailer; |
||
6 | use PHPMailer\PHPMailer\Exception as MailException; |
||
7 | |||
8 | /** |
||
9 | * CLASS MAIL |
||
10 | */ |
||
11 | class Mail |
||
12 | { |
||
13 | |||
14 | /** @var PHPMailer */ |
||
15 | protected PHPMailer $mail; |
||
16 | |||
17 | protected ?object $data; |
||
18 | |||
19 | private ?MailException $fail; |
||
20 | |||
21 | protected string $message; |
||
22 | |||
23 | public function __construct() |
||
24 | { |
||
25 | $this->mail = new PHPMailer(true); |
||
26 | |||
27 | //config |
||
28 | $this->mail->isSMTP(); |
||
29 | $this->mail->isHTML(CONF_MAIL_HTML); |
||
30 | $this->mail->CharSet = CONF_MAIL_CHARSET; |
||
31 | $this->mail->setLanguage(CONF_MAIL_LANG); |
||
32 | $this->mail->Host = CONF_MAIL_HOST; |
||
33 | $this->mail->SMTPAuth = CONF_MAIL_SMTPAUTH; |
||
34 | $this->mail->Username = CONF_MAIL_USERNAME; |
||
35 | $this->mail->Password = CONF_MAIL_PASSWORD; |
||
36 | $this->mail->SMTPSecure = CONF_MAIL_SMTPSECURE; |
||
37 | $this->mail->Port = CONF_MAIL_PORT; |
||
0 ignored issues
–
show
|
|||
38 | } |
||
39 | |||
40 | public function bootstrap(string $subject, string $body, string $recipient, string $recipientName): Mail |
||
41 | { |
||
42 | $this->data = new \stdClass(); |
||
43 | $this->data->subject = $subject; |
||
44 | $this->data->body = $body; |
||
45 | $this->data->recipient = $recipient; |
||
46 | $this->data->recipientName = $recipientName; |
||
47 | |||
48 | return $this; |
||
49 | } |
||
50 | |||
51 | public function fail(): ?MailException |
||
52 | { |
||
53 | return $this->fail; |
||
54 | } |
||
55 | |||
56 | public function attach(string $patch, string $name): ?Mail |
||
57 | { |
||
58 | $this->data->attach[$patch] = $name; |
||
59 | return $this; |
||
60 | } |
||
61 | |||
62 | public function send(string $from, string $fromName): bool |
||
63 | { |
||
64 | try { |
||
65 | if (!$this->required()) { |
||
66 | $this->message = "Preencha todos os dados para enviar"; |
||
67 | return false; |
||
68 | } |
||
69 | if (!filter_var($this->data->recipient, FILTER_VALIDATE_EMAIL)) { |
||
70 | $this->message = "O e-mail de destinatário não parece válido"; |
||
71 | return false; |
||
72 | } |
||
73 | |||
74 | if (!empty($this->data->attach)) { |
||
75 | foreach ($this->data->attach as $path => $name) { |
||
76 | $this->mail->addAttachment($path, $name); |
||
77 | } |
||
78 | } |
||
79 | $this->mail->setFrom($from, $fromName); |
||
80 | $this->mail->addAddress($this->data->recipient, $this->data->recipientName); |
||
81 | $this->mail->Subject = filter_var($this->data->subject, FILTER_SANITIZE_SPECIAL_CHARS); |
||
82 | $this->mail->Body = filter_var($this->data->body, FILTER_DEFAULT); |
||
83 | |||
84 | $this->mail->send(); |
||
85 | return true; |
||
86 | |||
87 | } catch (MailException $exception) { |
||
88 | $this->fail = $exception; |
||
89 | return false; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | private function required(): bool |
||
94 | { |
||
95 | if (empty($this->data->subject) || empty($this->data->body) || empty($this->data->recipient) || empty($this->data->recipientName)) { |
||
96 | return false; |
||
97 | } |
||
98 | |||
99 | return true; |
||
100 | } |
||
101 | } |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.