create-prisma-php-app 1.16.2 → 1.16.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -35,50 +35,80 @@ class Mailer
|
|
|
35
35
|
* @param string $to The recipient's email address.
|
|
36
36
|
* @param string $subject The subject of the email.
|
|
37
37
|
* @param string $body The HTML body of the email.
|
|
38
|
-
* @param
|
|
39
|
-
* @param string $altBody (optional) The plain text alternative body of the email.
|
|
38
|
+
* @param array $options (optional) Additional email options like name, altBody, CC, and BCC.
|
|
40
39
|
*
|
|
41
40
|
* @return bool Returns true if the email is sent successfully, false otherwise.
|
|
42
41
|
*
|
|
43
42
|
* @throws Exception Throws an exception if the email could not be sent.
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* $mailer = new Mailer();
|
|
47
|
-
* $to = 'recipient@example.com';
|
|
48
|
-
* $subject = 'Hello';
|
|
49
|
-
* $body = '<h1>Example Email</h1><p>This is the HTML body of the email.</p>';
|
|
50
|
-
* $name = 'John Doe';
|
|
51
|
-
* $altBody = 'This is the plain text alternative body of the email.';
|
|
52
|
-
*
|
|
53
|
-
* try {
|
|
54
|
-
* $result = $mailer->send($to, $subject, $body, $name, $altBody);
|
|
55
|
-
* if ($result) {
|
|
56
|
-
* echo 'Email sent successfully.';
|
|
57
|
-
* } else {
|
|
58
|
-
* echo 'Failed to send email.';
|
|
59
|
-
* }
|
|
60
|
-
* } catch (Exception $e) {
|
|
61
|
-
* echo 'An error occurred: ' . $e->getMessage();
|
|
62
|
-
* }
|
|
63
43
|
*/
|
|
64
|
-
public function send(string $to, string $subject, string $body,
|
|
44
|
+
public function send(string $to, string $subject, string $body, array $options = []): bool
|
|
65
45
|
{
|
|
66
46
|
try {
|
|
67
|
-
|
|
68
|
-
Validator::
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
47
|
+
// Validate and sanitize inputs
|
|
48
|
+
if (!Validator::email($to)) {
|
|
49
|
+
throw new \Exception('Invalid email address for the main recipient');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
$subject = Validator::string($subject);
|
|
53
|
+
$body = Validator::string($body);
|
|
54
|
+
|
|
55
|
+
$name = $options['name'] ?? '';
|
|
56
|
+
$altBody = $options['altBody'] ?? '';
|
|
57
|
+
$addCC = $options['addCC'] ?? [];
|
|
58
|
+
$addBCC = $options['addBCC'] ?? [];
|
|
72
59
|
|
|
60
|
+
$name = Validator::string($name);
|
|
61
|
+
$altBody = Validator::string($altBody);
|
|
62
|
+
|
|
63
|
+
// Handle CC recipients
|
|
64
|
+
$this->handleRecipients($addCC, 'CC');
|
|
65
|
+
// Handle BCC recipients
|
|
66
|
+
$this->handleRecipients($addBCC, 'BCC');
|
|
67
|
+
|
|
68
|
+
// Set the main recipient and other email properties
|
|
73
69
|
$this->mail->addAddress($to, $name);
|
|
74
70
|
$this->mail->isHTML(true);
|
|
75
71
|
$this->mail->Subject = $subject;
|
|
76
72
|
$this->mail->Body = $body;
|
|
77
73
|
$this->mail->AltBody = $altBody;
|
|
78
74
|
|
|
75
|
+
// Send the email
|
|
79
76
|
return $this->mail->send();
|
|
80
77
|
} catch (\Exception $e) {
|
|
81
78
|
throw new \Exception($e->getMessage());
|
|
82
79
|
}
|
|
83
80
|
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Handle adding CC or BCC recipients.
|
|
84
|
+
*
|
|
85
|
+
* @param string|array $recipients Email addresses to add.
|
|
86
|
+
* @param string $type Type of recipient ('CC' or 'BCC').
|
|
87
|
+
*
|
|
88
|
+
* @throws Exception Throws an exception if any email address is invalid.
|
|
89
|
+
*/
|
|
90
|
+
private function handleRecipients(string|array $recipients, string $type): void
|
|
91
|
+
{
|
|
92
|
+
if (!empty($recipients)) {
|
|
93
|
+
$method = $type === 'CC' ? 'addCC' : 'addBCC';
|
|
94
|
+
|
|
95
|
+
if (is_array($recipients)) {
|
|
96
|
+
foreach ($recipients as $recipient) {
|
|
97
|
+
$recipient = Validator::email($recipient);
|
|
98
|
+
if ($recipient) {
|
|
99
|
+
$this->mail->{$method}($recipient);
|
|
100
|
+
} else {
|
|
101
|
+
throw new \Exception("Invalid email address in $type");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
} else {
|
|
105
|
+
$recipient = Validator::email($recipients);
|
|
106
|
+
if ($recipient) {
|
|
107
|
+
$this->mail->{$method}($recipient);
|
|
108
|
+
} else {
|
|
109
|
+
throw new \Exception("Invalid email address in $type");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
84
114
|
}
|