Updated the mailing system

This commit is contained in:
Pinga 2023-11-16 11:32:12 +02:00
parent 3a9d39da38
commit b91fa31bf3
4 changed files with 63 additions and 40 deletions

View file

@ -1,21 +1,44 @@
<?php namespace App\Lib; <?php
use PHPMailer\PHPMailer\Exception; namespace App\Lib;
use PHPMailer\PHPMailer\Exception as PHPMailerException;
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\PHPMailer;
/** use Utopia\Messaging\Messages\Email;
* Mail use Utopia\Messaging\Adapters\Email\SendGrid;
* use Utopia\Messaging\Adapters\Email\Mailgun;
* @author Hezekiah O. <support@hezecom.com>
*/
class Mail class Mail
{ {
public static function send($subject, $body, $from=[], $to=[], $info=[]) public static function send($subject, $body, $from=[], $to=[], $info=[])
{ {
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); $mail = new PHPMailer(true);
try { try {
$mail->SMTPDebug = 0; $mail->SMTPDebug = 0;
if(envi('MAIL_DRIVER')=='smtp') {
$mail->isSMTP(); $mail->isSMTP();
$mail->Host = envi('MAIL_HOST'); $mail->Host = envi('MAIL_HOST');
$mail->SMTPAuth = true; $mail->SMTPAuth = true;
@ -23,13 +46,6 @@ class Mail
$mail->Password = envi('MAIL_PASSWORD'); $mail->Password = envi('MAIL_PASSWORD');
$mail->SMTPSecure = envi('MAIL_ENCRYPTION'); $mail->SMTPSecure = envi('MAIL_ENCRYPTION');
$mail->Port = envi('MAIL_PORT'); $mail->Port = envi('MAIL_PORT');
}
elseif(envi('MAIL_DRIVER')=='sendmail') {
$mail->isSendmail();
}
else{
$mail->isMail();
}
$mail->setFrom($from['email'], $from['name']); $mail->setFrom($from['email'], $from['name']);
$mail->addAddress($to['email'], $to['name']); $mail->addAddress($to['email'], $to['name']);
@ -38,12 +54,12 @@ class Mail
$mail->isHTML(true); $mail->isHTML(true);
$mail->Subject = $subject; $mail->Subject = $subject;
$mail->Body = $body; $mail->Body = $body;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send(); $mail->send();
//echo 'Message has been sent'; return true;
return false;
} catch (Exception $e) { } catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
return false;
}
} }
} }
} }

View file

@ -26,6 +26,7 @@
"nyholm/psr7-server": "^1.0.2", "nyholm/psr7-server": "^1.0.2",
"pinga/auth": "^0.1.1", "pinga/auth": "^0.1.1",
"phpmailer/phpmailer": "^6.8.1", "phpmailer/phpmailer": "^6.8.1",
"utopia-php/messaging": "^0.3.0",
"filp/whoops": "^2.15.3", "filp/whoops": "^2.15.3",
"imefisto/psr-swoole-native": "^1.1.2", "imefisto/psr-swoole-native": "^1.1.2",
"chubbyphp/chubbyphp-static-file": "^1.2", "chubbyphp/chubbyphp-static-file": "^1.2",

View file

@ -10,8 +10,9 @@ if (file_exists(__DIR__.'/.env')) {
return [ return [
'env' => $_ENV['APP_ENV'] ?? 'production', 'env' => $_ENV['APP_ENV'] ?? 'production',
'name' => $_ENV['APP_NAME'] ?? 'Hezecom', 'name' => $_ENV['APP_NAME'] ?? 'CP',
'url' => $_ENV['APP_URL'] ?? 'http://localhost', 'url' => $_ENV['APP_URL'] ?? 'http://localhost',
'domain' => $_ENV['APP_DOMAIN'] ?? 'example.com',
'timezone' => $_ENV['TIME_ZONE'] ?? 'UTC', 'timezone' => $_ENV['TIME_ZONE'] ?? 'UTC',
'default' => $_ENV['DB_DRIVER'] ?? 'mysql', 'default' => $_ENV['DB_DRIVER'] ?? 'mysql',
'connections' => [ 'connections' => [
@ -19,8 +20,8 @@ return [
'driver' => 'mysql', 'driver' => 'mysql',
'host' => $_ENV['DB_HOST'] ?? '127.0.0.1', 'host' => $_ENV['DB_HOST'] ?? '127.0.0.1',
'port' => $_ENV['DB_PORT'] ?? '3306', 'port' => $_ENV['DB_PORT'] ?? '3306',
'database' => $_ENV['DB_DATABASE'] ?? 'forge', 'database' => $_ENV['DB_DATABASE'] ?? 'db_username',
'username' => $_ENV['DB_USERNAME'] ?? 'forge', 'username' => $_ENV['DB_USERNAME'] ?? 'db_password',
'password' => $_ENV['DB_PASSWORD'] ?? '', 'password' => $_ENV['DB_PASSWORD'] ?? '',
'unix_socket' => $_ENV['DB_SOCKET'] ?? '', 'unix_socket' => $_ENV['DB_SOCKET'] ?? '',
'charset' => 'utf8mb4', 'charset' => 'utf8mb4',
@ -38,8 +39,8 @@ return [
'driver' => 'pgsql', 'driver' => 'pgsql',
'host' => $_ENV['DB_HOST'] ?? '127.0.0.1', 'host' => $_ENV['DB_HOST'] ?? '127.0.0.1',
'port' => $_ENV['DB_PORT'] ?? '5432', 'port' => $_ENV['DB_PORT'] ?? '5432',
'database' => $_ENV['DB_DATABASE'] ?? 'forge', 'database' => $_ENV['DB_DATABASE'] ?? 'db_username',
'username' => $_ENV['DB_USERNAME'] ?? 'forge', 'username' => $_ENV['DB_USERNAME'] ?? 'db_password',
'password' => $_ENV['DB_PASSWORD'] ?? '', 'password' => $_ENV['DB_PASSWORD'] ?? '',
'charset' => 'utf8', 'charset' => 'utf8',
'prefix' => '', 'prefix' => '',
@ -50,8 +51,8 @@ return [
'driver' => 'sqlsrv', 'driver' => 'sqlsrv',
'host' => $_ENV['DB_HOST'] ?? 'localhost', 'host' => $_ENV['DB_HOST'] ?? 'localhost',
'port' => $_ENV['DB_PORT'] ?? '1433', 'port' => $_ENV['DB_PORT'] ?? '1433',
'database' => $_ENV['DB_DATABASE'] ?? 'forge', 'database' => $_ENV['DB_DATABASE'] ?? 'db_username',
'username' => $_ENV['DB_USERNAME'] ?? 'forge', 'username' => $_ENV['DB_USERNAME'] ?? 'db_password',
'password' => $_ENV['DB_PASSWORD'] ?? '', 'password' => $_ENV['DB_PASSWORD'] ?? '',
'charset' => 'utf8', 'charset' => 'utf8',
'prefix' => '', 'prefix' => '',
@ -68,5 +69,7 @@ return [
'address' => $_ENV['MAIL_FROM_ADDRESS'] ?? 'hello@example.com', 'address' => $_ENV['MAIL_FROM_ADDRESS'] ?? 'hello@example.com',
'name' => $_ENV['MAIL_FROM_NAME'] ?? 'Example', 'name' => $_ENV['MAIL_FROM_NAME'] ?? 'Example',
], ],
'api_key' => $_ENV['MAIL_API_KEY'] ?? 'test-api-key',
'api_provider' => $_ENV['MAIL_API_PROVIDER'] ?? 'sendgrid',
], ],
]; ];

View file

@ -1,6 +1,7 @@
APP_NAME='CP' APP_NAME='CP'
APP_ENV=public APP_ENV=public
APP_URL=http://localhost APP_URL=http://localhost
APP_DOMAIN=example.com
DB_DRIVER=mysql DB_DRIVER=mysql
DB_HOST=localhost DB_HOST=localhost
@ -9,7 +10,7 @@ DB_USERNAME=root
DB_PASSWORD= DB_PASSWORD=
DB_PORT=3306 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_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io MAIL_HOST=smtp.mailtrap.io
@ -19,3 +20,5 @@ MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS='example@domain.com' MAIL_FROM_ADDRESS='example@domain.com'
MAIL_FROM_NAME='Example' MAIL_FROM_NAME='Example'
MAIL_API_KEY='test-api-key'
MAIL_API_PROVIDER='sendgrid'