create-prisma-php-app 2.0.0-beta.3 → 2.0.0-beta.5

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.
@@ -38,7 +38,8 @@ class Mailer
38
38
  * @param string $to The recipient's email address.
39
39
  * @param string $subject The subject of the email.
40
40
  * @param string $body The HTML body of the email.
41
- * @param array $options (optional) Additional email options like name, altBody, CC, and BCC.
41
+ * @param array $options (optional) Additional email options like name, altBody, CC, BCC, and attachments.
42
+ * - attachments: A string or an array of file paths, or an array of associative arrays with keys 'path' and 'name'.
42
43
  *
43
44
  * @return bool Returns true if the email is sent successfully, false otherwise.
44
45
  *
@@ -50,7 +51,7 @@ class Mailer
50
51
  // Validate and sanitize inputs
51
52
  $to = Validator::email($to);
52
53
  if (!$to) {
53
- throw new \Exception('Invalid email address for the main recipient');
54
+ throw new Exception('Invalid email address for the main recipient');
54
55
  }
55
56
 
56
57
  $subject = Validator::string($subject);
@@ -60,6 +61,7 @@ class Mailer
60
61
  $name = $options['name'] ?? '';
61
62
  $addCC = $options['addCC'] ?? [];
62
63
  $addBCC = $options['addBCC'] ?? [];
64
+ $attachments = $options['attachments'] ?? [];
63
65
 
64
66
  $name = Validator::string($name);
65
67
 
@@ -67,6 +69,10 @@ class Mailer
67
69
  $this->handleRecipients($addCC, 'CC');
68
70
  // Handle BCC recipients
69
71
  $this->handleRecipients($addBCC, 'BCC');
72
+ // Handle file attachments if provided
73
+ if (!empty($attachments)) {
74
+ $this->handleAttachments($attachments);
75
+ }
70
76
 
71
77
  // Set the main recipient and other email properties
72
78
  $this->mail->addAddress($to, $name);
@@ -77,8 +83,8 @@ class Mailer
77
83
 
78
84
  // Send the email
79
85
  return $this->mail->send();
80
- } catch (\Exception $e) {
81
- throw new \Exception($e->getMessage());
86
+ } catch (Exception $e) {
87
+ throw new Exception($e->getMessage());
82
88
  }
83
89
  }
84
90
 
@@ -101,7 +107,7 @@ class Mailer
101
107
  if ($recipient) {
102
108
  $this->mail->{$method}($recipient);
103
109
  } else {
104
- throw new \Exception("Invalid email address in $type");
110
+ throw new Exception("Invalid email address in $type");
105
111
  }
106
112
  }
107
113
  } else {
@@ -109,12 +115,47 @@ class Mailer
109
115
  if ($recipient) {
110
116
  $this->mail->{$method}($recipient);
111
117
  } else {
112
- throw new \Exception("Invalid email address in $type");
118
+ throw new Exception("Invalid email address in $type");
113
119
  }
114
120
  }
115
121
  }
116
122
  }
117
123
 
124
+ /**
125
+ * Handle adding file attachments.
126
+ *
127
+ * @param string|array $attachments File path(s) to attach.
128
+ * You can pass a string for a single file or an array of file paths.
129
+ * Alternatively, each attachment can be an array with keys 'path' and 'name' for custom naming.
130
+ *
131
+ * @throws Exception Throws an exception if any attachment file is not found.
132
+ */
133
+ private function handleAttachments(string|array $attachments): void
134
+ {
135
+ if (is_array($attachments)) {
136
+ foreach ($attachments as $attachment) {
137
+ if (is_array($attachment)) {
138
+ $file = $attachment['path'] ?? null;
139
+ $name = $attachment['name'] ?? '';
140
+ if (!$file || !file_exists($file)) {
141
+ throw new Exception("Attachment file does not exist: " . ($file ?? 'unknown'));
142
+ }
143
+ $this->mail->addAttachment($file, $name);
144
+ } else {
145
+ if (!file_exists($attachment)) {
146
+ throw new Exception("Attachment file does not exist: $attachment");
147
+ }
148
+ $this->mail->addAttachment($attachment);
149
+ }
150
+ }
151
+ } else {
152
+ if (!file_exists($attachments)) {
153
+ throw new Exception("Attachment file does not exist: $attachments");
154
+ }
155
+ $this->mail->addAttachment($attachments);
156
+ }
157
+ }
158
+
118
159
  /**
119
160
  * Convert HTML content to plain text.
120
161
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-prisma-php-app",
3
- "version": "2.0.0-beta.3",
3
+ "version": "2.0.0-beta.5",
4
4
  "description": "Prisma-PHP: A Revolutionary Library Bridging PHP with Prisma ORM",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",