diff --git a/automation/config.php.dist b/automation/config.php.dist index 951748d..4cdfee5 100644 --- a/automation/config.php.dist +++ b/automation/config.php.dist @@ -36,4 +36,18 @@ return [ 'urs_imap_host' => '{your_imap_server:993/imap/ssl}INBOX', 'urs_imap_username' => 'your_username', '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', ]; \ No newline at end of file diff --git a/automation/notifications.php b/automation/notifications.php index c421080..4bbdc7e 100644 --- a/automation/notifications.php +++ b/automation/notifications.php @@ -1,9 +1,140 @@ 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 -$url = 'http://127.0.0.1:9501'; -$data = ['type' => 'sendmail', 'other_params' => '...']; +$url = 'http://127.0.0.1:8250'; +$data = ['type' => 'sendgrid', 'other_params' => '...']; $options = [ CURLOPT_RETURNTRANSFER => true, @@ -26,80 +157,4 @@ if ($response === false) { curl_close($curl); -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(); +print_r($response);*/ \ No newline at end of file