1 | <?php |
||
2 | |||
3 | namespace EdyWladson\SendMail; |
||
4 | |||
5 | use PHPMailer\PHPMailer\PHPMailer; |
||
6 | use PHPMailer\PHPMailer\Exception; |
||
7 | |||
8 | /** |
||
9 | * Class SendMail |
||
10 | * @package EdyWladson\SendMail |
||
11 | */ |
||
12 | class SendMail |
||
13 | { |
||
14 | private $data; |
||
15 | |||
16 | private $mail; |
||
17 | |||
18 | private $message; |
||
19 | |||
20 | public function __construct( |
||
21 | string $mail_host, |
||
22 | string $mail_port, |
||
23 | string $mail_user, |
||
24 | string $mail_pass, |
||
25 | string $mail_lang = "en", |
||
26 | string $mail_secure = "tls", |
||
27 | string $mail_charset = "utf-8", |
||
28 | bool $mail_html = true, |
||
29 | bool $mail_auth = true |
||
30 | ) { |
||
31 | $this->mail = new PHPMailer(true); |
||
32 | $this->data = new \stdClass(); |
||
33 | |||
34 | //setup |
||
35 | $this->mail->isSMTP(); |
||
36 | $this->mail->setLanguage($mail_lang); |
||
37 | $this->mail->isHTML($mail_html); |
||
38 | $this->mail->SMTPAuth = $mail_auth; |
||
39 | $this->mail->SMTPSecure = $mail_secure; |
||
40 | $this->mail->CharSet = $mail_charset; |
||
41 | |||
42 | //auth |
||
43 | $this->mail->Host = $mail_host; |
||
44 | $this->mail->Port = $mail_port; |
||
0 ignored issues
–
show
|
|||
45 | $this->mail->Username = $mail_user; |
||
46 | $this->mail->Password = $mail_pass; |
||
47 | |||
48 | $this->mail->SMTPOptions = array( |
||
49 | 'ssl' => array( |
||
50 | 'verify_peer' => false, |
||
51 | 'verify_peer_name' => false, |
||
52 | 'allow_self_signed' => true |
||
53 | ) |
||
54 | ); |
||
55 | } |
||
56 | |||
57 | public function mail(string $subject, string $body, string $recipientMail, string $recipientName): SendMail |
||
58 | { |
||
59 | $this->data->subject = $subject; |
||
60 | $this->data->body = $body; |
||
61 | $this->data->recipientMail = $recipientMail; |
||
62 | $this->data->recipientName = $recipientName; |
||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | public function send(string $fromMail, string $fromName): bool |
||
67 | { |
||
68 | if (empty($this->data)) { |
||
69 | $this->message = "Error sending, please check the data"; |
||
70 | return false; |
||
71 | } |
||
72 | |||
73 | if (!filter_var($this->data->recipientMail, FILTER_VALIDATE_EMAIL)) { |
||
74 | $this->message = "The recipient's email is not valid"; |
||
75 | return false; |
||
76 | } |
||
77 | |||
78 | if (!filter_var($fromMail, FILTER_VALIDATE_EMAIL)) { |
||
79 | $this->message = "The sender's email is not valid"; |
||
80 | return false; |
||
81 | } |
||
82 | |||
83 | try { |
||
84 | $this->mail->setFrom($fromMail, $fromName); |
||
85 | $this->mail->Subject = $this->data->subject; |
||
86 | $this->mail->msgHTML($this->data->body); |
||
87 | $this->mail->addAddress($this->data->recipientMail, $this->data->recipientName); |
||
88 | |||
89 | $this->mail->send(); |
||
90 | return true; |
||
91 | } catch (Exception $exception) { |
||
92 | $this->message = $exception->getMessage(); |
||
93 | return false; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | public function message(): string |
||
98 | { |
||
99 | return $this->message; |
||
100 | } |
||
101 | } |
||
102 |
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.