|
5 | 5 | namespace Codefy\Framework\Helpers;
|
6 | 6 |
|
7 | 7 | use Codefy\Framework\Codefy;
|
| 8 | +use Codefy\Framework\Factory\FileLoggerFactory; |
| 9 | +use Codefy\Framework\Support\CodefyMailer; |
8 | 10 | use Qubus\Config\Collection;
|
9 | 11 | use Qubus\Dbal\Connection;
|
10 | 12 | use Qubus\Exception\Exception;
|
11 | 13 | use Qubus\Expressive\OrmBuilder;
|
| 14 | +use ReflectionException; |
12 | 15 |
|
13 | 16 | use function file_exists;
|
| 17 | +use function Qubus\Security\Helpers\__observer; |
14 | 18 | use function Qubus\Support\Helpers\is_false__;
|
15 | 19 | use function Qubus\Support\Helpers\is_null__;
|
16 | 20 | use function rtrim;
|
@@ -106,3 +110,103 @@ function dbal(): Connection
|
106 | 110 | {
|
107 | 111 | return Codefy::$PHP->getDbConnection();
|
108 | 112 | }
|
| 113 | + |
| 114 | +/** |
| 115 | + * Alternative to PHP's native mail function with SMTP support. |
| 116 | + * |
| 117 | + * This is a simple mail function to see for testing or for |
| 118 | + * sending simple email messages. |
| 119 | + * |
| 120 | + * @param string|array $to Recipient(s) |
| 121 | + * @param string $subject Subject of the email. |
| 122 | + * @param string $message The email body. |
| 123 | + * @param array $headers An array of headers. |
| 124 | + * @param array $attachments An array of attachments. |
| 125 | + * @return bool |
| 126 | + * @throws Exception|ReflectionException|\PHPMailer\PHPMailer\Exception |
| 127 | + */ |
| 128 | +function mail(string|array $to, string $subject, string $message, array $headers = [], array $attachments = []): bool |
| 129 | +{ |
| 130 | + // Instantiate CodefyMailer. |
| 131 | + $instance = new CodefyMailer(config: app(name: 'codefy.config')); |
| 132 | + |
| 133 | + // Set the mailer transport. |
| 134 | + $instance = config(key: 'mailer.mail_transport') === 'smtp' ? $instance->withSmtp() : $instance->withIsMail(); |
| 135 | + |
| 136 | + // Detect HTML markdown. |
| 137 | + if (substr_count(haystack: $message, needle: '</') >= 1) { |
| 138 | + $instance = $instance->withHtml(isHtml: true); |
| 139 | + } |
| 140 | + |
| 141 | + // Build recipient(s). |
| 142 | + $instance = $instance->withTo(address: $to); |
| 143 | + |
| 144 | + // Set from name and from email from environment variables. |
| 145 | + __observer()->filter->addFilter('mail.from.name', fn() => env(key: 'MAILER_FROM_NAME')); |
| 146 | + __observer()->filter->addFilter('mail.from.email', fn() => env(key: 'MAILER_FROM_EMAIL')); |
| 147 | + // Set charset |
| 148 | + __observer()->filter->addFilter('mail.charset', fn() => 'utf-8'); |
| 149 | + |
| 150 | + // Loop through the filters. |
| 151 | + foreach (__observer()->filter->getHooks() as $hook) { |
| 152 | + if ($hook['hook'] === 'mail.from.name') { |
| 153 | + $fromName = $hook['callback'](); |
| 154 | + } |
| 155 | + |
| 156 | + if ($hook['hook'] === 'mail.from.email') { |
| 157 | + $fromEmail = $hook['callback'](); |
| 158 | + } |
| 159 | + |
| 160 | + if ($hook['hook'] === 'mail.charset') { |
| 161 | + $charset = $hook['callback'](); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // Set email subject and body. |
| 166 | + $instance = $instance->withSubject(subject: $subject)->withBody(data: $message); |
| 167 | + |
| 168 | + // Check for other headers and loop through them. |
| 169 | + if (!empty($headers)) { |
| 170 | + foreach ($headers as $name => $content) { |
| 171 | + if ($name === 'cc') { |
| 172 | + $instance = $instance->withCc(address: $content); |
| 173 | + } |
| 174 | + |
| 175 | + if ($name === 'bcc') { |
| 176 | + $instance = $instance->withBcc(address: $content); |
| 177 | + } |
| 178 | + |
| 179 | + if ($name === 'replyTo') { |
| 180 | + $instance = $instance->withReplyTo(address: $content); |
| 181 | + } |
| 182 | + |
| 183 | + if (! in_array(needle: $name, haystack: ['MIME-Version','to','cc','bcc','replyTo'], strict: true)) { |
| 184 | + $instance = $instance->withCustomHeader(name: $name, value: $content); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // Set X-Mailer header |
| 190 | + $instance = $instance->withXMailer(xmailer: 'CodefyPHP Framework ' . Application::APP_VERSION); |
| 191 | + |
| 192 | + // Set email charset |
| 193 | + $instance = $instance->withCharset(charset: $charset ?? 'utf-8'); |
| 194 | + |
| 195 | + // Check if there are attachments and loop through them. |
| 196 | + if (!empty($attachments)) { |
| 197 | + foreach ($attachments as $filename => $filepath) { |
| 198 | + $filename = is_string(value: $filename) ? $filename : ''; |
| 199 | + $instance = $instance->withAttachment(path: $filepath, name: $filename); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + // Set sender. |
| 204 | + $instance = $instance->withFrom(address: $fromEmail ?? '', name: $fromName ?? ''); |
| 205 | + |
| 206 | + try { |
| 207 | + return $instance->send(); |
| 208 | + } catch (\PHPMailer\PHPMailer\Exception $e) { |
| 209 | + FileLoggerFactory::getLogger()->error($e->getMessage(), ['function' => '\Codefy\Framework\Helpers\mail']); |
| 210 | + return false; |
| 211 | + } |
| 212 | +} |
0 commit comments