Update on the notification script

This commit is contained in:
Pinga 2023-11-13 16:01:26 +02:00
parent 9b183ddf5d
commit d4590aa36a
2 changed files with 148 additions and 79 deletions

View file

@ -36,4 +36,18 @@ return [
'urs_imap_host' => '{your_imap_server:993/imap/ssl}INBOX', 'urs_imap_host' => '{your_imap_server:993/imap/ssl}INBOX',
'urs_imap_username' => 'your_username', 'urs_imap_username' => 'your_username',
'urs_imap_password' => 'your_password', 'urs_imap_password' => 'your_password',
// Notifications Configuration
'mailer' => 'phpmailer', // sendgrid, mailgun are also available
'mailer_api_key' => 'YOUR_API_KEY',
'mailer_domain' => 'example.com',
'mailer_from' => 'from@example.com',
'mailer_smtp_host' => 'smtp.example.com',
'mailer_smtp_username' => 'your_email@example.com',
'mailer_smtp_password' => 'your_password',
'mailer_smtp_port' => 587,
'mailer_sms' => 'twilio', // telesign, plivo, vonage, clickatell are also available
'mailer_sms_account' => 'YOUR_ACCOUNT_SID/USERNAME',
'mailer_sms_auth' => 'YOUR_AUTH_TOKEN/PASSWORD',
]; ];

View file

@ -1,9 +1,140 @@
<?php <?php
use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use \Utopia\Messaging\Messages\Email;
use \Utopia\Messaging\Adapters\Email\SendGrid;
use \Utopia\Messaging\Adapters\Email\Mailgun;
use \Utopia\Messaging\Messages\SMS;
use \Utopia\Messaging\Adapters\SMS\Twilio;
use \Utopia\Messaging\Adapters\SMS\Telesign;
use \Utopia\Messaging\Adapters\SMS\Plivo;
use \Utopia\Messaging\Adapters\SMS\Vonage;
use \Utopia\Messaging\Adapters\SMS\Clickatell;
require __DIR__ . '/vendor/autoload.php';
$c = require_once 'config.php';
require_once 'helpers.php';
$server = new Server("127.0.0.1", 8250);
$server->on("request", function (Request $request, Response $response) use ($c) {
// Parse the received data
$data = json_decode($request->rawContent(), true);
if (!$data || !isset($data['type'])) {
$response->end(json_encode(['status' => 'error', 'message' => 'Invalid request']));
return;
}
switch ($data['type']) {
case 'sendmail':
if ($c['mailer'] == 'phpmailer') {
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = $c['mailer_smtp_host'];
$mail->SMTPAuth = true;
$mail->Username = $c['mailer_smtp_username'];
$mail->Password = $c['mailer_smtp_password'];
$mail->SMTPSecure = 'tls';
$mail->Port = $c['mailer_smtp_port'];
$mail->setFrom($c['mailer_from']);
$mail->addAddress($data['toEmail']);
$mail->Subject = $data['subject'];
$mail->Body = $data['body'];
$mail->send();
} catch (Exception $e) {
$response->end(json_encode(['status' => 'error', 'message' => 'Mail could not be sent. PHPMailer Error: ' . $mail->ErrorInfo]));
return;
}
} elseif ($c['mailer'] == 'sendgrid') {
$message = new Email(
from: [$c['mailer_from']],
to: [$data['toEmail']],
subject: $data['subject'],
content: $data['body']
);
$messaging = new Sendgrid($c['mailer_api_key']);
$messaging->send($message);
} elseif ($c['mailer'] == 'mailgun') {
$message = new Email(
from: [$c['mailer_from']],
to: [$data['toEmail']],
subject: $data['subject'],
content: $data['body']
);
$messaging = new Mailgun($c['mailer_api_key'], $c['mailer_domain']);
$messaging->send($message);
} else {
$response->end(json_encode(['status' => 'error', 'message' => 'Invalid mailer specified']));
return;
}
break;
case 'sendsms':
if ($c['mailer_sms'] == 'twilio') {
$message = new SMS(
to: [$data['toSMS']],
content: $data['contentSMS']
);
$messaging = new Twilio($c['mailer_sms_account'], $c['mailer_sms_auth']);
$messaging->send($message);
} elseif ($c['mailer_sms'] == 'telesign') {
$message = new SMS(
to: [$data['toSMS']],
content: $data['contentSMS']
);
$messaging = new Telesign($c['mailer_sms_account'], $c['mailer_sms_auth']);
$messaging->send($message);
} elseif ($c['mailer_sms'] == 'plivo') {
$message = new SMS(
to: [$data['toSMS']],
content: $data['contentSMS']
);
$messaging = new Plivo($c['mailer_sms_account'], $c['mailer_sms_auth']);
$messaging->send($message);
} elseif ($c['mailer_sms'] == 'vonage') {
$message = new SMS(
to: [$data['toSMS']],
content: $data['contentSMS']
);
$messaging = new Vonage($c['mailer_sms_account'], $c['mailer_sms_auth']);
$messaging->send($message);
} elseif ($c['mailer_sms'] == 'clickatell') {
$message = new SMS(
to: [$data['toSMS']],
content: $data['contentSMS']
);
$messaging = new Clickatell($c['mailer_sms_account']);
$messaging->send($message);
} else {
$response->end(json_encode(['status' => 'error', 'message' => 'Invalid SMS provider specified']));
return;
}
break;
default:
$response->end(json_encode(['status' => 'error', 'message' => 'Unknown action']));
return;
}
$response->end(json_encode(['status' => 'success']));
});
$server->start();
/* USAGE /* USAGE
$url = 'http://127.0.0.1:9501'; $url = 'http://127.0.0.1:8250';
$data = ['type' => 'sendmail', 'other_params' => '...']; $data = ['type' => 'sendgrid', 'other_params' => '...'];
$options = [ $options = [
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
@ -26,80 +157,4 @@ if ($response === false) {
curl_close($curl); curl_close($curl);
print_r($response);*/ print_r($response);*/
use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;
$server = new Server("127.0.0.1", 9501);
$server->on("request", function (Request $request, Response $response) {
// Parse the received data
$data = json_decode($request->rawContent(), true);
if (!$data || !isset($data['type'])) {
$response->end(json_encode(['status' => 'error', 'message' => 'Invalid request']));
return;
}
switch ($data['type']) {
case 'sendmail':
if (isset($data['mailer']) && $data['mailer'] == 'phpmailer') {
// Use PHPMailer
$mail = new PHPMailer(true);
try {
$mail->setFrom($data['fromEmail'], $data['fromName']);
$mail->addAddress($data['toEmail'], $data['toName']);
$mail->Subject = $data['subject'];
$mail->Body = $data['body'];
$mail->send();
} catch (Exception $e) {
$response->end(json_encode(['status' => 'error', 'message' => 'Mail could not be sent. PHPMailer Error: ' . $mail->ErrorInfo]));
return;
}
} elseif (isset($data['mailer']) && $data['mailer'] == 'utopia') {
// Use utopia-php/messaging for email
// You'd have to set up Utopia's configurations accordingly
} elseif (isset($data['mailer']) && $data['mailer'] == 'sendmail') {
// Use Sendmail or another MIT licensed PHP mailer
// Implement the logic accordingly
} else {
$response->end(json_encode(['status' => 'error', 'message' => 'Invalid mailer specified']));
return;
}
break;
case 'sendsms':
if (isset($data['smsProvider']) && $data['smsProvider'] == 'twilio') {
// Use Twilio API
$twilio = new \Twilio\Rest\Client($twilio_sid, $twilio_token);
$message = $twilio->messages->create(
$data['toPhone'],
['from' => $twilio_phone_number, 'body' => $data['body']]
);
} elseif (isset($data['smsProvider']) && $data['smsProvider'] == 'utopia') {
// Use utopia-php/messaging for SMS
// You'd have to set up Utopia's configurations and use its API
} else {
$response->end(json_encode(['status' => 'error', 'message' => 'Invalid SMS provider specified']));
return;
}
break;
default:
$response->end(json_encode(['status' => 'error', 'message' => 'Unknown action']));
return;
}
$response->end(json_encode(['status' => 'success']));
});
$server->start();