create-prisma-php-app 1.16.3 → 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,99 +35,35 @@ 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.
|
|
40
|
-
* @param string|array $addCC (optional) Additional email addresses to send a carbon copy (CC) to.
|
|
41
|
-
* @param string|array $addBCC (optional) Additional email addresses to send a blind carbon copy (BCC) to.
|
|
38
|
+
* @param array $options (optional) Additional email options like name, altBody, CC, and BCC.
|
|
42
39
|
*
|
|
43
40
|
* @return bool Returns true if the email is sent successfully, false otherwise.
|
|
44
41
|
*
|
|
45
42
|
* @throws Exception Throws an exception if the email could not be sent.
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* $mailer = new Mailer();
|
|
49
|
-
* $to = 'recipient@example.com';
|
|
50
|
-
* $subject = 'Hello';
|
|
51
|
-
* $body = '<h1>Example Email</h1><p>This is the HTML body of the email.</p>';
|
|
52
|
-
* $name = 'John Doe';
|
|
53
|
-
* $altBody = 'This is the plain text alternative body of the email.';
|
|
54
|
-
* $addCC = ['cc1@example.com', 'cc2@example.com'];
|
|
55
|
-
* $addBCC = 'bcc@example.com';
|
|
56
|
-
*
|
|
57
|
-
* try {
|
|
58
|
-
* $result = $mailer->send($to, $subject, $body, $name, $altBody, $addCC, $addBCC);
|
|
59
|
-
* if ($result) {
|
|
60
|
-
* echo 'Email sent successfully.';
|
|
61
|
-
* } else {
|
|
62
|
-
* echo 'Failed to send email.';
|
|
63
|
-
* }
|
|
64
|
-
* } catch (Exception $e) {
|
|
65
|
-
* echo 'An error occurred: ' . $e->getMessage();
|
|
66
|
-
* }
|
|
67
43
|
*/
|
|
68
|
-
public function send(
|
|
69
|
-
|
|
70
|
-
string $subject,
|
|
71
|
-
string $body,
|
|
72
|
-
string $name = '',
|
|
73
|
-
string $altBody = '',
|
|
74
|
-
string|array $addCC = [],
|
|
75
|
-
string|array $addBCC = []
|
|
76
|
-
): bool {
|
|
44
|
+
public function send(string $to, string $subject, string $body, array $options = []): bool
|
|
45
|
+
{
|
|
77
46
|
try {
|
|
78
|
-
// Validate
|
|
79
|
-
|
|
80
|
-
if (!$to) {
|
|
47
|
+
// Validate and sanitize inputs
|
|
48
|
+
if (!Validator::email($to)) {
|
|
81
49
|
throw new \Exception('Invalid email address for the main recipient');
|
|
82
50
|
}
|
|
83
51
|
|
|
84
|
-
// Validate and sanitize other inputs
|
|
85
52
|
$subject = Validator::string($subject);
|
|
86
53
|
$body = Validator::string($body);
|
|
54
|
+
|
|
55
|
+
$name = $options['name'] ?? '';
|
|
56
|
+
$altBody = $options['altBody'] ?? '';
|
|
57
|
+
$addCC = $options['addCC'] ?? [];
|
|
58
|
+
$addBCC = $options['addBCC'] ?? [];
|
|
59
|
+
|
|
87
60
|
$name = Validator::string($name);
|
|
88
61
|
$altBody = Validator::string($altBody);
|
|
89
62
|
|
|
90
63
|
// Handle CC recipients
|
|
91
|
-
|
|
92
|
-
if (is_array($addCC)) {
|
|
93
|
-
foreach ($addCC as $cc) {
|
|
94
|
-
$cc = Validator::email($cc);
|
|
95
|
-
if ($cc) {
|
|
96
|
-
$this->mail->addCC($cc);
|
|
97
|
-
} else {
|
|
98
|
-
throw new \Exception('Invalid email address in CC');
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
} else {
|
|
102
|
-
$cc = Validator::email($addCC);
|
|
103
|
-
if ($cc) {
|
|
104
|
-
$this->mail->addCC($cc);
|
|
105
|
-
} else {
|
|
106
|
-
throw new \Exception('Invalid email address in CC');
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
64
|
+
$this->handleRecipients($addCC, 'CC');
|
|
111
65
|
// Handle BCC recipients
|
|
112
|
-
|
|
113
|
-
if (is_array($addBCC)) {
|
|
114
|
-
foreach ($addBCC as $bcc) {
|
|
115
|
-
$bcc = Validator::email($bcc);
|
|
116
|
-
if ($bcc) {
|
|
117
|
-
$this->mail->addBCC($bcc);
|
|
118
|
-
} else {
|
|
119
|
-
throw new \Exception('Invalid email address in BCC');
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
} else {
|
|
123
|
-
$bcc = Validator::email($addBCC);
|
|
124
|
-
if ($bcc) {
|
|
125
|
-
$this->mail->addBCC($bcc);
|
|
126
|
-
} else {
|
|
127
|
-
throw new \Exception('Invalid email address in BCC');
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
66
|
+
$this->handleRecipients($addBCC, 'BCC');
|
|
131
67
|
|
|
132
68
|
// Set the main recipient and other email properties
|
|
133
69
|
$this->mail->addAddress($to, $name);
|
|
@@ -142,4 +78,37 @@ class Mailer
|
|
|
142
78
|
throw new \Exception($e->getMessage());
|
|
143
79
|
}
|
|
144
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
|
+
}
|
|
145
114
|
}
|