diff --git a/cp/app/Lib/Mail.php b/cp/app/Lib/Mail.php index 7dd380a..38a8639 100644 --- a/cp/app/Lib/Mail.php +++ b/cp/app/Lib/Mail.php @@ -1,21 +1,44 @@ - - */ +use Utopia\Messaging\Messages\Email; +use Utopia\Messaging\Adapters\Email\SendGrid; +use Utopia\Messaging\Adapters\Email\Mailgun; + class Mail { - public static function send($subject, $body, $from=[], $to=[], $info=[]) { - $mail = new PHPMailer(true); - try { - $mail->SMTPDebug = 0; - if(envi('MAIL_DRIVER')=='smtp') { + if (envi('MAIL_DRIVER') == 'utopia') { + try { + $message = new Email( + from: [$from['email']], + to: [$to['email']], + subject: $subject, + content: $body + ); + + // Send email + if (envi('MAIL_API_PROVIDER') == 'sendgrid') { + $messaging = new Sendgrid(envi('MAIL_API_KEY')); + $messaging->send($message); + return true; + } else { + $messaging = new Mailgun(envi('MAIL_API_KEY'), envi('APP_DOMAIN')); + $messaging->send($message); + return true; + } + } catch (\Exception $e) { + echo "Message could not be sent. Error: {$e->getMessage()}"; + return false; + } + } else if (envi('MAIL_DRIVER') == 'smtp') { + $mail = new PHPMailer(true); + try { + $mail->SMTPDebug = 0; $mail->isSMTP(); $mail->Host = envi('MAIL_HOST'); $mail->SMTPAuth = true; @@ -23,27 +46,20 @@ class Mail $mail->Password = envi('MAIL_PASSWORD'); $mail->SMTPSecure = envi('MAIL_ENCRYPTION'); $mail->Port = envi('MAIL_PORT'); - } - elseif(envi('MAIL_DRIVER')=='sendmail') { - $mail->isSendmail(); - } - else{ - $mail->isMail(); - } - $mail->setFrom($from['email'], $from['name']); - $mail->addAddress($to['email'], $to['name']); - //$mail->addAttachment('path/to/invoice1.pdf', 'invoice1.pdf'); + $mail->setFrom($from['email'], $from['name']); + $mail->addAddress($to['email'], $to['name']); + //$mail->addAttachment('path/to/invoice1.pdf', 'invoice1.pdf'); - $mail->isHTML(true); - $mail->Subject = $subject; - $mail->Body = $body; - //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; - $mail->send(); - //echo 'Message has been sent'; - return false; - } catch (Exception $e) { - echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; + $mail->isHTML(true); + $mail->Subject = $subject; + $mail->Body = $body; + $mail->send(); + return true; + } catch (Exception $e) { + echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; + return false; + } } } -} +} \ No newline at end of file diff --git a/cp/composer.json b/cp/composer.json index 68d15c3..da26059 100644 --- a/cp/composer.json +++ b/cp/composer.json @@ -26,6 +26,7 @@ "nyholm/psr7-server": "^1.0.2", "pinga/auth": "^0.1.1", "phpmailer/phpmailer": "^6.8.1", + "utopia-php/messaging": "^0.3.0", "filp/whoops": "^2.15.3", "imefisto/psr-swoole-native": "^1.1.2", "chubbyphp/chubbyphp-static-file": "^1.2", diff --git a/cp/config/app.php b/cp/config/app.php index 14c529a..bd42e12 100644 --- a/cp/config/app.php +++ b/cp/config/app.php @@ -10,8 +10,9 @@ if (file_exists(__DIR__.'/.env')) { return [ 'env' => $_ENV['APP_ENV'] ?? 'production', - 'name' => $_ENV['APP_NAME'] ?? 'Hezecom', + 'name' => $_ENV['APP_NAME'] ?? 'CP', 'url' => $_ENV['APP_URL'] ?? 'http://localhost', + 'domain' => $_ENV['APP_DOMAIN'] ?? 'example.com', 'timezone' => $_ENV['TIME_ZONE'] ?? 'UTC', 'default' => $_ENV['DB_DRIVER'] ?? 'mysql', 'connections' => [ @@ -19,8 +20,8 @@ return [ 'driver' => 'mysql', 'host' => $_ENV['DB_HOST'] ?? '127.0.0.1', 'port' => $_ENV['DB_PORT'] ?? '3306', - 'database' => $_ENV['DB_DATABASE'] ?? 'forge', - 'username' => $_ENV['DB_USERNAME'] ?? 'forge', + 'database' => $_ENV['DB_DATABASE'] ?? 'db_username', + 'username' => $_ENV['DB_USERNAME'] ?? 'db_password', 'password' => $_ENV['DB_PASSWORD'] ?? '', 'unix_socket' => $_ENV['DB_SOCKET'] ?? '', 'charset' => 'utf8mb4', @@ -38,8 +39,8 @@ return [ 'driver' => 'pgsql', 'host' => $_ENV['DB_HOST'] ?? '127.0.0.1', 'port' => $_ENV['DB_PORT'] ?? '5432', - 'database' => $_ENV['DB_DATABASE'] ?? 'forge', - 'username' => $_ENV['DB_USERNAME'] ?? 'forge', + 'database' => $_ENV['DB_DATABASE'] ?? 'db_username', + 'username' => $_ENV['DB_USERNAME'] ?? 'db_password', 'password' => $_ENV['DB_PASSWORD'] ?? '', 'charset' => 'utf8', 'prefix' => '', @@ -50,8 +51,8 @@ return [ 'driver' => 'sqlsrv', 'host' => $_ENV['DB_HOST'] ?? 'localhost', 'port' => $_ENV['DB_PORT'] ?? '1433', - 'database' => $_ENV['DB_DATABASE'] ?? 'forge', - 'username' => $_ENV['DB_USERNAME'] ?? 'forge', + 'database' => $_ENV['DB_DATABASE'] ?? 'db_username', + 'username' => $_ENV['DB_USERNAME'] ?? 'db_password', 'password' => $_ENV['DB_PASSWORD'] ?? '', 'charset' => 'utf8', 'prefix' => '', @@ -68,5 +69,7 @@ return [ 'address' => $_ENV['MAIL_FROM_ADDRESS'] ?? 'hello@example.com', 'name' => $_ENV['MAIL_FROM_NAME'] ?? 'Example', ], + 'api_key' => $_ENV['MAIL_API_KEY'] ?? 'test-api-key', + 'api_provider' => $_ENV['MAIL_API_PROVIDER'] ?? 'sendgrid', ], ]; \ No newline at end of file diff --git a/cp/env-sample b/cp/env-sample index ecd7c51..f148a40 100644 --- a/cp/env-sample +++ b/cp/env-sample @@ -1,6 +1,7 @@ APP_NAME='CP' APP_ENV=public APP_URL=http://localhost +APP_DOMAIN=example.com DB_DRIVER=mysql DB_HOST=localhost @@ -9,7 +10,7 @@ DB_USERNAME=root DB_PASSWORD= DB_PORT=3306 -#mailer settings (Driver= smtp or sendmail or mail) +#mailer settings (Driver = smtp or utopia, Api Provder = sendgrid or mailgun) MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io @@ -19,3 +20,5 @@ MAIL_PASSWORD=password MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS='example@domain.com' MAIL_FROM_NAME='Example' +MAIL_API_KEY='test-api-key' +MAIL_API_PROVIDER='sendgrid'